diff --git a/boot.py b/boot.py index c5ad39d4..51221efb 100644 --- a/boot.py +++ b/boot.py @@ -1,3 +1,6 @@ +from __future__ import annotations + + def reload_plugin() -> None: import sys diff --git a/plugin/__init__.py b/plugin/__init__.py index 1b33738f..812038f3 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import importlib import importlib.machinery import pkgutil diff --git a/plugin/commands/__init__.py b/plugin/commands/__init__.py index 60b32b1a..b94b837d 100644 --- a/plugin/commands/__init__.py +++ b/plugin/commands/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .auto_set_syntax import AutoSetSyntaxCommand, run_auto_set_syntax_on_view from .auto_set_syntax_create_new_implementation import ( AutoSetSyntaxCreateNewConstraintCommand, diff --git a/plugin/logger.py b/plugin/logger.py index 340ff6f8..f55f5b9b 100644 --- a/plugin/logger.py +++ b/plugin/logger.py @@ -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 @@ -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) diff --git a/plugin/rules/__init__.py b/plugin/rules/__init__.py index adadbed5..43fc3c0c 100644 --- a/plugin/rules/__init__.py +++ b/plugin/rules/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .constraint import AbstractConstraint, ConstraintRule, find_constraint, get_constraints from .constraints import * # noqa: F401, F403 from .match import AbstractMatch, MatchableRule, MatchRule, find_match, get_matches diff --git a/plugin/rules/constraint.py b/plugin/rules/constraint.py index 106a79eb..1595a0a9 100644 --- a/plugin/rules/constraint.py +++ b/plugin/rules/constraint.py @@ -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 @@ -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() diff --git a/plugin/rules/constraints/__init__.py b/plugin/rules/constraints/__init__.py index f132852b..1e679007 100644 --- a/plugin/rules/constraints/__init__.py +++ b/plugin/rules/constraints/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .contains import ContainsConstraint from .contains_regex import ContainsRegexConstraint from .first_line_contains import FirstLineContainsConstraint diff --git a/plugin/rules/match.py b/plugin/rules/match.py index 93b5ed80..a6669b0a 100644 --- a/plugin/rules/match.py +++ b/plugin/rules/match.py @@ -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 @@ -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() diff --git a/plugin/rules/matches/__init__.py b/plugin/rules/matches/__init__.py index 1961922c..8d101c63 100644 --- a/plugin/rules/matches/__init__.py +++ b/plugin/rules/matches/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .all import AllMatch from .any import AnyMatch from .ratio import RatioMatch diff --git a/plugin/rules/syntax.py b/plugin/rules/syntax.py index def61e72..4d7b831c 100644 --- a/plugin/rules/syntax.py +++ b/plugin/rules/syntax.py @@ -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 @@ -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() @@ -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)) diff --git a/plugin/snapshot.py b/plugin/snapshot.py index d7a880d8..ec367450 100644 --- a/plugin/snapshot.py +++ b/plugin/snapshot.py @@ -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 @@ -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() diff --git a/plugin/types.py b/plugin/types.py index 7c3c9e2d..be1a5feb 100644 --- a/plugin/types.py +++ b/plugin/types.py @@ -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 @@ -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: diff --git a/plugin/utils.py b/plugin/utils.py index ef5e16be..830f1abe 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -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