Sublime Forum

View.find_by_class() returns an int?

#1

According to the API reference, View.find_by_class() returns a region. However this plugin code:

import sublime
import sublime_plugin

class TestPlugin(sublime_plugin.TextCommand):

    def run(self, edit):
        next_region = self.view.find_by_class(
            0,
            True,
            sublime.CLASS_LINE_START
        )

        print(type(next_region))

…prints this:

>>> view.run_command("test_plugin")
<class 'int'>

What’s happening?

0 Likes

#2

If I had to guess I would say that it’s a documentation bug, since all of the items that it can search for are things that would be a single character position and not a range of characters. The return of a Region makes more sense for e.g. view.expand_by_class since it’s looking in both directions at the same time.

As such the return is telling you the buffer offset of the next item of the given class after the position that you passed it, so in your case it’s telling you the buffer offset of the second line in the file since the starting position is always 0.

1 Like

#3

Why the offset from the starting position and not from the start of the buffer?

0 Likes

#4

I don’t understand your question; your example code specifies position 0 at the start position, and position 0 is always the start of the buffer.

0 Likes

#5

Oh I was asking about this bit:

As such the return is telling you the buffer offset of the next item of the given class

I thought you meant that the return would, in general, be telling me the offset of the next item from the passed-in position.

0 Likes