Skip to content

Commit

Permalink
Add support for folding range request (#2304)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann authored Aug 15, 2023
1 parent eac7700 commit 032e3fd
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,9 @@
"caption": "LSP: Toggle Inlay Hints",
"command": "lsp_toggle_inlay_hints",
},
// {
// "caption": "LSP: Fold All Comment Blocks",
// "command": "lsp_fold_all",
// "args": {"kind": "comment"}
// },
]
9 changes: 9 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
// "command": "lsp_expand_selection",
// "context": [{"key": "lsp.session_with_capability", "operand": "selectionRangeProvider"}]
// },
// Fold around caret position - an optional "strict" argument can be used to configure whether
// to fold only when the caret is contained within the folded region (true), or even when it is
// anywhere on the starting line (false).
// {
// "keys": ["UNBOUND"],
// "command": "lsp_fold",
// "args": {"strict": true},
// "context": [{"key": "lsp.session_with_capability", "operand": "foldingRangeProvider"}]
// },
//==== Internal key-bindings ====
{
"keys": ["<character>"],
Expand Down
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
17 changes: 17 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
{
"id": "edit",
"children": [
{
"id": "fold",
"children": [
{
"id": "lsp",
"caption": "-"
},
{
"command": "lsp_fold",
"args": {"prefetch": true}
}
]
},
{
"id": "lsp",
"caption": "-"
},
{
"command": "lsp_fold",
"args": {"prefetch": true, "hidden": true}
},
{
"command": "lsp_source_action",
"args": {"id": -1}
Expand Down
2 changes: 2 additions & 0 deletions boot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
from .plugin.documents import TextChangeListener
from .plugin.edit import LspApplyDocumentEditCommand
from .plugin.execute_command import LspExecuteCommand
from .plugin.folding_range import LspFoldAllCommand
from .plugin.folding_range import LspFoldCommand
from .plugin.formatting import LspFormatCommand
from .plugin.formatting import LspFormatDocumentCommand
from .plugin.formatting import LspFormatDocumentRangeCommand
Expand Down
2 changes: 2 additions & 0 deletions docs/src/keyboard_shortcuts.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Refer to the [Customization section](customization.md#keyboard-shortcuts-key-bin
| Auto Complete | <kbd>ctrl</kbd> <kbd>space</kbd> (also on macOS) | `auto_complete`
| Expand Selection | unbound | `lsp_expand_selection`
| Find References | <kbd>shift</kbd> <kbd>f12</kbd> | `lsp_symbol_references` (supports optional args: `{"include_declaration": true/false}`)
| Fold | unbound | `lsp_fold` (supports optional args: `{"strict": true/false}` - to configure whether to fold only when the caret is contained within the folded region (`true`), or even when it is anywhere on the starting line (`false`))
| Fold All | unbound | `lsp_fold_all` (supports optional args: `{"kind": "comment" | "imports" | "region"}`)
| Follow Link | unbound | `lsp_open_link`
| Format File | unbound | `lsp_format_document`
| Format Selection | unbound | `lsp_format_document_range`
Expand Down
4 changes: 4 additions & 0 deletions plugin/core/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -6136,6 +6136,10 @@ def prepareRename(cls, params: PrepareRenameParams, view: sublime.View, progress
def selectionRange(cls, params: SelectionRangeParams) -> 'Request':
return Request('textDocument/selectionRange', params)

@classmethod
def foldingRange(cls, params: FoldingRangeParams, view: sublime.View) -> 'Request':
return Request('textDocument/foldingRange', params, view)

@classmethod
def workspaceSymbol(cls, params: WorkspaceSymbolParams) -> 'Request':
return Request("workspace/symbol", params, None, progress=True)
Expand Down
11 changes: 11 additions & 0 deletions plugin/core/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .protocol import ExecuteCommandParams
from .protocol import FailureHandlingKind
from .protocol import FileEvent
from .protocol import FoldingRangeKind
from .protocol import GeneralClientCapabilities
from .protocol import InitializeError
from .protocol import InitializeParams
Expand Down Expand Up @@ -400,6 +401,16 @@ def get_initialize_params(variables: Dict[str, str], workspace_folders: List[Wor
"selectionRange": {
"dynamicRegistration": True
},
"foldingRange": {
"dynamicRegistration": True,
"foldingRangeKind": {
"valueSet": [
FoldingRangeKind.Comment,
FoldingRangeKind.Imports,
FoldingRangeKind.Region
]
}
},
"codeLens": {
"dynamicRegistration": True
},
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
regions = [
range_to_region(folding_range_to_range(folding_range), self.view)
for kind in kinds
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
Loading

0 comments on commit 032e3fd

Please sign in to comment.