Skip to content

Commit

Permalink
Add user setting to configure initially folded ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Aug 8, 2023
1 parent 15a0e0c commit b5a9d4f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions LSP.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down
23 changes: 23 additions & 0 deletions sublime-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit b5a9d4f

Please sign in to comment.