Sublime Forum

What's the logic behind view.indented_region?

#1

Consider this text

    x
        x
        x
            x
        x
x    
 x
  x   
    x
        x
        x
            x
        x
x

and this command:

class FooCommand(sublime_plugin.TextCommand):

    def run(self, edit, block=False):
        view = self.view
        pt = view.sel()[0].a
        view.sel().clear()
        view.sel().add(view.indented_region(pt))

and then just play around with it:

showcase

My question, does anyone know what’s the logic behind this simple command? It should be something really easy but I don’t understand it… :confused:

0 Likes

#2

@wbond When you’ve got a moment, could you please confirm us the Sublime’s classify routine is not buggy regarding those corner cases and it’s just intended behaviour? Thanks!

0 Likes

#3

I think you posted this in the wrong thread; this one is about the indented regions, not the classify routine.

0 Likes

#4

Oops, yeah, wrong thread, ty to let me know! When I’m back home tonight I’ll make more interesting the other thread by posting an example that shows the differences between sublime’s classify an a pure python implementation… As I’m also curious how much faster sublime’s could be… Although python re module is quite fast…

0 Likes

#5

For those who could be also interested to know about view.indented_region internal behaviour, here’s what I’ve figured out after lot of testing.

An indented region from a given position is a set of contiguous lines where the indentation_level of the contiguous lines of the base line (line from a given position) is greater or equal.

So yeah, the key to understand this function was just to understand indentation_level in the first place. I must to say the Sublime’s API is really great btw :slight_smile:

0 Likes

#6

Just as an FYI (and mild self promotion), the SnAPI package I announced on Monday has documentation on this:

Note that the documentation in SnAPI is taken directly from the Official Documentation (and Copyrighted by SublimeHQ), but anything that’s undocumented is my own take on it (since there is no official documentation in that case).

2 Likes

#7

@OdatNurd Oh, wow! I didn’t know there was this type of interesting documentation available :open_mouth: … thanks to point out! Tomorrow I’ll definitely give it a shot to your package to see what other pearls of wisdom may offer :wink:

0 Likes

#8

@OdatNurd Just to let you know there will be special cases where even if the line containing the given point returns an indentation_level of 0 the returned region will be a non 0-length region… I’m trying to discover the exact conditions when this happens though :wink:

0 Likes