Skip to content

Commit cd84151

Browse files
author
Junfeng Li
committed
Fix RangeFormatting range end.
Close #444.
1 parent 143eaac commit cd84151

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

autoload/LSP.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,11 @@ endfunction
1717
function! LSP#character()
1818
return col('.') - 1
1919
endfunction
20+
21+
function! LSP#range_start_line()
22+
return v:lnum - 1
23+
endfunction
24+
25+
function! LSP#range_end_line()
26+
return v:lnum - 1 + v:count
27+
endfunction

autoload/LanguageClient.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,10 @@ function! LanguageClient#textDocument_rangeFormatting(...) abort
521521
\ 'text': LSP#text(),
522522
\ 'line': LSP#line(),
523523
\ 'character': LSP#character(),
524+
\ '&tabstop': &tabstop,
525+
\ '&expandtab': &expandtab,
526+
\ 'LSP#range_start_line()': LSP#range_start_line(),
527+
\ 'LSP#range_end_line()': LSP#range_end_line(),
524528
\ 'handle': s:IsFalse(l:callback),
525529
\ }
526530
call extend(l:params, get(a:000, 0, {}))

src/languageclient.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,34 +1257,22 @@ impl State {
12571257
pub fn textDocument_rangeFormatting(&mut self, params: &Option<Params>) -> Result<Value> {
12581258
self.textDocument_didChange(params)?;
12591259
info!("Begin {}", lsp::request::RangeFormatting::METHOD);
1260-
let (buftype, languageId, filename, handle): (String, String, String, bool) = self.gather_args(
1261-
&[
1262-
VimVar::Buftype,
1263-
VimVar::LanguageId,
1264-
VimVar::Filename,
1265-
VimVar::Handle,
1266-
],
1267-
params,
1268-
)?;
1260+
let (buftype, languageId, filename, handle, tab_size, insert_spaces, start_line, end_line):
1261+
(String, String, String, bool, u64, u64, u64, u64) = self.gather_args(
1262+
&[
1263+
VimVar::Buftype.to_key().as_str(),
1264+
VimVar::LanguageId.to_key().as_str(),
1265+
VimVar::Filename.to_key().as_str(),
1266+
VimVar::Handle.to_key().as_str(),
1267+
"&tabstop",
1268+
"&expandtab",
1269+
"LSP#range_start_line()",
1270+
"LSP#range_end_line()",
1271+
], params)?;
12691272
if !buftype.is_empty() || languageId.is_empty() {
12701273
return Ok(Value::Null);
12711274
}
12721275

1273-
let (tab_size, insert_spaces, start_line, end_line, end_character): (
1274-
u64,
1275-
u64,
1276-
u64,
1277-
u64,
1278-
u64,
1279-
) = self.eval(
1280-
[
1281-
"&tabstop",
1282-
"&expandtab",
1283-
"v:lnum - 1",
1284-
"v:lnum - 1 + v:count - 1",
1285-
"len(getline(v:lnum + v:count - 1))",
1286-
].as_ref(),
1287-
)?;
12881276
let insert_spaces = insert_spaces == 1;
12891277
let result = self.call(
12901278
Some(&languageId),
@@ -1305,7 +1293,7 @@ impl State {
13051293
},
13061294
end: Position {
13071295
line: end_line,
1308-
character: end_character,
1296+
character: 0,
13091297
},
13101298
},
13111299
},

tests/data/vimrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ let g:LanguageClient_serverCommands = {
2424
\ 'rust': ['rustup', 'run', 'stable', 'rls'],
2525
\ }
2626
let g:LanguageClient_selectionUI = 'location-list'
27+
set formatexpr=LanguageClient#textDocument_rangeFormatting_sync()
28+
set signcolumn=yes
2729

2830
function! HandleWindowProgress(params) abort
2931
echomsg json_encode(a:params)

0 commit comments

Comments
 (0)