From 4caed5b45ada0e73ba256178bcf87f00818e8936 Mon Sep 17 00:00:00 2001 From: Thom Smith Date: Fri, 12 Mar 2021 15:03:14 -0500 Subject: [PATCH 1/3] Add option to trim redundant prefixes of syntax test suggestions. --- Package/PackageDev.sublime-settings | 4 ++++ plugins/syntaxtest_dev.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Package/PackageDev.sublime-settings b/Package/PackageDev.sublime-settings index afc8629f..22086be7 100644 --- a/Package/PackageDev.sublime-settings +++ b/Package/PackageDev.sublime-settings @@ -31,6 +31,10 @@ // then suggest meta.example.markdown (true) vs meta.example (false). "syntax_test.suggest_scope_suffix": true, + // Whether to trim leading scopes from suggestions + // that are already part of previous assertions. + "syntax_test.suggest_trimmed_prefix": false, + /// ADVANCED SETTINGS ///////////////////////////////////////////////////////////////////////// diff --git a/plugins/syntaxtest_dev.py b/plugins/syntaxtest_dev.py index 61e4d1fb..908dbf07 100644 --- a/plugins/syntaxtest_dev.py +++ b/plugins/syntaxtest_dev.py @@ -352,6 +352,22 @@ def run(self, edit, character='^'): suggest_suffix = get_setting('syntax_test.suggest_scope_suffix', True) scope = find_common_scopes(scopes, not suggest_suffix) + trim_prefix = get_setting('syntax_test.suggest_trimmed_prefix', False) + if trim_prefix: + above_scopes = [ + self.view.substr(sublime.Region( + line.line_region.begin() + line.assertion_colrange[1], + line.line_region.end(), + )).strip() + for line in lines[1:] + if line.assertion_colrange[0] <= lines[0].assertion_colrange[0] + and line.assertion_colrange[1] >= lines[0].assertion_colrange[1] + ] + + for above_scope in reversed(above_scopes): + if scope.startswith(above_scope): + scope = scope[len(above_scope):].strip() + # delete the existing selection if not view.sel()[0].empty(): view.erase(edit, view.sel()[0]) From f157671f9ec5b4ce75cc3eb5391f0850383ea25c Mon Sep 17 00:00:00 2001 From: Thom Smith Date: Sat, 20 Mar 2021 14:22:29 -0400 Subject: [PATCH 2/3] Improve behavior of scope prefix trimming. --- plugins/syntaxtest_dev.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/syntaxtest_dev.py b/plugins/syntaxtest_dev.py index 908dbf07..9b0c2c26 100644 --- a/plugins/syntaxtest_dev.py +++ b/plugins/syntaxtest_dev.py @@ -365,8 +365,17 @@ def run(self, edit, character='^'): ] for above_scope in reversed(above_scopes): - if scope.startswith(above_scope): - scope = scope[len(above_scope):].strip() + score = sublime.score_selector(scope, above_scope) + if score > 0: + scope_parts = scope.split(' ') + matched_count = -(-score.bit_length() // 3) - 1 + + score_of_last_part = score >> matched_count*3 + maximum_possible_score_of_last_part = len(scope_parts[matched_count - 1].split('.')) + if score_of_last_part != maximum_possible_score_of_last_part: + matched_count -= 1 + + scope = ' '.join(scope_parts[matched_count:]) # delete the existing selection if not view.sel()[0].empty(): From a78ded866828a394c7119fbfb65d27cebbad1d5c Mon Sep 17 00:00:00 2001 From: Thom Smith Date: Sat, 20 Mar 2021 14:24:35 -0400 Subject: [PATCH 3/3] Fix flake errors. --- plugins/syntaxtest_dev.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/syntaxtest_dev.py b/plugins/syntaxtest_dev.py index 9b0c2c26..94e0c9e1 100644 --- a/plugins/syntaxtest_dev.py +++ b/plugins/syntaxtest_dev.py @@ -370,9 +370,9 @@ def run(self, edit, character='^'): scope_parts = scope.split(' ') matched_count = -(-score.bit_length() // 3) - 1 - score_of_last_part = score >> matched_count*3 - maximum_possible_score_of_last_part = len(scope_parts[matched_count - 1].split('.')) - if score_of_last_part != maximum_possible_score_of_last_part: + score_of_last_part = score >> matched_count * 3 + possible_score_of_last_part = len(scope_parts[matched_count - 1].split('.')) + if score_of_last_part != possible_score_of_last_part: matched_count -= 1 scope = ' '.join(scope_parts[matched_count:])