Skip to content

Commit

Permalink
fix: make add_regions for TodoReview view persistent
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Cherng <[email protected]>
  • Loading branch information
jfcherng committed Apr 9, 2024
1 parent 8ad6a16 commit 01e8091
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions plugin/TodoReview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import re
import threading
import timeit
from typing import Any, Callable, Dict, Generator, Iterable, Iterator
from typing import Any, Callable, Dict, Generator, Iterable, Iterator, TypeVar

import sublime
import sublime_plugin

assert __package__

_T = TypeVar("_T", bound=Callable[..., Any])

RESULT = Dict[str, Any]

PACKAGE_NAME = __package__.partition(".")[0]
Expand Down Expand Up @@ -53,6 +55,18 @@ def merge_regexes(regexes: Iterable[str]) -> str:
return "" if merged == "(?:)" else merged


def writable_view(func: _T) -> _T:
"""A decorator for `sublime_plugin.TextCommand` methods to make a view writable during its execution."""

def wrapper(self: sublime_plugin.TextCommand, *args, **kwargs) -> Any:
self.view.set_read_only(False)
result = func(self, *args, **kwargs)
self.view.set_read_only(True)
return result

return wrapper # type: ignore


class Settings:
def __init__(self, view: sublime.View, args: dict[str, Any]):
self.user = sublime.load_settings("TodoReview.sublime-settings")
Expand Down Expand Up @@ -240,6 +254,7 @@ def get_or_create_view(self) -> sublime.View:
view.set_name("TodoReview")
view.assign_syntax(TODO_SYNTAX_FILE)
view.set_scratch(True)
view.set_read_only(True)
view.settings().update({
"todo_results": True,
"line_padding_bottom": 2,
Expand Down Expand Up @@ -290,6 +305,7 @@ def sort(self) -> Iterator[tuple[str, Iterator[RESULT]]]:

return itertools.groupby(results, key=lambda m: m["patt"])

@writable_view
def draw_header(self) -> None:
assert settings

Expand All @@ -307,6 +323,7 @@ def draw_header(self) -> None:
res += "\n"
self.view.insert(self.edit, len(self.view), res)

@writable_view
def draw_results(self) -> None:
data: tuple[list[sublime.Region], list[RESULT]] = ([], [])
for patt, _items in self.sorted:
Expand All @@ -325,7 +342,7 @@ def draw_results(self) -> None:
region = sublime.Region(start, len(self.view))
data[0].append(region)
data[1].append(item)
self.view.add_regions("results", data[0], "")
self.view.add_regions("results", data[0], "", flags=sublime.PERSISTENT)
d = dict((f"{k.a},{k.b}", v) for k, v in zip(data[0], data[1]))
self.view.settings().set("review_results", d)

Expand Down Expand Up @@ -400,6 +417,12 @@ def run(self, edit: sublime.Edit, **args: Any) -> None:
target = results[sel]
self.settings.set("selected_result", sel)
region = target.cover(target)
self.view.add_regions("selection", [region], "todo-list.selected", "circle", sublime.DRAW_NO_FILL)
self.view.add_regions(
"selection",
[region],
"todo-list.selected",
"circle",
flags=sublime.DRAW_NO_FILL | sublime.PERSISTENT,
)
self.view.show(sublime.Region(region.a, region.a + 5))
return

0 comments on commit 01e8091

Please sign in to comment.