Skip to content

Commit

Permalink
refactor: tidy codes
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Jun 13, 2024
1 parent 09067cb commit 927a6ab
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@


def plugin_loaded() -> None:
"""Executed when this plugin is loaded."""
# somehow "AutoSetSyntaxAppendLogCommand" won't be ready if we don't wait a bit
sublime.set_timeout(_plugin_loaded)

Expand All @@ -86,6 +87,7 @@ def _plugin_loaded() -> None:


def plugin_unloaded() -> None:
"""Executed when this plugin is unloaded."""
AioSettings.clear_on_change(PLUGIN_NAME)
AioSettings.tear_down()

Expand Down
12 changes: 10 additions & 2 deletions plugin/rules/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
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, list_all_subclasses, merge_regexes, parse_regex_flags, remove_suffix
from ..utils import (
camel_to_snake,
compile_regex,
drop_falsy,
list_all_subclasses,
merge_regexes,
parse_regex_flags,
remove_suffix,
)

T = TypeVar("T")

Expand Down Expand Up @@ -116,7 +124,7 @@ def test(self, view_snapshot: ViewSnapshot) -> bool:
@final
def _handled_args(self, normalizer: Callable[[T], T] | None = None) -> tuple[T, ...]:
"""Filter falsy args and normalize them. Note that `0`, `""` and `None` are falsy."""
args: Iterable[T] = filter(None, self.args)
args: Iterable[T] = drop_falsy(self.args)
if normalizer:
args = map(normalizer, args)
return tuple(args)
Expand Down
4 changes: 2 additions & 2 deletions plugin/rules/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..constants import VERSION
from ..snapshot import ViewSnapshot
from ..types import ListenerEvent, Optimizable, ST_SyntaxRule
from ..utils import find_syntax_by_syntax_likes
from ..utils import drop_falsy, find_syntax_by_syntax_likes
from .match import MatchRule


Expand Down Expand Up @@ -74,7 +74,7 @@ def make(cls, syntax_rule: ST_SyntaxRule) -> Self:
if (on_events := syntax_rule.get("on_events")) is not None:
if isinstance(on_events, str):
on_events = [on_events]
obj.on_events = set(filter(None, map(ListenerEvent.from_value, on_events)))
obj.on_events = set(drop_falsy(map(ListenerEvent.from_value, on_events)))

if match_rule_compiled := MatchRule.make(syntax_rule):
obj.root_rule = match_rule_compiled
Expand Down
4 changes: 2 additions & 2 deletions plugin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from more_itertools import unique_everseen

from .types import ST_SyntaxRule
from .utils import drop_falsy


def get_merged_plugin_setting(
Expand Down Expand Up @@ -52,8 +53,7 @@ def extra_settings_producer(settings: MergedSettingsDict) -> dict[str, Any]:

# use tuple to freeze setting for better performance (cache-able)
ret["trim_suffixes"] = tuple(
filter(
None, # remove falsy values
drop_falsy(
unique_everseen(
chain(
settings.get("project_trim_suffixes", []),
Expand Down
5 changes: 5 additions & 0 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ def compile_regex(regex: str | Pattern[str], flags: int = 0) -> Pattern[str]:
return re.compile(regex, flags)


def drop_falsy(iterable: Iterable[_T | None]) -> Generator[_T, None, None]:
"""Drops falsy values from the iterable."""
yield from filter(None, iterable)


def get_fqcn(obj: Any) -> str:
if obj is None:
return "None"
Expand Down

0 comments on commit 927a6ab

Please sign in to comment.