Skip to content

Commit 3aba91c

Browse files
remove lsp#utils#to_col in favor of lsp#utils#position#_lsp_to_vim (#645)
* refactor to use lsp#utils#position#_lsp_to_vim * remove lsp#utils#to_col in favor of lsp#utils#position#_lsp_to_vim * fix doc
1 parent c347a80 commit 3aba91c

File tree

10 files changed

+42
-106
lines changed

10 files changed

+42
-106
lines changed

autoload/lsp/omni.vim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,7 @@ endfunction
456456
function! s:get_cursor_pos_and_edit_length(text_edit) abort
457457
if !empty(a:text_edit)
458458
let l:start = a:text_edit['range']['start']
459-
let l:line = l:start['line'] + 1
460-
let l:char = l:start['character']
461-
let l:col = lsp#utils#to_col('%', l:line, l:char)
459+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim('%', l:start)
462460
let l:length = len(a:text_edit['newText'])
463461
let l:pos = [0, l:line, l:col, 0]
464462
else

autoload/lsp/ui/vim.vim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,12 +542,8 @@ function! s:handle_rename_prepare(server, last_req_id, type, data) abort
542542

543543
let l:range = a:data['response']['result']
544544
let l:lines = getline(1, '$')
545-
let l:start_line = l:range['start']['line'] + 1
546-
let l:start_char = l:range['start']['character']
547-
let l:start_col = lsp#utils#to_col('%', l:start_line, l:start_char)
548-
let l:end_line = l:range['end']['line'] + 1
549-
let l:end_char = l:range['end']['character']
550-
let l:end_col = lsp#utils#to_col('%', l:end_line, l:end_char)
545+
let [l:start_line, l:start_col] = lsp#utils#position#_lsp_to_vim('%', l:range['start'])
546+
let [l:end_line, l:end_col] = lsp#utils#position#_lsp_to_vim('%', l:range['end'])
551547
if l:start_line ==# l:end_line
552548
let l:name = l:lines[l:start_line - 1][l:start_col - 1 : l:end_col - 2]
553549
else

autoload/lsp/ui/vim/diagnostics.vim

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ function! lsp#ui#vim#diagnostics#get_diagnostics_under_cursor() abort
6868
let l:closest_distance = -1
6969

7070
for l:diagnostic in l:diagnostics
71-
let l:range = l:diagnostic['range']
72-
let l:start_line = l:range['start']['line'] + 1
73-
let l:start_char = l:range['start']['character']
74-
let l:start_col = lsp#utils#to_col('%', l:start_line, l:start_char)
71+
let [l:start_line, l:start_col] = lsp#utils#position#_lsp_to_vim('%', l:diagnostic['range']['start'])
7572

7673
if l:line == l:start_line
7774
let l:distance = abs(l:start_col - l:col)
@@ -111,9 +108,7 @@ function! s:next_diagnostic(diagnostics) abort
111108
let l:next_line = 0
112109
let l:next_col = 0
113110
for l:diagnostic in a:diagnostics
114-
let l:line = l:diagnostic['range']['start']['line'] + 1
115-
let l:char = l:diagnostic['range']['start']['character']
116-
let l:col = lsp#utils#to_col('%', l:line, l:char)
111+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim('%', l:diagnostic['range']['start'])
117112
if l:line > l:view['lnum']
118113
\ || (l:line == l:view['lnum'] && l:col > l:view['col'] + 1)
119114
let l:next_line = l:line
@@ -124,9 +119,8 @@ function! s:next_diagnostic(diagnostics) abort
124119

125120
if l:next_line == 0
126121
" Wrap to start
127-
let l:next_line = a:diagnostics[0]['range']['start']['line'] + 1
128-
let l:next_char = a:diagnostics[0]['range']['start']['character']
129-
let l:next_col = lsp#utils#to_col('%', l:next_line, l:next_char) - 1
122+
let [l:next_line, l:next_col] = lsp#utils#position#_lsp_to_vim('%', a:diagnostics[0]['range']['start'])
123+
let l:next_col -= 1
130124
endif
131125

