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 Feb 21, 2024
1 parent 367ecaa commit 01fd262
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion plugin/commands/auto_set_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def run_auto_set_syntax_on_view(
# prerequsites
if not (
(window := view.window())
and is_syntaxable_view(view, must_plaintext)
and is_syntaxable_view(view, must_plaintext=must_plaintext)
and (syntax_rule_collection := G.syntax_rule_collections.get(window))
):
return False
Expand Down
31 changes: 19 additions & 12 deletions plugin/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import annotations

from collections import deque

import sublime

from .settings import get_st_setting
from .utils import is_plaintext_syntax, is_transient_view, stable_unique
from .utils import is_plaintext_syntax, is_transient_view


def is_syntaxable_view(view: sublime.View, must_plaintext: bool = False) -> bool:
def is_syntaxable_view(view: sublime.View, *, must_plaintext: bool = False) -> bool:
"""Determinates whether the view is what we want to set a syntax on."""
return bool(
view.is_valid()
Expand All @@ -18,15 +20,20 @@ def is_syntaxable_view(view: sublime.View, must_plaintext: bool = False) -> bool


def resolve_magika_label_with_syntax_map(label: str, syntax_map: dict[str, list[str]]) -> list[str]:
res: list[str] = []
queue: list[str] = syntax_map.get(label, []).copy()

# @todo what if there are circular references?
while queue:
syntax_like = queue.pop()
if syntax_like.startswith("="):
queue.extend(syntax_map.get(syntax_like[1:], []))
# note that dict is insertion-ordered (since Python 3.7)
res: dict[str, bool] = {}

deq = deque(syntax_map.get(label, []))
while deq:
if (notation := deq.popleft()) in res:
continue
res.append(syntax_like)
res[notation] = False # visited

scope, _, ref = notation.partition("=")

if ref:
deq.extendleft(reversed(syntax_map.get(ref, [])))
else:
res[scope] = True # parsed

return list(stable_unique(reversed(res)))
return [scope for scope, is_parsed in res.items() if is_parsed]

0 comments on commit 01fd262

Please sign in to comment.