@@ -224,22 +224,34 @@ function! lsp#ui#vim#document_format() abort
224
224
return s: document_format (0 )
225
225
endfunction
226
226
227
- function ! s: get_visual_selection_pos () abort
228
- " https://groups.google.com/d/msg/vim_dev/oCUQzO3y8XE/vfIMJiHCHtEJ
229
- " https://stackoverflow.com/a/6271254
230
- " getpos("'>'") doesn't give the right column so need to do extra processing
231
- let [line_start, column_start] = getpos (" '<" )[1 :2 ]
232
- let [line_end, column_end] = getpos (" '>" )[1 :2 ]
233
- let lines = getline (line_start, line_end)
234
- if len (lines ) == 0
235
- return [0 , 0 , 0 , 0 ]
236
- endif
237
- let lines [-1 ] = lines [-1 ][: column_end - (&selection == # ' inclusive' ? 1 : 2 )]
238
- let lines [0 ] = lines [0 ][column_start - 1 :]
239
- return [line_start, column_start, line_end, len (lines [-1 ])]
227
+ function ! s: get_selection_pos (type ) abort
228
+ if a: type == ? ' v'
229
+ let l: start_pos = getpos (" '<" )[1 :2 ]
230
+ let l: end_pos = getpos (" '>" )[1 :2 ]
231
+ " fix end_pos column (see :h getpos() and :h 'selection')
232
+ let l: end_line = getline (l: end_pos [0 ])
233
+ let l: offset = (&selection == # ' inclusive' ? 1 : 2 )
234
+ let l: end_pos [1 ] = len (l: end_line [:l: end_pos [1 ]- l: offset ])
235
+ " edge case: single character selected with selection=exclusive
236
+ if l: start_pos [0 ] == l: end_pos [0 ] && l: start_pos [1 ] > l: end_pos [1 ]
237
+ let l: end_pos [1 ] = l: start_pos [1 ]
238
+ endif
239
+ elseif a: type == ? ' line'
240
+ let l: start_pos = [line (" '[" ), 1 ]
241
+ let l: end_lnum = line (" ']" )
242
+ let l: end_pos = [line (" ']" ), len (getline (l: end_lnum ))]
243
+ elseif a: type == ? ' char'
244
+ let l: start_pos = getpos (" '[" )[1 :2 ]
245
+ let l: end_pos = getpos (" ']" )[1 :2 ]
246
+ else
247
+ let l: start_pos = [0 , 0 ]
248
+ let l: end_pos = [0 , 0 ]
249
+ endif
250
+
251
+ return l: start_pos + l: end_pos
240
252
endfunction
241
253
242
- function ! s: document_format_range (sync ) abort
254
+ function ! s: document_format_range (sync , type ) abort
243
255
let l: servers = filter (lsp#get_whitelisted_servers (), ' lsp#capabilities#has_document_range_formatting_provider(v:val)' )
244
256
let s: last_req_id = s: last_req_id + 1
245
257
@@ -251,7 +263,7 @@ function! s:document_format_range(sync) abort
251
263
" TODO: ask user to select server for formatting
252
264
let l: server = l: servers [0 ]
253
265
254
- let [l: start_lnum , l: start_col , l: end_lnum , l: end_col ] = s: get_visual_selection_pos ( )
266
+ let [l: start_lnum , l: start_col , l: end_lnum , l: end_col ] = s: get_selection_pos ( a: type )
255
267
let l: start_char = lsp#utils#to_char (' %' , l: start_lnum , l: start_col )
256
268
let l: end_char = lsp#utils#to_char (' %' , l: end_lnum , l: end_col )
257
269
redraw | echo ' Formatting document range ...'
@@ -274,11 +286,15 @@ function! s:document_format_range(sync) abort
274
286
endfunction
275
287
276
288
function ! lsp#ui#vim#document_range_format_sync () abort
277
- return s: document_format_range (1 )
289
+ return s: document_format_range (1 , visualmode () )
278
290
endfunction
279
291
280
292
function ! lsp#ui#vim#document_range_format () abort
281
- return s: document_format_range (0 )
293
+ return s: document_format_range (0 , visualmode ())
294
+ endfunction
295
+
296
+ function ! lsp#ui#vim#document_range_format_opfunc (type ) abort
297
+ return s: document_format_range (1 , a: type )
282
298
endfunction
283
299
284
300
function ! lsp#ui#vim#workspace_symbol () abort
0 commit comments