From 1368fc2878270b568002ad0e1e54d10a6cd25e9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=80=D0=B5=D0=B4=D1=80=D0=B0=D0=B3=20=D0=9D=D0=B8?= =?UTF-8?q?=D0=BA=D0=BE=D0=BB=D0=B8=D1=9B?= Date: Tue, 12 Dec 2023 20:56:01 +0100 Subject: [PATCH] fix https://github.com/predragnikolic/InlineFold/issues/9 Although I believe that this solution is not optimal, I specified fold regions in a large package-lock json file, and I didn't see any performance issues. --- main.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/main.py b/main.py index a5ebfc2..601e760 100644 --- a/main.py +++ b/main.py @@ -110,6 +110,27 @@ def fold(view: sublime.View, fold_r: sublime.Region, preceding_text: Optional[st view.fold(fold_r) +class FixFoldsWhenFormattingListener(sublime_plugin.TextChangeListener): + # Attempt to fix https://github.com/predragnikolic/InlineFold/issues/9 + # This code here will try to detect if multiple lines were formatted + # and if so, it will retrigger the inline_fold_all. + def on_text_changed(self, changes: List[sublime.TextChange]): + if not self.buffer: + return + view = self.buffer.primary_view() + if not view: + return + for c in changes: + is_editing_multiple_lines = '\n' in c.str + if is_editing_multiple_lines: + def retrigger_fold(): + # first unfold all - this might lead to unwanted behavoir + [view.unfold(r) for r in view.folded_regions()] + view.run_command('inline_fold_all') + sublime.set_timeout(retrigger_fold, 0) + break + + def first_selection_region(view: sublime.View) -> Optional[sublime.Region]: try: return view.sel()[0]