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 May 22, 2024
1 parent 3c320dc commit e9c3b12
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions plugin/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from collections.abc import Generator
from contextlib import contextmanager
from typing import Any, Final
from typing import Final

import sublime
import sublime_plugin
Expand All @@ -24,7 +24,7 @@ def _editable_view(view: sublime.View) -> Generator[sublime.View, None, None]:
view.set_read_only(is_read_only)


def _find_log_panel(obj: Any) -> sublime.View | None:
def _find_log_panel(obj: sublime.Buffer | sublime.View | sublime.Sheet | sublime.Window) -> sublime.View | None:
return (resolve_window(obj) or sublime.active_window()).find_output_panel(PLUGIN_NAME)


Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any, Pattern, TypeVar, final

from more_itertools import first_true
from typing_extensions import Self

from ..cache import clearable_lru_cache
from ..constants import PLUGIN_NAME, ST_PLATFORM
Expand Down Expand Up @@ -62,7 +63,7 @@ def test(self, view_snapshot: ViewSnapshot) -> bool:
return not result if self.inverted else result

@classmethod
def make(cls, constraint_rule: ST_ConstraintRule) -> ConstraintRule:
def make(cls, constraint_rule: ST_ConstraintRule) -> Self:
"""Build this object with the `constraint_rule`."""
obj = cls()

Expand Down
3 changes: 2 additions & 1 deletion plugin/rules/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Any, Union, final

from more_itertools import first_true
from typing_extensions import Self

from ..cache import clearable_lru_cache
from ..snapshot import ViewSnapshot
Expand Down Expand Up @@ -58,7 +59,7 @@ def test(self, view_snapshot: ViewSnapshot) -> bool:
return self.match.test(view_snapshot, self.rules)

@classmethod
def make(cls, match_rule: ST_MatchRule) -> MatchRule:
def make(cls, match_rule: ST_MatchRule) -> Self:
"""Build this object with the `match_rule`."""
obj = cls()

Expand Down
5 changes: 3 additions & 2 deletions plugin/rules/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import sublime
from more_itertools import first_true
from typing_extensions import Self

from ..constants import VERSION
from ..snapshot import ViewSnapshot
Expand Down Expand Up @@ -52,7 +53,7 @@ def test(self, view_snapshot: ViewSnapshot, event: ListenerEvent | None = None)
return self.root_rule.test(view_snapshot)

@classmethod
def make(cls, syntax_rule: ST_SyntaxRule) -> SyntaxRule:
def make(cls, syntax_rule: ST_SyntaxRule) -> Self:
"""Build this object with the `syntax_rule`."""
obj = cls()

Expand Down Expand Up @@ -106,7 +107,7 @@ def test(self, view_snapshot: ViewSnapshot, event: ListenerEvent | None = None)
return first_true(self.rules, pred=lambda rule: rule.test(view_snapshot, event))

@classmethod
def make(cls, syntax_rules: Iterable[ST_SyntaxRule]) -> SyntaxRuleCollection:
def make(cls, syntax_rules: Iterable[ST_SyntaxRule]) -> Self:
"""Build this object with the `syntax_rules`."""
obj = cls()
obj.rules = tuple(map(SyntaxRule.make, syntax_rules))
Expand Down
3 changes: 2 additions & 1 deletion plugin/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path

import sublime
from typing_extensions import Self

from .encodings import from_sublime as encoding_from_sublime
from .settings import get_merged_plugin_setting
Expand Down Expand Up @@ -77,7 +78,7 @@ def valid_view(self) -> sublime.View | None:
return self.view if self.view.is_valid() else None

@classmethod
def from_view(cls, view: sublime.View) -> ViewSnapshot:
def from_view(cls, view: sublime.View) -> Self:
"""Create a `ViewSnapshot` object from a `sublime.View` object."""
window = view.window() or sublime.active_window()

Expand Down
3 changes: 2 additions & 1 deletion plugin/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Any, Generic, TypedDict, TypeVar, Union, overload

import sublime
from typing_extensions import Self

SyntaxLike = Union[str, sublime.Syntax]
WindowId = int
Expand Down Expand Up @@ -73,7 +74,7 @@ class ListenerEvent(StrEnum):
UNTRANSIENTIZE = "untransientize"

@classmethod
def from_value(cls, value: Any) -> ListenerEvent | None:
def from_value(cls, value: Any) -> Self | None:
try:
return cls(value)
except ValueError:
Expand Down
4 changes: 3 additions & 1 deletion plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,12 @@ def extract_prefixed_dict(dict_: Mapping[str, _T], *, prefix: str) -> dict[str,
return {k[len(prefix) :]: v for k, v in dict_.items() if k.startswith(prefix)}


def resolve_window(obj: Any) -> sublime.Window | None:
def resolve_window(obj: sublime.Buffer | sublime.View | sublime.Sheet | sublime.Window) -> sublime.Window | None:
if isinstance(obj, sublime.Window):
return obj
window = None
if isinstance(obj, sublime.Buffer):
obj = obj.primary_view()
if isinstance(obj, (sublime.View, sublime.Sheet)):
window = obj.window()
return window
Expand Down

0 comments on commit e9c3b12

Please sign in to comment.