@@ -178,6 +178,10 @@ def executeCommand(cls, params: dict):
178
178
def formatting (cls , params : dict ):
179
179
return Request ("textDocument/formatting" , params )
180
180
181
+ @classmethod
182
+ def rangeFormatting (cls , params : dict ):
183
+ return Request ("textDocument/rangeFormatting" , params )
184
+
181
185
@classmethod
182
186
def documentSymbols (cls , params : dict ):
183
187
return Request ("textDocument/documentSymbol" , params )
@@ -1398,7 +1402,7 @@ def run(self, edit):
1398
1402
"uri" : filename_to_uri (self .view .file_name ())
1399
1403
},
1400
1404
"options" : {
1401
- "tabSize" : 4 ,
1405
+ "tabSize" : 4 , # TODO: Fetch these from the project settings / global settings
1402
1406
"insertSpaces" : True
1403
1407
}
1404
1408
}
@@ -1411,6 +1415,36 @@ def handle_response(self, response, pos):
1411
1415
{'changes' : response })
1412
1416
1413
1417
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
+
1414
1448
class LspSymbolDefinitionCommand (sublime_plugin .TextCommand ):
1415
1449
def is_enabled (self , event = None ):
1416
1450
# TODO: check what kind of scope we're in.
0 commit comments