132126
let l:view['lnum'] = l:next_line
@@ -172,9 +166,7 @@ function! s:previous_diagnostic(diagnostics) abort
172166
let l:next_col = 0
173167
let l:index = len(a:diagnostics) - 1
174168
while l:index >= 0
175-
let l:line = a:diagnostics[l:index]['range']['start']['line'] + 1
176-
let l:char = a:diagnostics[l:index]['range']['start']['character']
177-
let l:col = lsp#utils#to_col('%', l:line, l:char)
169+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim('%', a:diagnostics[l:index]['range']['start'])
178170
if l:line < l:view['lnum']
179171
\ || (l:line == l:view['lnum'] && l:col < l:view['col'])
180172
let l:next_line = l:line
@@ -186,9 +178,8 @@ function! s:previous_diagnostic(diagnostics) abort
186178

187179
if l:next_line == 0
188180
" Wrap to end
189-
let l:next_line = a:diagnostics[-1]['range']['start']['line'] + 1
190-
let l:next_char = a:diagnostics[-1]['range']['start']['character']
191-
let l:next_col = lsp#utils#to_col('%', l:next_line, l:next_char) - 1
181+
let [l:next_line, l:next_col] = lsp#utils#position#_lsp_to_vim('%', a:diagnostics[-1]['range']['start'])
182+
let l:next_col -= 1
192183
endif
193184

194185
let l:view['lnum'] = l:next_line

autoload/lsp/ui/vim/diagnostics/textprop.vim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,8 @@ function! s:place_highlights(server_name, path, diagnostics) abort
117117
let l:bufnr = bufnr(a:path)
118118
if !empty(a:diagnostics) && l:bufnr >= 0
119119
for l:item in a:diagnostics
120-
let l:start_line = l:item['range']['start']['line'] + 1
121-
let l:start_char = l:item['range']['start']['character']
122-
let l:start_col = lsp#utils#to_col(l:bufnr, l:start_line, l:start_char)
123-
let l:end_line = l:item['range']['end']['line'] + 1
124-
let l:end_char = l:item['range']['end']['character']
125-
let l:end_col = lsp#utils#to_col(l:bufnr, l:end_line, l:end_char)
120+
let [l:start_line, l:start_col] = lsp#utils#position#_lsp_to_vim(l:bufnr, l:item['range']['start'])
121+
let [l:end_line, l:end_col] = lsp#utils#position#_lsp_to_vim(l:bufnr, l:item['range']['end'])
126122

