Sublime Forum

What's the view.replace algorithm?

#1

I’m trying to understand how view.replace method works but I haven’t been able to figure it yet…

Docs says:

replace(edit, region, string) None Replaces the contents of the region with the given string.

not much relevant info… and the method in sublime.py is just a simple wrapper:

def replace(self, edit, r, text):
    if edit.edit_token == 0:
        raise ValueError("Edit objects may not be used after the TextCommand's run method has returned")

    sublime_api.view_replace(self.view_id, edit.edit_token, r, text)

So I’ve tried to analize the behaviour with the next dummy TextCommand:

class FooCommand(sublime_plugin.TextCommand):

    def run(self, edit, block=False):
        self.view.replace(edit, sublime.Region(0,10), "abcde")

Here’s the tests I’ve used to understand the behaviour:

demo

But this is still eluding me… :frowning:

@jps or @wbond Hey guys, sorry to ping… I won’t ask for sublime_api.view_replace source but would you mind to explain how the algorithm works internally? :blush:

Thanks in advance!

0 Likes

#2

What part specifically are you having problems with? Are you curious about how it decides the cursor positions when there are selections inside of the region you replace?

0 Likes

#3

@OdatNurd Right now I’ve got this:

def replace(self, r, text):
    offset = len(text)

    self.set_selection(r)        
    self.replace_selected_text(text)

    sels = []
    for i,s in enumerate(reversed(self.selections)):
        if s.begin()>=r.b:
            sels.append(Region(s.a+r.size()-1,s.b+r.size()-1))
        elif s.end()<=r.a:
            sels.append(Region(s.a,s.b))

    self.selections = sels

In the above code self.selections is a list containing the original selections (Regions)… I don’t know what’s going on when neither of the 2 inner loop ifs are True (ie: s.begin()<r.b and s.end()>r.a)), do you?

What part specifically are you having problems with? Are you curious about how it decides the cursor positions when there are selections inside of the region you replace?

Yeah, pretty much

0 Likes

#4

Nobody? Just to clarify a little bit more here… Reason I’m asking about this is cos I’m creating a very specific IDE (3d graphics) using Scintilla and there are some features from Sublime I’d like to mimick… it’s not like I’m trying to create a Sublime replacement haha… Sublime is my main choice when it comes to serious coding.

I’ve already tried embedding directly Sublime on my app by using win32 API and also sending the data from Sublime to my IDE with some custom commands and interprocess communications… none of these solutions were ideal so that’s why I’m coding my little Scintilla widget trying to mimick some of the Sublime features I use a lot.

And just for the record… if there was available a Sublime widget you could embedd easily on qt apps offered by a reasonable prize I wouldn’t mind to pay for it. Coding a good text editor like ST is just too time consuming :confused:

0 Likes

#5

I’m not sure I see why it’s necessary to exactly replicate the nuances of the replacement algorithm in your own code for something like this. Do you often have multiple cursors on the same line while background code replaces the contents of the buffer?

0 Likes

Open Source
#6

That’s a reasonable question but… it’s not like I’ll use directly these little building blocks such as insert, erase or replace directly for some specific purpose but instead I’ll use commands such as comment.py on scintilla, which happens to use these builtin ones.

I’d asked about proper commenting feature few months ago and I was using the answer provided by @matkuki but recently I’ve detected in some cases it wasn’t behaving very well, that’s why I’d like to mimick exactly Sublime’s commenting. Which is something I use very very often :slight_smile:

Thing here is, when it comes to web development you’ll find working out of the box modern solutions such as ace but for dekstop other than Scintilla I’m not aware of any any reasonable solution that will bring me the quality I’d expect from a modern state of the art such as ST.

Btw, I’m confused, before you asked me

What part specifically are you having problems with? Are you curious about how it decides the cursor positions when there are selections inside of the region you replace?

I assumed you’d already figured out the logic behind the curtains :wink:

0 Likes

#7

Just for the record, I’d solved this one by myself long time ago (or that’s what I think)… today I’ve pushed my versions of both insert/erase/replace algorithms to pyblime… Probably the recomputation of selections in SublimeText is made differently but the tests I did at that time were passing succesfully.

For people who’s interested to know more about how these algorithms work behind the curtains you can take a look here:

@OdatNurd Hopefully all the questions I’ve been opening for the last weeks makes more sense to you now :slight_smile:

0 Likes