Skip to content

Commit 604df77

Browse files
rwolstomv564
authored andcommitted
Implement textDocument/rangeFormatting (sublimelsp#166)
* Implement textDocument/rangeFormatting There might be race bugs when using this with multiple selections. * Only format selection when a single selection is present
1 parent 7a1fbfe commit 604df77

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

Commands/Default.sublime-commands

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
"command": "lsp_format_document",
3737
"args": {}
3838
},
39+
{
40+
"caption": "LSP: Format Selection",
41+
"command": "lsp_format_document_range",
42+
"args": {}
43+
},
3944
{
4045
"caption": "LSP: Restart Servers",
4146
"command": "lsp_restart_client",
@@ -51,4 +56,4 @@
5156
"command": "lsp_show_diagnostics_panel",
5257
"args": {}
5358
}
54-
]
59+
]

Menus/Context.sublime-menu

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
"command": "lsp_format_document",
2121
"caption": "Format Document"
2222
},
23+
{
24+
"command": "lsp_format_document_range",
25+
"caption": "Format Selection"
26+
},
2327
{ "caption": "-", "id": "lsp_end"}
24-
]
28+
]

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ Any fields in a client configuration can be overridden by adding an LSP settings
259259
* Recommendation: Override `f12` (built-in goto definition),
260260
* LSP falls back to ST3's built-in goto definition command in case LSP fails.
261261
* Format Document: UNBOUND
262+
* Format Selection: UNBOUND
262263
* Document Symbols: UNBOUND
263264

264265
**Workspace actions**

main.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def executeCommand(cls, params: dict):
178178
def formatting(cls, params: dict):
179179
return Request("textDocument/formatting", params)
180180

181+
@classmethod
182+
def rangeFormatting(cls, params: dict):
183+
return Request("textDocument/rangeFormatting", params)
184+
181185
@classmethod
182186
def documentSymbols(cls, params: dict):
183187
return Request("textDocument/documentSymbol", params)
@@ -1398,7 +1402,7 @@ def run(self, edit):
13981402
"uri": filename_to_uri(self.view.file_name())
13991403
},
14001404
"options": {
1401-
"tabSize": 4,
1405+
"tabSize": 4, # TODO: Fetch these from the project settings / global settings
14021406
"insertSpaces": True
14031407
}
14041408
}
@@ -1411,6 +1415,36 @@ def handle_response(self, response, pos):
14111415
{'changes': response})
14121416

14131417

1418+
class LspFormatDocumentRangeCommand(sublime_plugin.TextCommand):
1419+
def is_enabled(self):
1420+
if is_supported_view(self.view):
1421+
client = client_for_view(self.view)
1422+
if client and client.has_capability('documentRangeFormattingProvider'):
1423+
if len(self.view.sel()) == 1:
1424+
region = self.view.sel()[0]
1425+
if region.begin() != region.end():
1426+
return True
1427+
return False
1428+
1429+
def run(self, _):
1430+
client = client_for_view(self.view)
1431+
if client:
1432+
region = self.view.sel()[0]
1433+
params = {
1434+
"textDocument": {
1435+
"uri": filename_to_uri(self.view.file_name())
1436+
},
1437+
"range": Range.from_region(self.view, region).to_lsp(),
1438+
"options": {
1439+
"tabSize": 4, # TODO: Fetch these from the project settings / global settings
1440+
"insertSpaces": True
1441+
}
1442+
}
1443+
client.send_request(Request.rangeFormatting(params),
1444+
lambda response: self.view.run_command('lsp_apply_document_edit',
1445+
{'changes': response}))
1446+
1447+
14141448
class LspSymbolDefinitionCommand(sublime_plugin.TextCommand):
14151449
def is_enabled(self, event=None):
14161450
# TODO: check what kind of scope we're in.

0 commit comments

Comments
 (0)