12
12
from .core .views import has_single_nonempty_selection
13
13
from .core .views import text_document_formatting
14
14
from .core .views import text_document_range_formatting
15
+ from .core .views import text_document_ranges_formatting
15
16
from .core .views import will_save_wait_until
16
17
from .save_command import LspSaveCommand , SaveTask
17
18
import sublime
@@ -115,16 +116,27 @@ class LspFormatDocumentRangeCommand(LspTextCommand):
115
116
capability = 'documentRangeFormattingProvider'
116
117
117
118
def is_enabled (self , event : Optional [dict ] = None , point : Optional [int ] = None ) -> bool :
118
- if super ().is_enabled (event , point ):
119
- return has_single_nonempty_selection (self .view )
119
+ if not super ().is_enabled (event , point ):
120
+ return False
121
+ if has_single_nonempty_selection (self .view ):
122
+ return True
123
+ if self .view .has_non_empty_selection_region () and \
124
+ bool (self .best_session ('documentRangeFormattingProvider.rangesSupport' )):
125
+ return True
120
126
return False
121
127
122
128
def run (self , edit : sublime .Edit , event : Optional [dict ] = None ) -> None :
123
- session = self .best_session (self .capability )
124
- selection = first_selection_region (self .view )
125
- if session and selection is not None :
126
- req = text_document_range_formatting (self .view , selection )
127
- session .send_request (req , lambda response : apply_text_edits_to_view (response , self .view ))
129
+ if has_single_nonempty_selection (self .view ):
130
+ session = self .best_session (self .capability )
131
+ selection = first_selection_region (self .view )
132
+ if session and selection is not None :
133
+ req = text_document_range_formatting (self .view , selection )
134
+ session .send_request (req , lambda response : apply_text_edits_to_view (response , self .view ))
135
+ elif self .view .has_non_empty_selection_region ():
136
+ session = self .best_session ('documentRangeFormattingProvider.rangesSupport' )
137
+ if session :
138
+ req = text_document_ranges_formatting (self .view )
139
+ session .send_request (req , lambda response : apply_text_edits_to_view (response , self .view ))
128
140
129
141
130
142
class LspFormatCommand (LspTextCommand ):
@@ -139,11 +151,20 @@ def is_visible(self, event: Optional[dict] = None, point: Optional[int] = None)
139
151
return self .is_enabled (event , point )
140
152
141
153
def description (self , ** kwargs ) -> str :
142
- return "Format Selection" if self ._range_formatting_available () else "Format File"
154
+ if self ._range_formatting_available ():
155
+ if has_single_nonempty_selection (self .view ):
156
+ return "Format Selection"
157
+ return "Format Selections"
158
+ return "Format File"
143
159
144
160
def run (self , edit : sublime .Edit , event : Optional [dict ] = None ) -> None :
145
161
command = 'lsp_format_document_range' if self ._range_formatting_available () else 'lsp_format_document'
146
162
self .view .run_command (command )
147
163
148
164
def _range_formatting_available (self ) -> bool :
149
- return has_single_nonempty_selection (self .view ) and bool (self .best_session ('documentRangeFormattingProvider' ))
165
+ if has_single_nonempty_selection (self .view ) and bool (self .best_session ('documentRangeFormattingProvider' )):
166
+ return True
167
+ if self .view .has_non_empty_selection_region () and \
168
+ bool (self .best_session ('documentRangeFormattingProvider.rangesSupport' )):
169
+ return True
170
+ return False
0 commit comments