Sublime Forum

How to implement split_into_lines command?

#1

I’d like to implement my own version of the builtin split_into_lines command, so far I’ve got this:

class MySplitIntoLinesCommand(sublime_plugin.TextCommand):

    def run(self, edit):
        view = self.view
        lst = []
        for s in view.sel():
            for i,l in enumerate(view.lines(s)):
                if i==0:
                    lst.append(Region(s.begin(),l.end()))
                else:
                    lst.append(l)

        view.sel().clear()
        view.sel().add_all(lst)

but it doesn’t work as the original one… guess using view.lines isn’t the way to go here.

I’ve found on the internet a vscode extension that looks like this, pretty interesting that lineAt method…

Can you think of a simple way to implement this one? I can think of few but I’d like a simple one that uses the Sublime API :slight_smile:

0 Likes

#2

From some quick experimentation the main difference I can see between the internal implementation and yours is that if the selection ends in the middle of a line, the internal command end the last selection there and yours selects the whole last line. That’s easily solved by an extra case for when the line is the last one, though:

for s in view.sel():
    lines = view.lines(s)
    for i,l in enumerate(lines):
        if i==0:
            lst.append(sublime.Region(s.begin(),l.end()))
        elif i==len(lines)-1:
            lst.append(sublime.Region(l.begin(),s.end()))                    
        else:
            lst.append(l)

Is there another way that it doesn’t do the same thing? I didn’t investigate it overly deeply.

1 Like

#3

Is there another way that it doesn’t do the same thing? I didn’t investigate it overly deeply.

@OdatNurd Dunno, from the tests I’ve been doing it looks good to me. If eventually find any corner case I’ll come back to this thread, for the time being I’ll just validate & say “thank you very much” :smiley:

PS. This is strang!e though, I don’t see any “validate button” on your thread? :open_mouth:

showcase

0 Likes

#4

You can only mark a comment as the “answer” in the technical support category.

2 Likes