From 16b2912dd2cb62243e7f6887bc4799798386ede5 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Mon, 26 Feb 2024 18:12:29 +0800 Subject: [PATCH] refactor: tidy codes Signed-off-by: Jack Cherng --- plugin/listener.py | 2 +- plugin/rules/constraints/selector_matches.py | 25 ++++++++++++++++---- plugin/utils.py | 6 ++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/plugin/listener.py b/plugin/listener.py index 09974c14..e5b96c4d 100644 --- a/plugin/listener.py +++ b/plugin/listener.py @@ -78,7 +78,7 @@ def names_as_str(items: Iterable[Any], *, sep: str = ", ") -> str: def _configured_debounce(func: _T_Callable) -> _T_Callable: """Debounce a function so that it's called once in seconds.""" - def debounced(*args: Any, **kwargs: Any) -> None: + def debounced(*args: Any, **kwargs: Any) -> Any: if (time_s := get_merged_plugin_setting("debounce", 0)) > 0: return debounce(time_s)(func)(*args, **kwargs) return func(*args, **kwargs) diff --git a/plugin/rules/constraints/selector_matches.py b/plugin/rules/constraints/selector_matches.py index 677de920..5e2ee045 100644 --- a/plugin/rules/constraints/selector_matches.py +++ b/plugin/rules/constraints/selector_matches.py @@ -9,11 +9,28 @@ @final class SelectorMatchesConstraint(AbstractConstraint): - # Quick tips: - # - sublime.score_selector('a.b', 'b') == 0 - # - sublime.score_selector('a.b', '') == 1 - # - sublime.score_selector('a.b', 'a') == 8 SCORE_THRESHOLD = 1 + """ + Quick tips (ST >= 4173): + + ```python + sublime.score_selector("a.b", "b") == 0 + sublime.score_selector("a.b", "") == 1 + sublime.score_selector("a.b", " ") == 1 + sublime.score_selector("a.b", "a") == 1 + sublime.score_selector("a.b", "a.b") == 2 + ``` + + Quick tips (ST < 4173): + + ```python + sublime.score_selector("a.b", "b") == 0 + sublime.score_selector("a.b", "") == 1 + sublime.score_selector("a.b", " ") == 1 + sublime.score_selector("a.b", "a") == 8 + sublime.score_selector("a.b", "a.b") == 16 + ``` + """ def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) diff --git a/plugin/utils.py b/plugin/utils.py index cc996b1f..0559819e 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -323,7 +323,11 @@ def list_all_views(*, include_transient: bool = False) -> Generator[sublime.View def get_view_by_id(id: int) -> sublime.View | None: - return first_true(list_all_views(include_transient=True), pred=lambda view: view.id() == id) + return view if (view := sublime.View(id)).is_valid() else None + + +def get_window_by_id(id: int) -> sublime.Window | None: + return window if (window := sublime.Window(id)).is_valid() else None def head_tail_content(content: str, partial: int) -> str: