Skip to content

Commit

Permalink
feat: use view.expand_to_scope API for ST >= 4132
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Mar 29, 2022
1 parent 4ea2fa6 commit 33a7c61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
5 changes: 5 additions & 0 deletions plugin/constant.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 20 additions & 10 deletions plugin/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .constant import ST_SUPPORT_EXPAND_TO_SCOPE
from .types import RegionLike
from typing import (
Any,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 33a7c61

Please sign in to comment.