Skip to content

Commit

Permalink
refactor: tidy codes
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 8f8665f commit 4ea2fa6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
4 changes: 2 additions & 2 deletions plugin/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .timer import RepeatingTimer
from .utils import is_processable_view
from .utils import is_transient_view
from .utils import view_find_all_fast
from .utils import view_find_all
from typing import Generator
import sublime

Expand Down Expand Up @@ -60,7 +60,7 @@ def _update_view(self, view: sublime.View) -> None:

def _detect_uris_globally(self, view: sublime.View) -> None:
uri_regions = tuple(
view_find_all_fast(
view_find_all(
view,
global_get("uri_regex_obj"),
get_setting("expand_uri_regions_selectors"),
Expand Down
23 changes: 7 additions & 16 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
cast,
overload,
)
import itertools
import sublime

AnyCallable = TypeVar("AnyCallable", bound=Callable[..., Any])
Expand Down Expand Up @@ -88,7 +89,7 @@ def dotted_set(var: Any, dotted: str, value: Any) -> None:
setattr(var, last_key, value)


def view_find_all_fast(
def view_find_all(
view: sublime.View,
regex_obj: Pattern[str],
expand_selectors: Iterable[str] = tuple(),
Expand Down Expand Up @@ -184,7 +185,7 @@ def region_expand(
if isinstance(region, sublime.Region):
return sublime.Region(region.a - expansion[0], region.b + expansion[1])

return (region[0] - expansion[0], region[-1] + expansion[1])
return (region[0] - expansion[0], region[-1] + expansion[-1])


def convert_to_region_tuple(region: RegionLike, sort: bool = False) -> Tuple[int, int]:
Expand All @@ -200,11 +201,11 @@ def convert_to_region_tuple(region: RegionLike, sort: bool = False) -> Tuple[int
seq: Sequence[int]

if isinstance(region, sublime.Region):
seq = (region.a, region.b)
seq = region.to_tuple()
elif isinstance(region, int):
seq = (region, region)
elif isinstance(region, Iterable):
seq = tuple(region)[:2]
seq = tuple(itertools.islice(region, 2))

if sort:
seq = sorted(seq)
Expand All @@ -222,17 +223,7 @@ def convert_to_st_region(region: RegionLike, sort: bool = False) -> sublime.Regi
@return the "region" in ST's region form
"""

seq: Sequence[int]

if isinstance(region, int):
seq = (region, region)
elif isinstance(region, Iterable):
seq = tuple(region)[:2]

if sort:
seq = sorted(seq)

return sublime.Region(seq[0], seq[-1])
return sublime.Region(*convert_to_region_tuple(region, sort))


def merge_regions(regions: Iterable[sublime.Region], allow_boundary: bool = False) -> List[sublime.Region]:
Expand Down Expand Up @@ -270,7 +261,7 @@ def is_regions_intersected(region1: sublime.Region, region2: sublime.Region, all
@return True if intersected, False otherwise.
"""

return region1.intersects(region2) or (allow_boundary and bool(set(region1.to_tuple()) & set(region2.to_tuple())))
return region1.intersects(region2) or (allow_boundary and len({*region1.to_tuple(), *region2.to_tuple()}) != 4)


def is_processable_view(view: sublime.View) -> bool:
Expand Down

0 comments on commit 4ea2fa6

Please sign in to comment.