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 4, 2024
1 parent 515d27d commit 31c9eb1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion plugin/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

POINT = int
TUPLE_REGION = Tuple[POINT, POINT]
INDENT_LEVEL = int

if sys.version_info >= (3, 11):
from enum import StrEnum
Expand Down Expand Up @@ -41,7 +42,7 @@ def indent_length(self) -> int:

@cached_property
def indent_pattern(self) -> str:
return rf"^({self.indent_chars})+"
return rf"^(?:{self.indent_chars})+"

@cached_property
def indent_pattern_compiled(self) -> Pattern[str]:
Expand Down
2 changes: 1 addition & 1 deletion plugin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def is_renderable_view(view: sublime.View) -> bool:
# activation by plugin's logics
return bool(
view.is_valid()
and view.element() is None
and not view.element()
and not is_transient_view(view)
and (syntax := view.syntax())
and sublime.score_selector(syntax.scope, get_enabled_selector()) > 0
Expand Down
19 changes: 19 additions & 0 deletions plugin/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from .constants import PLUGIN_NAME


def log_debug(message: str) -> None:
print(f"[{PLUGIN_NAME}][DEBUG] {message}")


def log_info(message: str) -> None:
print(f"[{PLUGIN_NAME}][INFO] {message}")


def log_warning(message: str) -> None:
print(f"[{PLUGIN_NAME}][WARNING] {message}")


def log_error(message: str) -> None:
print(f"[{PLUGIN_NAME}][ERROR] {message}")
11 changes: 8 additions & 3 deletions plugin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import sublime

from plugin.log import log_warning

from .constants import LEVEL_COLORS_FALLBACK, PLUGIN_NAME
from .data_types import LevelStyle
from .utils import debounce
Expand Down Expand Up @@ -31,8 +33,6 @@ def get_plugin_setting(key: str) -> Any: ...
def get_plugin_setting(key: str, default: None) -> Any: ...
@overload
def get_plugin_setting(key: str, default: _T) -> _T: ...


def get_plugin_setting(key: str, default: Any = None) -> Any:
return get_plugin_settings().get(key, default)

Expand All @@ -54,7 +54,12 @@ def get_level_colors() -> list[str]:


def get_level_style() -> LevelStyle:
return LevelStyle(get_plugin_setting("level_style", "block"))
style = get_plugin_setting("level_style", "block")
try:
return LevelStyle(style)
except ValueError:
log_warning(f'Invalid "level_style" setting: {style}')
return LevelStyle.BLOCK


def get_file_size_limit() -> int:
Expand Down
14 changes: 7 additions & 7 deletions plugin/view_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sublime

from .data_types import IndentInfo, LevelStyle
from .data_types import INDENT_LEVEL, IndentInfo, LevelStyle
from .helpers import get_regions_key
from .indent_renderer import AbstractIndentRenderer, find_indent_renderer
from .settings import get_level_colors, get_level_style
Expand All @@ -17,9 +17,9 @@ def calcualte_level_regions(
*,
indent_info: IndentInfo | None = None,
regions: Sequence[sublime.Region] | None = None,
) -> defaultdict[int, list[sublime.Region]]:
) -> defaultdict[INDENT_LEVEL, list[sublime.Region]]:
"""
Calculates the begin point of indents for each level.
Calculates regions of indents for each level.
:param view: The view.
:param indent_info: The indent information.
Expand All @@ -28,18 +28,18 @@ def calcualte_level_regions(
They should be whole lines, non-overlapping and sorted in ascending order beforehand.
If `None`, the whole region of the `view` will be used.
:returns: A dictionary whose keys are the indent level and values are regions of level indents.
:returns: A dictionary whose keys are the indent level (started from `0`) and values are regions of level indents.
"""
if indent_info is None:
indent_info = IndentInfo.from_view(view)
if regions is None:
regions = (sublime.Region(0, view.size()),)

whole_content = view.substr(sublime.Region(0, view.size()))
level_regions: defaultdict[int, list[sublime.Region]] = defaultdict(list)
level_regions: defaultdict[INDENT_LEVEL, list[sublime.Region]] = defaultdict(list)
for region in regions:
for m in indent_info.indent_pattern_compiled.finditer(whole_content, region.begin(), region.end()):
for level, level_pt in enumerate(range(m.start(), m.end(), indent_info.indent_length)):
for m in indent_info.indent_pattern_compiled.finditer(whole_content, *region.to_tuple()):
for level, level_pt in enumerate(range(*m.span(), indent_info.indent_length)):
level_regions[level].append(sublime.Region(level_pt, level_pt + indent_info.indent_length))
return level_regions

Expand Down

0 comments on commit 31c9eb1

Please sign in to comment.