Skip to content

Commit

Permalink
refactor: 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 Mar 1, 2024
1 parent 020bb04 commit 16b2912
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion plugin/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 21 additions & 4 deletions plugin/rules/constraints/selector_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 16b2912

Please sign in to comment.