diff --git a/CHANGELOG.md b/CHANGELOG.md index c51e5c7..c396c5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # OpenUri Changelog +## 7.1.4 + +- refactor: for ST >= 4132, utilize `view.expand_to_scope` API + ## 7.1.3 - refactor: simplify `boot.py` diff --git a/plugin/constant.py b/plugin/constant.py index cbc7859..47dc1d5 100644 --- a/plugin/constant.py +++ b/plugin/constant.py @@ -1,2 +1,7 @@ +import sublime + + PLUGIN_NAME = __package__.partition(".")[0] SETTINGS_FILE_NAME = f"{PLUGIN_NAME}.sublime-settings" + +ST_SUPPORT_EXPAND_TO_SCOPE = int(sublime.version()) >= 4132 diff --git a/plugin/utils.py b/plugin/utils.py index d0785b0..d22c147 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -1,3 +1,4 @@ +from .constant import ST_SUPPORT_EXPAND_TO_SCOPE from .types import RegionLike from typing import ( Any, @@ -95,27 +96,36 @@ def view_find_all( expand_selectors: Iterable[str] = tuple(), ) -> Generator[sublime.Region, None, None]: """ - @brief A faster/simpler implementation of View.find_all(). + @brief Find all content matching the regex and expand found regions with selectors. @param view the View object @param regex_obj the compiled regex object - @param expand_selector the selectors used to expand result regions + @param expand_selector the selectors used to expand found regions @return A generator for found regions """ - if isinstance(expand_selectors, str): - expand_selectors = (expand_selectors,) + def expand(region: sublime.Region) -> sublime.Region: + # still having a ST core bug: https://github.com/sublimehq/sublime_text/issues/5333 + if ST_SUPPORT_EXPAND_TO_SCOPE: + return next( + filter(None, (view.expand_to_scope(region.a, selector) for selector in expand_selectors)), + region, + ) - for m in regex_obj.finditer(view.substr(sublime.Region(0, len(view)))): - r = sublime.Region(*m.span()) for selector in expand_selectors: - if not view.match_selector(r.a, selector): + if not view.match_selector(region.a, selector): continue - while view.match_selector(r.b, selector): - r.b += 1 + while view.match_selector(region.b, selector): + region.b += 1 break - yield r + return region + + if isinstance(expand_selectors, str): + expand_selectors = (expand_selectors,) + + for m in regex_obj.finditer(view.substr(sublime.Region(0, len(view)))): + yield expand(sublime.Region(*m.span())) @overload