Skip to content

Commit d375cf3

Browse files
committed
Syntax: Split code paths for test completions
Also simplify '^' code and outsource it into a function. Fixes #224 where `<-` completions would not work on the first test line.
1 parent 5f563f6 commit d375cf3

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

plugins_/syntaxtest_dev.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def function():
239239
# as any scopes that doesn't appear in this index aren't worth checking, they can't be common
240240

241241
# skip the base scope i.e. `source.python`
242-
check_scopes = scopes[0].split()[1:]
242+
check_scopes = next(iter(scopes)).split()[1:]
243243

244244
shared_scopes = ''
245245
# stop as soon as at least one shared scope was found
@@ -328,6 +328,7 @@ def run(self, edit, character='^'):
328328
view = self.view
329329
view.replace(edit, view.sel()[0], character)
330330
insert_at = view.sel()[0].begin()
331+
_, col = view.rowcol(insert_at)
331332

332333
listener = sublime_plugin.find_view_event_listener(view, SyntaxTestHighlighterListener)
333334
if not listener.header:
@@ -341,43 +342,15 @@ def run(self, edit, character='^'):
341342
else:
342343
end_token = ''
343344

344-
scopes = []
345-
length = 0
346-
# find the following columns on the line to be tested where the scopes don't change
347-
test_at_start_of_comment = False
348-
col = view.rowcol(insert_at)[1]
349-
assertion_colrange = lines[0].assertion_colrange or (-1, -1)
350-
if assertion_colrange[0] == assertion_colrange[1]:
351-
col = assertion_colrange[1]
352-
test_at_start_of_comment = True
353-
lines = lines[1:]
354-
355-
col_start, col_end = lines[0].assertion_colrange
356-
base_scope = path.commonprefix([
357-
view.scope_name(pos)
358-
for pos in range(line.begin() + col_start, line.begin() + col_end)
359-
])
360-
361-
for pos in range(line.begin() + col, line.end() + 1):
362-
scope = view.scope_name(pos)
363-
if len(scopes) == 0:
364-
scopes.append(scope)
365-
elif not scope.startswith(base_scope):
366-
break
367-
length += 1
368-
if test_at_start_of_comment:
369-
break
370-
371-
# find the unique scopes at each existing assertion position
372-
if lines and not test_at_start_of_comment:
373-
col_start, col_end = lines[0].assertion_colrange
374-
for pos in range(line.begin() + col_start, line.begin() + col_end):
375-
scope = view.scope_name(pos)
376-
if scope not in scopes:
377-
scopes.append(scope)
345+
if character == '-':
346+
length = 1
347+
scopes = {view.scope_name(line.begin() + col)}
348+
elif character == '^':
349+
length, scopes = self.determine_test_extends(lines, line, col)
350+
else:
351+
return
378352

379353
suggest_suffix = get_setting('syntax_test.suggest_scope_suffix', True)
380-
381354
scope = find_common_scopes(scopes, not suggest_suffix)
382355

383356
# delete the existing selection
@@ -394,6 +367,29 @@ def run(self, edit, character='^'):
394367
insert_at + length + len(' ' + scope + end_token)
395368
))
396369

370+
def determine_test_extends(self, lines, line, start_col):
371+
"""Determine extend of token(s) to test and return lenght and scope set.
372+
373+
To be precise, increase column as long as the selector wouldn't change
374+
and collect the scopes.
375+
"""
376+
view = self.view
377+
col_start, col_end = lines[0].assertion_colrange
378+
scopes = {
379+
view.scope_name(pos)
380+
for pos in range(line.begin() + col_start, line.begin() + col_end)
381+
}
382+
base_scope = path.commonprefix(list(scopes))
383+
384+
length = 0
385+
for pos in range(line.begin() + col_end - 1, line.end() + 1):
386+
scope = view.scope_name(pos)
387+
if scope.startswith(base_scope):
388+
scopes.add(scope)
389+
length += 1
390+
391+
return length, scopes
392+
397393

398394
class AssignSyntaxTestSyntaxListener(sublime_plugin.EventListener):
399395

0 commit comments

Comments
 (0)