Skip to content

Commit a36baad

Browse files
heavenshellmattn
authored andcommitted
Fix :LspDenoDefinition to handle correctly
Run `:LspDenoDefinition` twice at built-in function, the result of the second run is not processed correctly
1 parent ec38220 commit a36baad

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

settings/deno.vim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ function! s:open_new_buffer(ctx, server, type, data) abort
120120
execute 'call cursor(' . l:line . ',' . l:col . ')'
121121
endfunction
122122

123+
function! s:normalize_target_uris(result) abort
124+
" When run `:LspDenoDefinition` at Deno's built-in function like `console`
125+
" `targetUri` is `deno:/asset/lib.deno.shared_globals.d.ts`.
126+
" Run `:LspDenoDefinition` again,
127+
" Deno targetUri is `file:///path/to/deno/project/deno%3A/asset/lib.deno.shared_globals.d.ts`
128+
" and it contains extra file path before `deno:/`
129+
" Remove unecessary path from all targetUri for display the definition.
130+
for item in a:result
131+
if !has_key(item, 'targetUri')
132+
continue
133+
endif
134+
let l:target_uri = item['targetUri']
135+
if match(l:target_uri, '^file:///.*/deno%3A/.*') != -1
136+
let l:deno_path = matchstr(l:target_uri, 'deno%3A/.*')
137+
let item['targetUri'] = substitute(l:deno_path, 'deno%3A', 'deno:', '')
138+
endif
139+
endfor
140+
return a:result
141+
endfunction
142+
123143
function! s:handle_deno_location(ctx, server, type, data) abort "ctx = {counter, list, last_command_id, jump_if_one, mods, in_preview}
124144
"" Based on vim-lsp/autoload/lsp/ui/vim.vim s:handle_location()
125145
if a:ctx['last_command_id'] != lsp#_last_command()
@@ -130,7 +150,13 @@ function! s:handle_deno_location(ctx, server, type, data) abort "ctx = {counter,
130150
if lsp#client#is_error(a:data['response']) || !has_key(a:data['response'], 'result')
131151
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': ' . lsp#client#error_message(a:data['response']))
132152
return
153+
elseif type(a:data['response']['result']) == type(v:null)
154+
" e.g.
155+
" `import` response v:null
156+
call lsp#utils#error('Failed to retrieve '. a:type . ' for ' . a:server . ': response is null')
157+
return
133158
else
159+
let a:data['response']['result'] = s:normalize_target_uris(a:data['response']['result'])
134160
let a:ctx['list'] = a:ctx['list'] + lsp#utils#location#_lsp_to_vim_list(a:data['response']['result'])
135161
endif
136162

0 commit comments

Comments
 (0)