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 Mar 28, 2024
1 parent 54eaa22 commit e7075eb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 31 deletions.
1 change: 1 addition & 0 deletions plugin/commands/auto_set_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def assign_syntax_to_view(
continue

_view.assign_syntax(syntax)
_view.settings().set(VIEW_KEY_IS_ASSIGNED, True)
Logger.log(
f"✔ Change {stringify(_view)} syntax"
+ f' from "{get_syntax_name(syntax_old)}" to "{get_syntax_name(syntax)}" because {stringify(details)}',
Expand Down
4 changes: 2 additions & 2 deletions plugin/commands/auto_set_syntax_create_new_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import sublime
import sublime_plugin

from ..constants import PLUGIN_CUSTOM_DIR, PLUGIN_CUSTOM_MODULE_PATHS, PLUGIN_NAME
from ..constants import PLUGIN_CUSTOM_DIR, PLUGIN_CUSTOM_MODULE_PATHS, PLUGIN_NAME, VIEW_KEY_IS_CREATED
from ..types import SyntaxLike
from ..utils import find_syntax_by_syntax_like

Expand Down Expand Up @@ -64,7 +64,7 @@ def _clone_file_as_template(
new.run_command("append", {"characters": template})
new.settings().update({
"default_dir": save_dir,
"is_auto_set_syntax_template_buffer": True,
VIEW_KEY_IS_CREATED: True,
})

if syntax and (syntax := find_syntax_by_syntax_like(syntax)):
Expand Down
6 changes: 3 additions & 3 deletions plugin/commands/auto_set_syntax_debug_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sublime
import sublime_plugin

from ..constants import PLUGIN_NAME, PY_VERSION, ST_CHANNEL, ST_PLATFORM_ARCH, ST_VERSION, VERSION
from ..constants import PLUGIN_NAME, PY_VERSION, ST_CHANNEL, ST_PLATFORM_ARCH, ST_VERSION, VERSION, VIEW_KEY_IS_CREATED
from ..rules.constraint import get_constraints
from ..rules.match import get_matches
from ..settings import get_merged_plugin_settings
Expand All @@ -14,7 +14,7 @@

TEMPLATE = f"""
# === {PLUGIN_NAME} Debug Information === #
# You may use the following website to format this debug information.
# You may use the following website to beautify this debug information.
# @link https://play.ruff.rs/?secondary=Format
###############
Expand Down Expand Up @@ -78,7 +78,7 @@ def run(self, *, copy_only: bool = False) -> None:
view.set_scratch(True)
view.run_command("append", {"characters": content})
view.settings().update({
"is_auto_set_syntax_template_buffer": True,
VIEW_KEY_IS_CREATED: True,
})

if syntax := find_syntax_by_syntax_like("scope:source.python"):
Expand Down
12 changes: 6 additions & 6 deletions plugin/commands/auto_set_syntax_download_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def tar_safe_extract(


def simple_urlopen(url: str, *, chunk_size: int = 512 * 1024) -> bytes:
response = urllib.request.urlopen(url)
data = b""
while chunk := response.read(chunk_size):
data += chunk
if response.info().get("Content-Encoding") == "gzip":
data = gzip.decompress(data)
with urllib.request.urlopen(url) as resp:
data = b""
while chunk := resp.read(chunk_size):
data += chunk
if resp.info().get("Content-Encoding") == "gzip":
data = gzip.decompress(data)
return data


Expand Down
14 changes: 6 additions & 8 deletions plugin/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@

################################################################################

VIEW_RUN_ID_SETTINGS_KEY = f"{PLUGIN_NAME}/run_id"
VIEW_IS_TRANSIENT_SETTINGS_KEY = f"{PLUGIN_NAME}/is_transient"
VIEW_KEY_IS_CREATED = f"{PLUGIN_NAME}/is_created"
"""This view setting indicates that this view is created by AutoSetSyntax."""
VIEW_KEY_IS_ASSIGNED = f"{PLUGIN_NAME}/is_assigned"
"""This view setting indicates that the syntax of this view is assigned by AutoSetSyntax."""
VIEW_KEY_IS_TRANSIENT = f"{PLUGIN_NAME}/is_transient"
"""This view setting is just a temporary flag during running AutoSetSyntax on a transient view."""

################################################################################

Expand All @@ -54,9 +58,3 @@
ref="dependencies",
file=PLUGIN_PY_LIBS_ZIP_NAME,
)

GUESSLANG_SERVER_TAG = "server-0.1.7"
GUESSLANG_SERVER_URL = "https://github.com/{repo}/archive/{ref}.zip".format(
repo="jfcherng-sublime/ST-AutoSetSyntax",
ref=f"refs/tags/{GUESSLANG_SERVER_TAG}",
)
16 changes: 4 additions & 12 deletions plugin/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@
import sublime_plugin

from .commands.auto_set_syntax import run_auto_set_syntax_on_view
from .constants import (
PLUGIN_NAME,
PY_VERSION,
ST_CHANNEL,
ST_PLATFORM_ARCH,
ST_VERSION,
VERSION,
VIEW_IS_TRANSIENT_SETTINGS_KEY,
)
from .constants import PLUGIN_NAME, PY_VERSION, ST_CHANNEL, ST_PLATFORM_ARCH, ST_VERSION, VERSION, VIEW_KEY_IS_TRANSIENT
from .helpers import is_syntaxable_view
from .logger import Logger
from .rules import SyntaxRuleCollection, get_constraints, get_matches
Expand Down Expand Up @@ -121,7 +113,7 @@ def on_init(self, views: list[sublime.View]) -> None:
G.startup_views |= set(views)

def on_load(self, view: sublime.View) -> None:
view.settings().set(VIEW_IS_TRANSIENT_SETTINGS_KEY, is_transient_view(view))
view.settings().set(VIEW_KEY_IS_TRANSIENT, is_transient_view(view))
run_auto_set_syntax_on_view(view, ListenerEvent.LOAD)

def on_load_project(self, window: sublime.Window) -> None:
Expand Down Expand Up @@ -174,7 +166,7 @@ def _try_assign_syntax_when_text_changed(view: sublime.View, changes: Sequence[s


def _try_assign_syntax_when_view_untransientize(view: sublime.View) -> bool:
if (settings := view.settings()).get(VIEW_IS_TRANSIENT_SETTINGS_KEY):
settings.erase(VIEW_IS_TRANSIENT_SETTINGS_KEY)
if (settings := view.settings()).get(VIEW_KEY_IS_TRANSIENT):
settings.erase(VIEW_KEY_IS_TRANSIENT)
return run_auto_set_syntax_on_view(view, ListenerEvent.UNTRANSIENTIZE)
return False

0 comments on commit e7075eb

Please sign in to comment.