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 Jan 18, 2025
1 parent 930bb0c commit abe162b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 25 deletions.
10 changes: 4 additions & 6 deletions plugin/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ def indent_pattern_compiled(self) -> Pattern[str]:
@classmethod
def from_view(cls, view: sublime.View) -> Self:
settings = view.settings()
tab_size = int(settings.get("tab_size", 4))
if settings.get("translate_tabs_to_spaces", False):
style = IndentStyle.SPACE
else:
style = IndentStyle.TAB
return cls(tab_size=tab_size, style=style)
return cls(
tab_size=int(settings.get("tab_size", 4)),
style=IndentStyle.SPACE if settings.get("translate_tabs_to_spaces", False) else IndentStyle.TAB,
)


class IndentStyle(StrEnum):
Expand Down
3 changes: 2 additions & 1 deletion plugin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sublime

from .constants import PLUGIN_NAME, VIEW_KEY_USER_DISABLED
from .data_types import INDENT_LEVEL
from .settings import get_enabled_selector, get_file_size_limit


Expand All @@ -25,5 +26,5 @@ def is_transient_view(view: sublime.View) -> bool:
return bool((sheet := view.sheet()) and sheet.is_transient())


def get_regions_key(level: int) -> str:
def get_regions_key(level: INDENT_LEVEL) -> str:
return f"{PLUGIN_NAME}:level@{level}"
8 changes: 4 additions & 4 deletions plugin/indent_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sublime
from more_itertools import first_true

from .data_types import LevelStyle
from .data_types import INDENT_LEVEL, LevelStyle
from .helpers import get_regions_key
from .utils import camel_to_snake, get_circular_nth, list_all_subclasses, remove_suffix

Expand Down Expand Up @@ -45,7 +45,7 @@ def render(
self,
*,
level_colors: Sequence[str],
level_regions: Mapping[int, Sequence[sublime.Region]],
level_regions: Mapping[INDENT_LEVEL, Sequence[sublime.Region]],
) -> None:
"""Render the view based on the points of each level."""

Expand All @@ -61,7 +61,7 @@ def render(
self,
*,
level_colors: Sequence[str],
level_regions: Mapping[int, Sequence[sublime.Region]],
level_regions: Mapping[INDENT_LEVEL, Sequence[sublime.Region]],
) -> None:
for level, regions in level_regions.items():
self.view.add_regions(
Expand All @@ -83,7 +83,7 @@ def render(
self,
*,
level_colors: Sequence[str],
level_regions: Mapping[int, Sequence[sublime.Region]],
level_regions: Mapping[INDENT_LEVEL, Sequence[sublime.Region]],
) -> None:
for level, regions in level_regions.items():
self.view.add_regions(
Expand Down
2 changes: 1 addition & 1 deletion plugin/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@debounce_by_settings
def refresh_rendering(view: sublime.View) -> None:
vm = ViewManager.get_instance(view)
vm = ViewManager(view)
if is_renderable_view(view):
vm.render_view()
else:
Expand Down
5 changes: 3 additions & 2 deletions plugin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ def get_level_colors() -> list[str]:


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


Expand Down
26 changes: 15 additions & 11 deletions plugin/view_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Iterable, Sequence

import sublime
from typing_extensions import Self

from .data_types import INDENT_LEVEL, IndentInfo, LevelStyle
from .helpers import get_regions_key
Expand Down Expand Up @@ -45,25 +46,28 @@ def calcualte_level_regions(


class ViewManager:
__instances: weakref.WeakKeyDictionary[sublime.View, ViewManager] = weakref.WeakKeyDictionary()
__instances: weakref.WeakKeyDictionary[sublime.View, Self] = weakref.WeakKeyDictionary()
"""A map which maps managed `view` to its manager."""

def __init__(self, view: sublime.View, *, _from_init: bool = True) -> None:
if _from_init:
raise ValueError("Use `get_instance()` instead.")
# singleton pattern
def __new__(cls, view: sublime.View) -> Self:
if view not in cls.__instances:
instance = super().__new__(cls)
instance.__initialized = False
cls.__instances[view] = instance
return cls.__instances[view]

def __init__(self, view: sublime.View) -> None:
self.__initialized: bool
if self.__initialized:
return
self.__initialized = True

self.view = view
"""The managed `view`."""
self.max_level = -1
"""The max indent level of the managed `view`."""

@classmethod
def get_instance(cls, view: sublime.View) -> ViewManager:
"""Gets the manager instance of `view`. (per-view singleton pattern)"""
if view not in cls.__instances:
cls.__instances[view] = cls(view, _from_init=False)
return cls.__instances[view]

@classmethod
def clear_all_views(cls, views: Iterable[sublime.View] | None = None) -> None:
"""Clears managed `views`. If `views` is `None`, clear all managed `views`."""
Expand Down

0 comments on commit abe162b

Please sign in to comment.