127123
let l:prop_type = s:get_prop_type(a:server_name, l:item['severity'])
128124
call prop_add(l:start_line, l:start_col, {

autoload/lsp/ui/vim/highlights.vim

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,8 @@ function! s:place_highlights(server_name, path, diagnostics) abort
9595

9696
if !empty(a:diagnostics) && l:bufnr >= 0
9797
for l:item in a:diagnostics
98-
let l:line = l:item['range']['start']['line'] + 1
99-
let l:start_char = l:item['range']['start']['character']
100-
let l:start_col = lsp#utils#to_col(l:bufnr, l:line, l:start_char)
101-
let l:end_char = l:item['range']['end']['character']
102-
let l:end_col = lsp#utils#to_col(l:bufnr, l:line, l:end_char)
98+
let [l:line, l:start_col] = lsp#utils#position#_lsp_to_vim(l:bufnr, l:item['range']['start'])
99+
let [l:_, l:end_col] = lsp#utils#position#_lsp_to_vim(l:bufnr, l:item['range']['end'])
103100

104101
let l:name = get(s:severity_sign_names_mapping, l:item['severity'], 'LspError')
105102
let l:hl_name = l:name . 'Highlight'

autoload/lsp/ui/vim/references.vim

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,10 @@ endif
1212
" positions, one for each line.
1313
" Return a list of positions.
1414
function! s:range_to_position(bufnr, range) abort
15-
let l:start = a:range['start']
16-
let l:end = a:range['end']
1715
let l:position = []
1816

19-
let l:start_line = l:start['line'] + 1
20-
let l:start_char = l:start['character']
21-
let l:start_col = lsp#utils#to_col(a:bufnr, l:start_line, l:start_char)
22-
let l:end_line = l:end['line'] + 1
23-
let l:end_char = l:end['character']
24-
let l:end_col = lsp#utils#to_col(a:bufnr, l:end_line, l:end_char)
17+
let [l:start_line, l:start_col] = lsp#utils#position#_lsp_to_vim(a:bufnr, a:range['start'])
18+
let [l:end_line, l:end_col] = lsp#utils#position#_lsp_to_vim(a:bufnr, a:range['end'])
2519
if l:end_line == l:start_line
2620
let l:position = [[
2721
\ l:start_line,

autoload/lsp/ui/vim/utils.vim

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
2525
for l:location in l:locations
2626
if s:is_file_uri(l:location[l:uri])
2727
let l:path = lsp#utils#uri_to_path(l:location[l:uri])
28-
let l:line = l:location[l:range]['start']['line'] + 1
29-
let l:char = l:location[l:range]['start']['character']
30-
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
28+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim(l:path, l:location[l:range]['start'])
3129

3230
let l:index = l:line - 1
3331
if has_key(l:cache, l:path)
@@ -107,9 +105,7 @@ let s:diagnostic_severity = {
107105

108106
function! s:symbols_to_loc_list_children(server, path, list, symbols, depth) abort
109107
for l:symbol in a:symbols
110-
let l:line = l:symbol['range']['start']['line'] + 1
111-
let l:char = l:symbol['range']['start']['character']
112-
let l:col = lsp#utils#to_col(a:path, l:line, l:char)
108+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim(a:path, l:symbol['range']['start'])
113109

114110
call add(a:list, {
115111
\ 'filename': a:path,
@@ -138,9 +134,7 @@ function! lsp#ui#vim#utils#symbols_to_loc_list(server, result) abort
138134
let l:location = l:symbol['location']
139135
if s:is_file_uri(l:location['uri'])
140136
let l:path = lsp#utils#uri_to_path(l:location['uri'])
141-
let l:line = l:location['range']['start']['line'] + 1
142-
let l:char = l:location['range']['start']['character']
143-
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
137+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim(l:path, l:location['range']['start'])
144138
call add(l:list, {
145139
\ 'filename': l:path,
146140
\ 'lnum': l:line,
@@ -152,9 +146,7 @@ function! lsp#ui#vim#utils#symbols_to_loc_list(server, result) abort
152146
let l:location = a:result['request']['params']['textDocument']['uri']
153147
if s:is_file_uri(l:location)
154148
let l:path = lsp#utils#uri_to_path(l:location)
155-
let l:line = l:symbol['range']['start']['line'] + 1
156-
let l:char = l:symbol['range']['start']['character']
157-
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
149+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim(l:path, l:symbol['range']['start'])
158150
call add(l:list, {
159151
\ 'filename': l:path,
160152
\ 'lnum': l:line,
@@ -196,9 +188,7 @@ function! lsp#ui#vim#utils#diagnostics_to_loc_list(result) abort
196188
let l:text .= l:item['code'] . ':'
197189
endif
198190
let l:text .= l:item['message']
199-
let l:line = l:item['range']['start']['line'] + 1
200-
let l:char = l:item['range']['start']['character']
201-
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
191+
let [l:line, l:col] = lsp#utils#position#_lsp_to_vim(l:path, l:item['range'])
202192
call add(l:list, {
203193
\ 'filename': l:path,
204194
\ 'lnum': l:line,

autoload/lsp/utils.vim

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,6 @@ function! lsp#utils#echo_with_truncation(msg) abort
186186
exec 'echo l:msg'
187187
endfunction
188188

189-
" Convert a character-index (0-based) to byte-index (1-based)
190-
" This function requires a buffer specifier (expr, see :help bufname()),
191-
" a line number (lnum, 1-based), and a character-index (char, 0-based).
192-
function! lsp#utils#to_col(expr, lnum, char) abort
193-
let l:lines = getbufline(a:expr, a:lnum)
194-
if l:lines == []
195-
if type(a:expr) != v:t_string || !filereadable(a:expr)
196-
" invalid a:expr
197-
return a:char + 1
198-
endif
199-
" a:expr is a file that is not yet loaded as a buffer
200-
let l:lines = readfile(a:expr, '', a:lnum)
201-
endif
202-
let l:linestr = l:lines[-1]
203-
return strlen(strcharpart(l:linestr, 0, a:char)) + 1
204-
endfunction
205-
206189
" Convert a byte-index (1-based) to a character-index (0-based)
207190
" This function requires a buffer specifier (expr, see :help bufname()),
208191
" a line number (lnum, 1-based), and a byte-index (char, 1-based).

autoload/lsp/utils/position.vim

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
" @param bufnr = bufnr
1+
" This function can be error prone if the caller forgets to use +1 to vim line
2+
" so use lsp#utils#position#_lsp_to_vim instead
3+
" Convert a character-index (0-based) to byte-index (1-based)
4+
" This function requires a buffer specifier (expr, see :help bufname()),
5+
" a line number (lnum, 1-based), and a character-index (char, 0-based).
6+
function! s:to_col(expr, lnum, char) abort
7+
let l:lines = getbufline(a:expr, a:lnum)
8+
if l:lines == []
9+
if type(a:expr) != v:t_string || !filereadable(a:expr)
10+
" invalid a:expr
11+
return a:char + 1
12+
endif
13+
" a:expr is a file that is not yet loaded as a buffer
14+
let l:lines = readfile(a:expr, '', a:lnum)
15+
endif
16+
let l:linestr = l:lines[-1]
17+
return strlen(strcharpart(l:linestr, 0, a:char)) + 1
18+
endfunction
19+
20+
" @param expr = see :help bufname()
221
" @param position = {
322
" 'line': 1,
423
" 'character': 1
@@ -10,6 +29,6 @@
1029
function! lsp#utils#position#_lsp_to_vim(expr, position) abort
1130
let l:line = a:position['line'] + 1
1231
let l:char = a:position['character']
13-
let l:col = lsp#utils#to_col(a:expr, l:line, l:char)
32+
let l:col = s:to_col(a:expr, l:line, l:char)
1433
return [l:line, l:col]
1534
endfunction

test/lsp/utils.vimspec

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,6 @@ Describe lsp#utils
8282
End
8383
End
8484

85-
Describe lsp#utils#to_col
86-
It should return the byte-index from the given character-index on a buffer
87-
call setline(1, ['a β c', 'δ', ''])
88-
Assert lsp#utils#to_col('%', 1, 0) == 1
89-
Assert lsp#utils#to_col('%', 1, 1) == 2
90-
Assert lsp#utils#to_col('%', 1, 2) == 3
91-
Assert lsp#utils#to_col('%', 1, 3) == 5
92-
Assert lsp#utils#to_col('%', 1, 4) == 6
93-
Assert lsp#utils#to_col('%', 1, 5) == 7
94-
Assert lsp#utils#to_col('%', 2, 0) == 1
95-
Assert lsp#utils#to_col('%', 2, 1) == 3
96-
Assert lsp#utils#to_col('%', 3, 0) == 1
97-
%delete
98-
End
99-
100-
It should return the byte-index from the given character-index in an unloaded file
101-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 0) == 1
102-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 1) == 2
103-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 2) == 3
104-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 3) == 5
105-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 4) == 6
106-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 1, 5) == 7
107-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 2, 0) == 1
108-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 2, 1) == 3
109-
Assert lsp#utils#to_col('./test/testfiles/multibyte.txt', 3, 0) == 1
110-
End
111-
End
112-
11385
Describe lsp#utils#to_char
11486
It should return the character-index from the given byte-index on a buffer
11587
call setline(1, ['a β c', 'δ', ''])

0 commit comments

Comments
 (0)