Skip to content

Commit 47a883d

Browse files
glenvt18prabirshrestha
authored andcommitted
Fix empty locations handling (#538)
This fixes out-of-range error for l:locations[0].
1 parent 46af4cd commit 47a883d

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

autoload/lsp/ui/vim/utils.vim

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
77

88
let l:locations = type(a:result['response']['result']) == type({}) ? [a:result['response']['result']] : a:result['response']['result']
99

10+
if empty(l:locations) " some servers also return null so check to make sure it isn't empty
11+
return []
12+
endif
13+
1014
if has_key(l:locations[0],'targetUri') " server returns locationLinks
1115
let l:use_link = 1
1216
let l:uri = 'targetUri'
@@ -17,50 +21,48 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
1721
let l:range = 'range'
1822
endif
1923

20-
if !empty(l:locations) " some servers also return null so check to make sure it isn't empty
21-
let l:cache={}
22-
for l:location in l:locations
23-
if s:is_file_uri(l:location[l:uri])
24-
let l:path = lsp#utils#uri_to_path(l:location[l:uri])
25-
let l:line = l:location[l:range]['start']['line'] + 1
26-
let l:char = l:location[l:range]['start']['character']
27-
let l:col = lsp#utils#to_col(l:path, l:line, l:char)
24+
let l:cache={}
25+
for l:location in l:locations
26+
if s:is_file_uri(l:location[l:uri])
27+
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)
2831

29-
let l:index = l:line - 1
30-
if has_key(l:cache, l:path)
31-
let l:text = l:cache[l:path][l:index]
32-
else
33-
let l:contents = getbufline(l:path, 1, '$')
34-
if !empty(l:contents)
35-
let l:text = l:contents[l:index]
36-
else
37-
let l:contents = readfile(l:path)
38-
let l:cache[l:path] = l:contents
39-
let l:text = l:contents[l:index]
40-
endif
41-
endif
42-
if l:use_link
43-
let l:viewstart = l:location['targetRange']['start']['line']
44-
let l:viewend = l:location['targetRange']['end']['line']
45-
call add(l:list, {
46-
\ 'filename': l:path,
47-
\ 'lnum': l:line,
48-
\ 'col': l:col,
49-
\ 'text': l:text,
50-
\ 'viewstart': l:viewstart,
51-
\ 'viewend': l:viewend
52-
\ })
32+
let l:index = l:line - 1
33+
if has_key(l:cache, l:path)
34+
let l:text = l:cache[l:path][l:index]
35+
else
36+
let l:contents = getbufline(l:path, 1, '$')
37+
if !empty(l:contents)
38+
let l:text = l:contents[l:index]
5339
else
54-
call add(l:list, {
55-
\ 'filename': l:path,
56-
\ 'lnum': l:line,
57-
\ 'col': l:col,
58-
\ 'text': l:text,
59-
\ })
40+
let l:contents = readfile(l:path)
41+
let l:cache[l:path] = l:contents
42+
let l:text = l:contents[l:index]
6043
endif
6144
endif
62-
endfor
63-
endif
45+
if l:use_link
46+
let l:viewstart = l:location['targetRange']['start']['line']
47+
let l:viewend = l:location['targetRange']['end']['line']
48+
call add(l:list, {
49+
\ 'filename': l:path,
50+
\ 'lnum': l:line,
51+
\ 'col': l:col,
52+
\ 'text': l:text,
53+
\ 'viewstart': l:viewstart,
54+
\ 'viewend': l:viewend
55+
\ })
56+
else
57+
call add(l:list, {
58+
\ 'filename': l:path,
59+
\ 'lnum': l:line,
60+
\ 'col': l:col,
61+
\ 'text': l:text,
62+
\ })
63+
endif
64+
endif
65+
endfor
6466

6567
return l:list
6668
endfunction

0 commit comments

Comments
 (0)