From b46143e9746b30eb4cf1d6addc9b8ff1a31816e1 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Fri, 24 May 2024 20:13:45 +0800 Subject: [PATCH] refactor: tidy codes Signed-off-by: Jack Cherng --- plugin/data_types.py | 3 ++- plugin/helpers.py | 2 +- plugin/settings.py | 2 -- plugin/view_manager.py | 8 ++++---- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/plugin/data_types.py b/plugin/data_types.py index 52ab020..0d144da 100644 --- a/plugin/data_types.py +++ b/plugin/data_types.py @@ -12,6 +12,7 @@ POINT = int TUPLE_REGION = Tuple[POINT, POINT] +INDENT_LEVEL = int if sys.version_info >= (3, 11): from enum import StrEnum @@ -41,7 +42,7 @@ def indent_length(self) -> int: @cached_property def indent_pattern(self) -> str: - return rf"^({self.indent_chars})+" + return rf"^(?:{self.indent_chars})+" @cached_property def indent_pattern_compiled(self) -> Pattern[str]: diff --git a/plugin/helpers.py b/plugin/helpers.py index f84e72e..14ffd3d 100644 --- a/plugin/helpers.py +++ b/plugin/helpers.py @@ -13,7 +13,7 @@ def is_renderable_view(view: sublime.View) -> bool: # activation by plugin's logics return bool( view.is_valid() - and view.element() is None + and not view.element() and not is_transient_view(view) and (syntax := view.syntax()) and sublime.score_selector(syntax.scope, get_enabled_selector()) > 0 diff --git a/plugin/settings.py b/plugin/settings.py index 8146c93..f401601 100644 --- a/plugin/settings.py +++ b/plugin/settings.py @@ -31,8 +31,6 @@ def get_plugin_setting(key: str) -> Any: ... def get_plugin_setting(key: str, default: None) -> Any: ... @overload def get_plugin_setting(key: str, default: _T) -> _T: ... - - def get_plugin_setting(key: str, default: Any = None) -> Any: return get_plugin_settings().get(key, default) diff --git a/plugin/view_manager.py b/plugin/view_manager.py index f3e1475..f81a467 100644 --- a/plugin/view_manager.py +++ b/plugin/view_manager.py @@ -6,7 +6,7 @@ import sublime -from .data_types import IndentInfo, LevelStyle +from .data_types import INDENT_LEVEL, IndentInfo, LevelStyle from .helpers import get_regions_key from .indent_renderer import AbstractIndentRenderer, find_indent_renderer from .settings import get_level_colors, get_level_style @@ -17,9 +17,9 @@ def calcualte_level_regions( *, indent_info: IndentInfo | None = None, regions: Sequence[sublime.Region] | None = None, -) -> defaultdict[int, list[sublime.Region]]: +) -> defaultdict[INDENT_LEVEL, list[sublime.Region]]: """ - Calculates the begin point of indents for each level. + Calculates regions of indents for each level. :param view: The view. :param indent_info: The indent information. @@ -36,7 +36,7 @@ def calcualte_level_regions( regions = (sublime.Region(0, view.size()),) whole_content = view.substr(sublime.Region(0, view.size())) - level_regions: defaultdict[int, list[sublime.Region]] = defaultdict(list) + level_regions: defaultdict[INDENT_LEVEL, list[sublime.Region]] = defaultdict(list) for region in regions: for m in indent_info.indent_pattern_compiled.finditer(whole_content, region.begin(), region.end()): for level, level_pt in enumerate(range(m.start(), m.end(), indent_info.indent_length)):