Skip to content

Commit

Permalink
feat: new command: auto_set_syntax_syntax_rules_summary_command
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Nov 2, 2024
1 parent 951d8f9 commit 9d2a7ff
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion AutoSetSyntax.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"rules": [
{
"constraint": "first_line_contains_regex",
"args": ["^# === AutoSetSyntax Debug Information === #"]
"args": ["^# === [AutoSetSyntax] "]
}
]
}
Expand Down
11 changes: 10 additions & 1 deletion docs/src/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ Just run `AutoSetSyntax: Debug Information` from the command palette[^1].

!!! tip

The debug information is designed to be Python-compatible, thus you can format it
The output is designed to be Python-compatible, thus you can format it
with a Python formatter like [Ruff][ruff-formatter-online].

## Syntax Rules Summary

Run `AutoSetSyntax: Syntax Rules Summary` from the command palette[^1].

!!! tip

The output is designed to be Python-compatible, thus you can format it
with a Python formatter like [Ruff][ruff-formatter-online].

[^1]: Command palette: ++ctrl+p++ for Windows/Linux. ++cmd+p++ for macOS.
4 changes: 4 additions & 0 deletions menus/Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"caption": "AutoSetSyntax: Debug Information",
"command": "auto_set_syntax_debug_information",
},
{
"caption": "AutoSetSyntax: Syntax Rules Summary",
"command": "auto_set_syntax_syntax_rules_summary",
},
{
"caption": "AutoSetSyntax: Document (Online)",
"command": "open_url",
Expand Down
2 changes: 2 additions & 0 deletions plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
AutoSetSyntaxCreateNewMatchCommand,
AutoSetSyntaxDebugInformationCommand,
AutoSetSyntaxDownloadDependenciesCommand,
AutoSetSyntaxSyntaxRulesSummaryCommand,
run_auto_set_syntax_on_view,
)
from .constants import PLUGIN_CUSTOM_MODULE_PATHS, PLUGIN_NAME, PLUGIN_PY_LIBS_DIR
Expand Down Expand Up @@ -52,6 +53,7 @@
"AutoSetSyntaxCreateNewMatchCommand",
"AutoSetSyntaxDebugInformationCommand",
"AutoSetSyntaxDownloadDependenciesCommand",
"AutoSetSyntaxSyntaxRulesSummaryCommand",
# ST: listeners
"AioSettings",
"AutoSetSyntaxEventListener",
Expand Down
2 changes: 2 additions & 0 deletions plugin/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from .auto_set_syntax_debug_information import AutoSetSyntaxDebugInformationCommand
from .auto_set_syntax_download_dependencies import AutoSetSyntaxDownloadDependenciesCommand
from .auto_set_syntax_syntax_rules_summary import AutoSetSyntaxSyntaxRulesSummaryCommand

__all__ = (
# ST: commands
Expand All @@ -15,6 +16,7 @@
"AutoSetSyntaxCreateNewMatchCommand",
"AutoSetSyntaxDebugInformationCommand",
"AutoSetSyntaxDownloadDependenciesCommand",
"AutoSetSyntaxSyntaxRulesSummaryCommand",
# ...
"run_auto_set_syntax_on_view",
)
10 changes: 5 additions & 5 deletions plugin/commands/auto_set_syntax_debug_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..utils import find_syntax_by_syntax_like, get_fqcn, stringify

TEMPLATE = f"""
# === {PLUGIN_NAME} Debug Information === #
# === [{PLUGIN_NAME}] Debug Information === #
# You may use the following website to beautify this debug information.
# @link https://play.ruff.rs/?secondary=Format
Expand Down Expand Up @@ -70,16 +70,16 @@ def run(self, *, copy_only: bool = False) -> None:

if copy_only:
sublime.set_clipboard(content)
sublime.message_dialog(f"{PLUGIN_NAME} debug information has been copied to the clipboard.")
sublime.message_dialog(f"[{PLUGIN_NAME}] The result has been copied to the clipboard.")
return

view = self.window.new_file()
view.set_name(f"{PLUGIN_NAME} Debug Information")
view.set_name(self.description())
view.set_scratch(True)
view.run_command("append", {"characters": content})
view.settings().update({
VIEW_KEY_IS_CREATED: True,
})

if syntax := find_syntax_by_syntax_like("scope:source.python"):
view.assign_syntax(syntax)
if syntax_ := find_syntax_by_syntax_like("scope:source.python"):
view.assign_syntax(syntax_)
61 changes: 61 additions & 0 deletions plugin/commands/auto_set_syntax_syntax_rules_summary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import annotations

from collections import defaultdict

import sublime
import sublime_plugin

from ..constants import PLUGIN_NAME, VIEW_KEY_IS_CREATED
from ..rules import SyntaxRule
from ..shared import G
from ..utils import find_syntax_by_syntax_like, stringify

TEMPLATE = f"""
# === [{PLUGIN_NAME}] Syntax Rules Summary === #
# You may use the following website to beautify this debug information.
# @link https://play.ruff.rs/?secondary=Format
########################
# Syntax Rules Summary #
########################
{{content}}
""".lstrip()


class AutoSetSyntaxSyntaxRulesSummaryCommand(sublime_plugin.WindowCommand):
def description(self) -> str:
return f"{PLUGIN_NAME}: Syntax Rules Summary"

def run(self, *, copy_only: bool = False) -> None:
if not (rule_collection := G.syntax_rule_collections.get(self.window)):
return

summary: defaultdict[sublime.Syntax, list[SyntaxRule]] = defaultdict(list)
for rule in rule_collection.rules:
if rule.syntax:
summary[rule.syntax].append(rule)

content = ""
for syntax_, rules in sorted(summary.items(), key=lambda x: x[0].name.casefold()):
content += f"# Syntax: {syntax_.name}\n"
for rule in rules:
content += f"{stringify(rule)}\n"
content += "\n"
content = TEMPLATE.format(content=content)

if copy_only:
sublime.set_clipboard(content)
sublime.message_dialog(f"[{PLUGIN_NAME}] The result has been copied to the clipboard.")
return

view = self.window.new_file()
view.set_name(self.description())
view.set_scratch(True)
view.run_command("append", {"characters": content})
view.settings().update({
VIEW_KEY_IS_CREATED: True,
})

if syntax := find_syntax_by_syntax_like("scope:source.python"):
view.assign_syntax(syntax)

0 comments on commit 9d2a7ff

Please sign in to comment.