Skip to content

Commit

Permalink
fix: syntax.name maybe an empty string
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Oct 31, 2023
1 parent 6ee15c3 commit 3e41019
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
7 changes: 4 additions & 3 deletions plugin/commands/auto_set_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
find_syntax_by_syntax_like,
find_syntax_by_syntax_likes,
first_true,
get_syntax_name,
get_view_by_id,
is_plaintext_syntax,
list_trimmed_filenames,
Expand Down Expand Up @@ -79,7 +80,7 @@ def on_message(self, ws: websocket.WebSocketApp, message: str) -> None:

best_syntax, confidence = resolved_prediction
details = {"event": event, "reason": "predict", "confidence": confidence}
status_message = f'Predicted as "{best_syntax.name}"'
status_message = f'Predicted as "{get_syntax_name(best_syntax)}"'
if confidence >= 0:
status_message += f" ({int(confidence * 100)}% confidence)"

Expand Down Expand Up @@ -433,15 +434,15 @@ def assign_syntax_to_view(
if syntax == (syntax_old := view.syntax() or sublime.Syntax("", "", False, "")):
details["reason"] = f'[ALREADY] {details["reason"]}'
Logger.log(
f'💯 Remain {stringify(_view)} syntax "{syntax.name}" because {stringify(details)}',
f'💯 Remain {stringify(_view)} syntax "{get_syntax_name(syntax)}" because {stringify(details)}',
window=_window,
)
continue

_view.assign_syntax(syntax)
Logger.log(
f"✔ Change {stringify(_view)} syntax"
+ f' from "{syntax_old.name}" to "{syntax.name}" because {stringify(details)}',
+ f' from "{get_syntax_name(syntax_old)}" to "{get_syntax_name(syntax)}" because {stringify(details)}',
window=_window,
)

Expand Down
13 changes: 11 additions & 2 deletions plugin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def find_like(like: SyntaxLike) -> Generator[sublime.Syntax, None, None]:
# by name
yield from sublime.find_syntax_by_name(like)
# by name (case-insensitive)
yield from filter(lambda syntax: like_cf == syntax.name.casefold(), all_syntaxes)
yield from filter(lambda syntax: like_cf == get_syntax_name(syntax).casefold(), all_syntaxes)
# by partial path
yield from filter(lambda syntax: like in syntax.path, all_syntaxes) # type: ignore

Expand Down Expand Up @@ -352,7 +352,7 @@ def head_tail_content_st(view: sublime.View, partial: int) -> str:

def is_plaintext_syntax(syntax: sublime.Syntax) -> bool:
"""Determinates whether the syntax is plain text."""
return syntax.name == "Plain Text"
return get_syntax_name(syntax) == "Plain Text"


def is_transient_view(view: sublime.View) -> bool:
Expand Down Expand Up @@ -470,6 +470,15 @@ def rmtree_ex(path: str | Path, ignore_errors: bool = False, **kwargs: Any) -> N
shutil.rmtree(path, ignore_errors, **kwargs)


def get_syntax_name(syntax: sublime.Syntax) -> str:
"""
Gets syntax name with a workaround.
@see https://github.com/sublimehq/sublime_text/issues/5560
"""
return syntax.name or Path(syntax.path).stem


def stringify(obj: Any) -> str:
"""Custom object-to-string converter. Just used for debug messages."""
if isinstance(obj, sublime.View):
Expand Down

0 comments on commit 3e41019

Please sign in to comment.