Skip to content

Commit

Permalink
chore: tidy codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Jul 25, 2021
1 parent 11aa522 commit d17f142
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
3 changes: 1 addition & 2 deletions plugin/helpers/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def find_uri_regions_by_regions(

uri_regions.extend(
# convert "finditer()" coordinate into ST's coordinate
sublime.Region(*region_shift(m.span(), coordinate_bias)) # type: ignore
sublime.Region(*region_shift(m.span(), coordinate_bias))
for m in global_get("uri_regex_obj").finditer(view.substr(region))
)

Expand All @@ -162,7 +162,6 @@ def find_uri_regions_by_regions(

if is_regions_intersected(uri_region, region, True):
uri_regions_intersected.append(uri_region)

break

return uri_regions_intersected
Expand Down
2 changes: 1 addition & 1 deletion plugin/helpers/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _update_view(self, view: sublime.View) -> None:
view_is_dirty_val(view, False)

def _detect_uris_globally(self, view: sublime.View) -> None:
uri_regions = view_find_all_fast(view, global_get("uri_regex_obj"))
uri_regions = tuple(view_find_all_fast(view, global_get("uri_regex_obj")))

# handle Phantoms
if get_setting_show_open_button(view) == "always":
Expand Down
2 changes: 1 addition & 1 deletion plugin/helpers/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class G:
renderer_thread: Optional[threading.Thread] = None

activated_schemes: Tuple[str, ...] = tuple()
uri_regex_obj: Optional[Pattern] = None
uri_regex_obj: Optional[Pattern[str]] = None

images: Dict[str, Any] = {
# image informations
Expand Down
93 changes: 42 additions & 51 deletions plugin/helpers/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .types import RegionLike
from typing import Any, Callable, Iterable, List, Optional, Pattern, Union
from typing import Any, Callable, Generator, Iterable, List, Optional, Pattern, Sequence, Tuple, Union
import functools
import sublime

Expand Down Expand Up @@ -75,7 +75,10 @@ def dotted_set(var: Any, dotted: str, value: Any) -> None:
setattr(var, last_key, value)


def view_find_all_fast(view: sublime.View, regex_obj: Pattern) -> List[sublime.Region]:
def view_find_all_fast(
view: sublime.View,
regex_obj: Pattern[str],
) -> Generator[sublime.Region, None, None]:
"""
@brief A faster/simpler implementation of View.find_all().
Expand All @@ -85,10 +88,10 @@ def view_find_all_fast(view: sublime.View, regex_obj: Pattern) -> List[sublime.R
@return Found regions
"""

return [sublime.Region(*m.span()) for m in regex_obj.finditer(view.substr(sublime.Region(0, len(view))))]
return (sublime.Region(*m.span()) for m in regex_obj.finditer(view.substr(sublime.Region(0, len(view)))))


def region_shift(region: RegionLike, shift: int) -> RegionLike:
def region_shift(region: RegionLike, shift: int) -> Union[Tuple[int, int], sublime.Region]:
"""
@brief Shift the region by given amount.
Expand All @@ -98,16 +101,19 @@ def region_shift(region: RegionLike, shift: int) -> RegionLike:
@return the shifted region
"""

if isinstance(region, (int, float)):
return region + shift
if isinstance(region, int):
return (region + shift, region + shift)

if isinstance(region, sublime.Region):
return sublime.Region(region.a + shift, region.b + shift)

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


def region_expand(region: RegionLike, expansion: Union[int, List[int]]) -> RegionLike:
def region_expand(
region: RegionLike,
expansion: Union[int, Tuple[int, int], List[int]],
) -> Union[Tuple[int, int], sublime.Region]:
"""
@brief Expand the region by given amount.
Expand All @@ -117,54 +123,41 @@ def region_expand(region: RegionLike, expansion: Union[int, List[int]]) -> Regio
@return the expanded region
"""

if isinstance(expansion, (int, float)):
expansion = [int(expansion)] * 2
if isinstance(expansion, int):
expansion = (expansion, expansion)

if len(expansion) == 0:
raise ValueError(f"Invalid expansion: {expansion}")

if len(expansion) == 1:
# do not modify the input variable by "expansion *= 2"
expansion = [expansion[0]] * 2

if isinstance(region, (int, float)):
return [region - expansion[0], region + expansion[1]]
if isinstance(region, int):
return (region - expansion[0], region + expansion[1])

if isinstance(region, sublime.Region):
return sublime.Region(region.begin() - expansion[0], region.end() + expansion[1])
return sublime.Region(region.a - expansion[0], region.b + expansion[1])

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


def region_into_list_form(region: RegionLike, sort_result: bool = False) -> List[int]:
def region_into_tuple_form(region: RegionLike, sort_result: bool = False) -> Tuple[int, int]:
"""
@brief Convert the "region" into list form
@brief Convert the "region" into tuple form
@param region The region
@param sort_result Sort the region
@return the "region" in list form
@return the "region" in tuple form
"""

if isinstance(region, sublime.Region):
region = [region.a, region.b]
elif isinstance(region, (int, float)):
region = [int(region)] * 2
elif isinstance(region, Iterable) and not isinstance(region, list):
region = list(region)

assert isinstance(region, list)
seq: Sequence[int]

if not region:
raise ValueError("region must not be empty.")
if isinstance(region, sublime.Region):
seq = (region.a, region.b)
elif isinstance(region, int):
seq = (region, region)
elif isinstance(region, Iterable):
seq = tuple(region)[:2]

if len(region) > 0:
region = [region[0], region[-1]]
if sort_result:
seq = sorted(seq)

return sorted(region) if sort_result else region
return (seq[0], seq[-1])


def region_into_st_region_form(region: RegionLike, sort_result: bool = False) -> sublime.Region:
Expand All @@ -177,18 +170,17 @@ def region_into_st_region_form(region: RegionLike, sort_result: bool = False) ->
@return the "region" in ST's region form
"""

if isinstance(region, (int, float)):
region = [int(region)] * 2
elif isinstance(region, Iterable) and not isinstance(region, list):
region = list(region)
seq: Sequence[int]

if isinstance(region, list) and not region:
raise ValueError("region must not be empty.")
if isinstance(region, int):
seq = (region, region)
elif isinstance(region, Iterable):
seq = tuple(region)[:2]

if not isinstance(region, sublime.Region):
region = sublime.Region(region[0], region[-1])
if sort_result:
seq = sorted(seq)

return sublime.Region(region.begin(), region.end()) if sort_result else region
return sublime.Region(seq[0], seq[-1])


def simplify_intersected_regions(
Expand All @@ -208,7 +200,6 @@ def simplify_intersected_regions(
for region in sorted(regions):
if not merged_regions:
merged_regions.append(region)

continue

region_prev = merged_regions[-1]
Expand Down Expand Up @@ -258,4 +249,4 @@ def is_transient_view(view: sublime.View) -> bool:
if not (window := view.window()):
return True
# @see https://forum.sublimetext.com/t/is-view-transient-preview-method/3247/2
return window.get_view_index(view)[1] == -1
return view == window.transient_view_in_group(window.active_group())

0 comments on commit d17f142

Please sign in to comment.