From b5a9d4f587108578440c4bef9d825634942d0436 Mon Sep 17 00:00:00 2001 From: Janos Wortmann Date: Tue, 8 Aug 2023 08:23:26 +0200 Subject: [PATCH] Add user setting to configure initially folded ranges --- LSP.sublime-settings | 8 ++++++++ plugin/core/types.py | 2 ++ plugin/documents.py | 24 ++++++++++++++++++++++++ sublime-package.json | 23 +++++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/LSP.sublime-settings b/LSP.sublime-settings index 45b2c8280..b5692d58d 100644 --- a/LSP.sublime-settings +++ b/LSP.sublime-settings @@ -212,6 +212,14 @@ // about how to configure your color scheme for semantic highlighting. "semantic_highlighting": false, + // Determines ranges which initially should be folded when a document is opened, + // provided that the language server has support for this. + "initially_folded": [ + // "comment", + // "imports", + // "region", + ], + // --- Debugging ---------------------------------------------------------------------- // Show verbose debug messages in the sublime console. diff --git a/plugin/core/types.py b/plugin/core/types.py index b6ce5ea0b..79c86f3b0 100644 --- a/plugin/core/types.py +++ b/plugin/core/types.py @@ -198,6 +198,7 @@ class Settings: hover_highlight_style = cast(str, None) inhibit_snippet_completions = cast(bool, None) inhibit_word_completions = cast(bool, None) + initially_folded = cast(List[str], None) link_highlight_style = cast(str, None) completion_insert_mode = cast(str, None) log_debug = cast(bool, None) @@ -240,6 +241,7 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None: r("disabled_capabilities", []) r("document_highlight_style", "underline") r("hover_highlight_style", "") + r("initially_folded", []) r("link_highlight_style", "underline") r("log_debug", False) r("log_max_size", 8 * 1024) diff --git a/plugin/documents.py b/plugin/documents.py index b3018b73a..4b20f16b7 100644 --- a/plugin/documents.py +++ b/plugin/documents.py @@ -10,6 +10,8 @@ from .core.protocol import DocumentHighlightKind from .core.protocol import DocumentHighlightParams from .core.protocol import DocumentUri +from .core.protocol import FoldingRange +from .core.protocol import FoldingRangeParams from .core.protocol import Request from .core.protocol import SignatureHelp from .core.protocol import SignatureHelpContext @@ -41,9 +43,11 @@ from .core.views import MarkdownLangMap from .core.views import range_to_region from .core.views import show_lsp_popup +from .core.views import text_document_identifier from .core.views import text_document_position_params from .core.views import update_lsp_popup from .core.windows import WindowManager +from .folding_range import folding_range_to_range from .hover import code_actions_content from .session_buffer import SessionBuffer from .session_view import SessionView @@ -329,6 +333,14 @@ def on_load_async(self) -> None: if not self._registered and is_regular_view(self.view): self._register_async() return + initially_folded_kinds = userprefs().initially_folded + if initially_folded_kinds: + session = self.session_async('foldingRangeProvider') + if session: + params = {'textDocument': text_document_identifier(self.view)} # type: FoldingRangeParams + session.send_request_async( + Request.foldingRange(params, self.view), + partial(self._on_initial_folding_ranges, initially_folded_kinds)) self.on_activated_async() def on_activated_async(self) -> None: @@ -773,6 +785,18 @@ def render_highlights_on_main_thread() -> None: sublime.set_timeout(render_highlights_on_main_thread) + # --- textDocument/foldingRange ------------------------------------------------------------------------------------ + + def _on_initial_folding_ranges(self, kinds: List[str], response: Optional[List[FoldingRange]]) -> None: + if not response: + return + for kind in kinds: + regions = [ + range_to_region(folding_range_to_range(folding_range), self.view) + for folding_range in response if kind == folding_range.get('kind') + ] + self.view.fold(regions) + # --- Public utility methods --------------------------------------------------------------------------------------- def session_async(self, capability: str, point: Optional[int] = None) -> Optional[Session]: diff --git a/sublime-package.json b/sublime-package.json index 00cd93edb..2f3945e46 100644 --- a/sublime-package.json +++ b/sublime-package.json @@ -726,6 +726,29 @@ "default": false, "markdownDescription": "Show inlay hints in the editor. Inlay hints are short annotations within the code, which show variable types or parameter names.\nThis is the default value used for new windows but can be overriden per-window using the `LSP: Toggle Inlay Hints` command from the Command Palette, Main Menu or a custom keybinding." }, + "initially_folded": { + "type": "array", + "items": { + "anyOf": [ + { + "enum": [ + "comment", + "imports", + "region" + ], + "markdownEnumDescriptions": [ + "Block comments", + "Imports or includes", + "Regions (e.g. `#region`)" + ] + }, + { + "type": "string" + } + ] + }, + "markdownDescription": "Determines ranges which initially should be folded when a document is opened, provided that the language server has support for this." + } }, "additionalProperties": false }