Consider this text:
# I'm a comment
def foo():
print('# No comment')
and this command:
class FooCommand(sublime_plugin.TextCommand):
def class_flags(self, flags):
CLASS_WORD_START = 1
CLASS_WORD_END = 2
CLASS_PUNCTUATION_START = 4
CLASS_PUNCTUATION_END = 8
CLASS_SUB_WORD_START = 16
CLASS_SUB_WORD_END = 32
CLASS_LINE_START = 64
CLASS_LINE_END = 128
CLASS_EMPTY_LINE = 256
CLASS_MIDDLE_WORD = 512
CLASS_WORD_START_WITH_PUNCTUATION = 1024
CLASS_WORD_END_WITH_PUNCTUATION = 2048
CLASS_OPENING_PARENTHESIS = 4096
CLASS_CLOSING_PARENTHESIS = 8192
res = []
if flags & CLASS_WORD_START: res.append("CLASS_WORD_START")
if flags & CLASS_WORD_END: res.append("CLASS_WORD_END")
if flags & CLASS_PUNCTUATION_START: res.append("CLASS_PUNCTUATION_START")
if flags & CLASS_PUNCTUATION_END: res.append("CLASS_PUNCTUATION_END")
if flags & CLASS_SUB_WORD_START: res.append("CLASS_SUB_WORD_START")
if flags & CLASS_SUB_WORD_END: res.append("CLASS_SUB_WORD_END")
if flags & CLASS_LINE_START: res.append("CLASS_LINE_START")
if flags & CLASS_LINE_END: res.append("CLASS_LINE_END")
if flags & CLASS_EMPTY_LINE: res.append("CLASS_EMPTY_LINE")
if flags & CLASS_MIDDLE_WORD: res.append("CLASS_MIDDLE_WORD")
if flags & CLASS_WORD_START_WITH_PUNCTUATION: res.append("CLASS_WORD_START_WITH_PUNCTUATION")
if flags & CLASS_WORD_END_WITH_PUNCTUATION: res.append("CLASS_WORD_END_WITH_PUNCTUATION")
if flags & CLASS_OPENING_PARENTHESIS: res.append("CLASS_OPENING_PARENTHESIS")
if flags & CLASS_CLOSING_PARENTHESIS: res.append("CLASS_CLOSING_PARENTHESIS")
return " | ".join(reversed(res))
def run(self, edit, block=False):
for i in range(self.view.size()):
l = "{}, {}, {}".format(
i, repr(self.view.substr(i)), self.view.classify(i)
)
print("{:<30}{}".format(l, self.class_flags(self.view.classify(i))))
bind that command to a key and run it on the test view, you should get this output:
0, '#', 1092 CLASS_WORD_START_WITH_PUNCTUATION | CLASS_LINE_START | CLASS_PUNCTUATION_START
1, ' ', 2056 CLASS_WORD_END_WITH_PUNCTUATION | CLASS_PUNCTUATION_END
2, 'I', 49 CLASS_SUB_WORD_END | CLASS_SUB_WORD_START | CLASS_WORD_START
3, "'", 6 CLASS_PUNCTUATION_START | CLASS_WORD_END
4, 'm', 9 CLASS_PUNCTUATION_END | CLASS_WORD_START
5, ' ', 2 CLASS_WORD_END
6, 'a', 1 CLASS_WORD_START
7, ' ', 2 CLASS_WORD_END
8, 'c', 1 CLASS_WORD_START
9, 'o', 512 CLASS_MIDDLE_WORD
10, 'm', 512 CLASS_MIDDLE_WORD
11, 'm', 512 CLASS_MIDDLE_WORD
12, 'e', 512 CLASS_MIDDLE_WORD
13, 'n', 512 CLASS_MIDDLE_WORD
14, 't', 512 CLASS_MIDDLE_WORD
15, '\n', 130 CLASS_LINE_END | CLASS_WORD_END
16, '\n', 448 CLASS_EMPTY_LINE | CLASS_LINE_END | CLASS_LINE_START
17, '\n', 448 CLASS_EMPTY_LINE | CLASS_LINE_END | CLASS_LINE_START
18, 'd', 65 CLASS_LINE_START | CLASS_WORD_START
19, 'e', 512 CLASS_MIDDLE_WORD
20, 'f', 512 CLASS_MIDDLE_WORD
21, ' ', 2 CLASS_WORD_END
22, 'f', 1 CLASS_WORD_START
23, 'o', 512 CLASS_MIDDLE_WORD
24, 'o', 512 CLASS_MIDDLE_WORD
25, '(', 4102 CLASS_OPENING_PARENTHESIS | CLASS_PUNCTUATION_START | CLASS_WORD_END
26, ')', 0
27, ':', 8192 CLASS_CLOSING_PARENTHESIS
28, '\n', 2184 CLASS_WORD_END_WITH_PUNCTUATION | CLASS_LINE_END | CLASS_PUNCTUATION_END
29, ' ', 64 CLASS_LINE_START
30, ' ', 0
31, ' ', 0
32, ' ', 0
33, 'p', 1 CLASS_WORD_START
34, 'r', 512 CLASS_MIDDLE_WORD
35, 'i', 512 CLASS_MIDDLE_WORD
36, 'n', 512 CLASS_MIDDLE_WORD
37, 't', 512 CLASS_MIDDLE_WORD
38, '(', 4102 CLASS_OPENING_PARENTHESIS | CLASS_PUNCTUATION_START | CLASS_WORD_END
39, "'", 4096 CLASS_OPENING_PARENTHESIS
40, '#', 0
41, ' ', 2056 CLASS_WORD_END_WITH_PUNCTUATION | CLASS_PUNCTUATION_END
42, 'N', 49 CLASS_SUB_WORD_END | CLASS_SUB_WORD_START | CLASS_WORD_START
43, 'o', 512 CLASS_MIDDLE_WORD
44, ' ', 2 CLASS_WORD_END
45, 'c', 1 CLASS_WORD_START
46, 'o', 512 CLASS_MIDDLE_WORD
47, 'm', 512 CLASS_MIDDLE_WORD
48, 'm', 512 CLASS_MIDDLE_WORD
49, 'e', 512 CLASS_MIDDLE_WORD
50, 'n', 512 CLASS_MIDDLE_WORD
51, 't', 512 CLASS_MIDDLE_WORD
52, "'", 6 CLASS_PUNCTUATION_START | CLASS_WORD_END
53, ')', 8192 CLASS_CLOSING_PARENTHESIS
54, '\n', 10376 CLASS_CLOSING_PARENTHESIS | CLASS_WORD_END_WITH_PUNCTUATION | CLASS_LINE_END | CLASS_PUNCTUATION_END
Couple of questions:
-
Why are you getting 0 at position 26, ie:
26, ')', 0
? Is that a Sublime bug or intended behaviour? -
Sublime only exposes these constants in
sublime.py
:CLASS_WORD_START = 1
CLASS_WORD_END = 2
CLASS_PUNCTUATION_START = 4
CLASS_PUNCTUATION_END = 8
CLASS_SUB_WORD_START = 16
CLASS_SUB_WORD_END = 32
CLASS_LINE_START = 64
CLASS_LINE_END = 128
CLASS_EMPTY_LINE = 256
but there are clearly some constants missing as you can see values given by classify are quite higher… if we look at it in limetext there are these ones missing:
CLASS_MIDDLE_WORD
CLASS_WORD_START_WITH_PUNCTUATION
CLASS_WORD_END_WITH_PUNCTUATION
CLASS_OPENING_PARENTHESIS
CLASS_CLOSING_PARENTHESIS
so the question is, why are not these constants living in sublime.py
?
Could anyone clarify? I was comparing the behaviour between limetext’s and sublime and both behave pretty similarly but there are subtle differences in some corner cases . Like for example, limetext’s routine in position 26 will give:
26, ')', 12288 CLASS_CLOSING_PARENTHESIS | CLASS_OPENING_PARENTHESIS
while in Sublime report 0, as already explained… which one is the correct?