diff --git a/plugin/rules/constraint.py b/plugin/rules/constraint.py index de93e931..106a79eb 100644 --- a/plugin/rules/constraint.py +++ b/plugin/rules/constraint.py @@ -7,19 +7,13 @@ from pathlib import Path from typing import Any, Pattern, TypeVar, final +from more_itertools import first_true + from ..cache import clearable_lru_cache from ..constants import PLUGIN_NAME, ST_PLATFORM from ..snapshot import ViewSnapshot from ..types import Optimizable, ST_ConstraintRule -from ..utils import ( - camel_to_snake, - compile_regex, - first_true, - list_all_subclasses, - merge_regexes, - parse_regex_flags, - remove_suffix, -) +from ..utils import camel_to_snake, compile_regex, list_all_subclasses, merge_regexes, parse_regex_flags, remove_suffix T = TypeVar("T") diff --git a/plugin/rules/constraints/contains.py b/plugin/rules/constraints/contains.py index f0381cdd..fc6449e9 100644 --- a/plugin/rules/constraints/contains.py +++ b/plugin/rules/constraints/contains.py @@ -2,8 +2,10 @@ from typing import Any, final +from more_itertools import nth + from ...snapshot import ViewSnapshot -from ...utils import nth, str_finditer +from ...utils import str_finditer from ..constraint import AbstractConstraint diff --git a/plugin/rules/constraints/contains_regex.py b/plugin/rules/constraints/contains_regex.py index 27663484..7bf73feb 100644 --- a/plugin/rules/constraints/contains_regex.py +++ b/plugin/rules/constraints/contains_regex.py @@ -2,8 +2,9 @@ from typing import Any, final +from more_itertools import nth + from ...snapshot import ViewSnapshot -from ...utils import nth from ..constraint import AbstractConstraint diff --git a/plugin/rules/match.py b/plugin/rules/match.py index 5ef1c735..93b5ed80 100644 --- a/plugin/rules/match.py +++ b/plugin/rules/match.py @@ -5,10 +5,12 @@ from dataclasses import dataclass, field from typing import Any, Union, final +from more_itertools import first_true + from ..cache import clearable_lru_cache from ..snapshot import ViewSnapshot from ..types import Optimizable, ST_MatchRule -from ..utils import camel_to_snake, first_true, list_all_subclasses, remove_suffix +from ..utils import camel_to_snake, list_all_subclasses, remove_suffix from .constraint import ConstraintRule diff --git a/plugin/rules/matches/ratio.py b/plugin/rules/matches/ratio.py index a988ea28..a0f51da0 100644 --- a/plugin/rules/matches/ratio.py +++ b/plugin/rules/matches/ratio.py @@ -2,8 +2,9 @@ from typing import Any, final +from more_itertools import nth + from ...snapshot import ViewSnapshot -from ...utils import nth from ..match import AbstractMatch, MatchableRule diff --git a/plugin/rules/matches/some.py b/plugin/rules/matches/some.py index 22f683d4..32986f36 100644 --- a/plugin/rules/matches/some.py +++ b/plugin/rules/matches/some.py @@ -2,8 +2,9 @@ from typing import Any, final +from more_itertools import nth + from ...snapshot import ViewSnapshot -from ...utils import nth from ..match import AbstractMatch, MatchableRule diff --git a/plugin/rules/syntax.py b/plugin/rules/syntax.py index 4c55cfbd..def61e72 100644 --- a/plugin/rules/syntax.py +++ b/plugin/rules/syntax.py @@ -4,11 +4,12 @@ from dataclasses import dataclass import sublime +from more_itertools import first_true from ..constants import VERSION from ..snapshot import ViewSnapshot from ..types import ListenerEvent, Optimizable, ST_SyntaxRule -from ..utils import find_syntax_by_syntax_likes, first_true +from ..utils import find_syntax_by_syntax_likes from .match import MatchRule diff --git a/plugin/utils.py b/plugin/utils.py index 5c58f2ea..ef5e16be 100644 --- a/plugin/utils.py +++ b/plugin/utils.py @@ -11,18 +11,17 @@ import threading from collections.abc import Generator, Iterable, Mapping from functools import cmp_to_key, lru_cache, reduce, wraps -from itertools import islice from pathlib import Path -from typing import Any, Callable, Pattern, TypeVar, Union, cast, overload +from typing import Any, Callable, Pattern, TypeVar, Union, cast import sublime +from more_itertools import first_true from .cache import clearable_lru_cache from .libs.trie import TrieNode from .types import SyntaxLike _T = TypeVar("_T") -_U = TypeVar("_U") _T_Callable = TypeVar("_T_Callable", bound=Callable[..., Any]) _T_ExpandableVar = TypeVar("_T_ExpandableVar", bound=Union[None, bool, int, float, str, dict, list, tuple]) @@ -147,34 +146,6 @@ def call_function() -> Any: return decorator -@overload -def first_true( - items: Iterable[_T], - default: _U, - pred: Callable[[_T], bool] | None = None, -) -> _T | _U: ... - - -@overload -def first_true( - items: Iterable[_T], - *, - pred: Callable[[_T], bool] | None = None, -) -> _T | None: ... - - -def first_true( - items: Iterable[_T], - default: _U | None = None, - pred: Callable[[_T], bool] | None = None, -) -> _T | _U | None: - """ - Gets the first item which satisfies the `pred`. Otherwise, `default`. - If `pred` is not given or `None`, the first truthy item will be returned. - """ - return next(filter(pred, items), default) - - def list_all_subclasses( root: type[_T], skip_abstract: bool = False, @@ -187,19 +158,6 @@ def list_all_subclasses( yield from list_all_subclasses(leaf, skip_self=False, skip_abstract=skip_abstract) -@overload -def nth(items: Iterable[_T], n: int) -> _T | None: ... - - -@overload -def nth(items: Iterable[_T], n: int, default: _U) -> _T | _U: ... - - -def nth(items: Iterable[_T], n: int, default: _U | None = None) -> _T | _U | None: - """Gets the `n`th item (started from 0th) in `items`. Returns `default` if no such item.""" - return next(islice(iter(items), n, None), default) - - def stable_unique(items: Iterable[_T], *, key: Callable[[_T], Any] | None = None) -> Generator[_T, None, None]: """Lists unique items from the iterable, in their original order.""" key = key or (lambda x: x)