Skip to content

Commit 550c7eb

Browse files
Christian Clasonthomasfaingnaert
authored andcommitted
Allow custom server-specific symbol and completion item kind (#524)
* allow customization of symbol_kinds * allow customization of completion_item_kinds * make server name optional * add docs * slightly rearrange documentation * Remove trailing whitespace
1 parent a0b859d commit 550c7eb

File tree

4 files changed

+87
-30
lines changed

4 files changed

+87
-30
lines changed

autoload/lsp/omni.vim

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" constants {{{
22

3-
let s:kind_text_mappings = {
3+
let s:default_completion_item_kinds = {
44
\ '1': 'text',
55
\ '2': 'method',
66
\ '3': 'function',
@@ -28,6 +28,8 @@ let s:kind_text_mappings = {
2828
\ '25': 'type parameter',
2929
\ }
3030

31+
let s:completion_item_kinds = {}
32+
3133
let s:completion_status_success = 'success'
3234
let s:completion_status_failed = 'failed'
3335
let s:completion_status_pending = 'pending'
@@ -96,7 +98,7 @@ function! s:handle_omnicompletion(server_name, complete_counter, data) abort
9698
return
9799
endif
98100

99-
let l:result = s:get_completion_result(a:data)
101+
let l:result = s:get_completion_result(a:server_name, a:data)
100102
let l:matches = l:result['matches']
101103

102104
if g:lsp_async_completion
@@ -107,8 +109,25 @@ function! s:handle_omnicompletion(server_name, complete_counter, data) abort
107109
endif
108110
endfunction
109111

110-
function! lsp#omni#get_kind_text(completion_item) abort
111-
return has_key(a:completion_item, 'kind') && has_key(s:kind_text_mappings, a:completion_item['kind']) ? s:kind_text_mappings[a:completion_item['kind']] : ''
112+
function! lsp#omni#get_kind_text(completion_item, ...) abort
113+
let l:server = get(a:, 1, '')
114+
if empty(l:server) " server name
115+
let l:completion_item_kinds = s:default_completion_item_kinds
116+
else
117+
if !has_key(s:completion_item_kinds, l:server)
118+
let l:server_info = lsp#get_server_info(l:server)
119+
if has_key (l:server_info, 'config') && has_key(l:server_info['config'], 'completion_item_kinds')
120+
let s:completion_item_kinds[l:server] = s:default_completion_item_kinds
121+
call extend(s:completion_item_kinds[l:server] , l:server_info['config']['completion_item_kinds'])
122+
else
123+
let s:completion_item_kinds[l:server] = s:default_completion_item_kinds
124+
endif
125+
endif
126+
let l:completion_item_kinds = s:completion_item_kinds[l:server]
127+
endif
128+
129+
return has_key(a:completion_item, 'kind') && has_key(l:completion_item_kinds, a:completion_item['kind'])
130+
\ ? l:completion_item_kinds[a:completion_item['kind']] : ''
112131
endfunction
113132

114133
" auxiliary functions {{{
@@ -140,7 +159,7 @@ function! s:send_completion_request(info) abort
140159
\ })
141160
endfunction
142161

143-
function! s:get_completion_result(data) abort
162+
function! s:get_completion_result(server_name, data) abort
144163
let l:result = a:data['response']['result']
145164

146165
if type(l:result) == type([])
@@ -154,7 +173,7 @@ function! s:get_completion_result(data) abort
154173
let l:incomplete = 0
155174
endif
156175

157-
let l:matches = type(l:items) == type([]) ? map(l:items, {_, item -> lsp#omni#get_vim_completion_item(item, 1) }) : []
176+
let l:matches = type(l:items) == type([]) ? map(l:items, {_, item -> lsp#omni#get_vim_completion_item(item, a:server_name, 1) }) : []
158177

159178
return {'matches': l:matches, 'incomplete': l:incomplete}
160179
endfunction
@@ -182,7 +201,8 @@ function! s:remove_typed_part(word) abort
182201
endfunction
183202

184203
function! lsp#omni#default_get_vim_completion_item(item, ...) abort
185-
let l:do_remove_typed_part = get(a:, 1, 0)
204+
let l:server_name = get(a:, 1, '')
205+
let l:do_remove_typed_part = get(a:, 2, 0)
186206

187207
if g:lsp_insert_text_enabled && has_key(a:item, 'insertText') && !empty(a:item['insertText'])
188208
if has_key(a:item, 'insertTextFormat') && a:item['insertTextFormat'] != 1
@@ -199,7 +219,8 @@ function! lsp#omni#default_get_vim_completion_item(item, ...) abort
199219
if l:do_remove_typed_part
200220
let l:word = s:remove_typed_part(l:word)
201221
endif
202-
let l:kind = lsp#omni#get_kind_text(a:item)
222+
223+
let l:kind = lsp#omni#get_kind_text(a:item, l:server_name)
203224

204225
let l:completion = {
205226
\ 'word': l:word,
@@ -370,7 +391,7 @@ function! s:get_cursor_pos_and_edit_length(text_edit) abort
370391
endfunction
371392

372393
function! lsp#omni#get_completion_item_kinds() abort
373-
return map(keys(s:kind_text_mappings), {idx, key -> str2nr(key)})
394+
return map(keys(s:default_completion_item_kinds), {idx, key -> str2nr(key)})
374395
endfunction
375396

376397
" }}}

autoload/lsp/ui/vim.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function! s:handle_symbol(server, last_req_id, type, data) abort
408408
return
409409
endif
410410

411-
let l:list = lsp#ui#vim#utils#symbols_to_loc_list(a:data)
411+
let l:list = lsp#ui#vim#utils#symbols_to_loc_list(a:server, a:data)
412412

413413
call setqflist(l:list)
414414

autoload/lsp/ui/vim/utils.vim

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function! lsp#ui#vim#utils#locations_to_loc_list(result) abort
6565
return l:list
6666
endfunction
6767

68-
let s:symbol_kinds = {
68+
let s:default_symbol_kinds = {
6969
\ '1': 'file',
7070
\ '2': 'module',
7171
\ '3': 'namespace',
@@ -94,14 +94,16 @@ let s:symbol_kinds = {
9494
\ '26': 'type parameter',
9595
\ }
9696

97+
let s:symbol_kinds = {}
98+
9799
let s:diagnostic_severity = {
98100
\ 1: 'Error',
99101
\ 2: 'Warning',
100102
\ 3: 'Information',
101103
\ 4: 'Hint',
102104
\ }
103105

104-
function! lsp#ui#vim#utils#symbols_to_loc_list(result) abort
106+
function! lsp#ui#vim#utils#symbols_to_loc_list(server, result) abort
105107
if !has_key(a:result['response'], 'result')
106108
return []
107109
endif
@@ -123,7 +125,7 @@ function! lsp#ui#vim#utils#symbols_to_loc_list(result) abort
123125
\ 'filename': l:path,
124126
\ 'lnum': l:line,
125127
\ 'col': l:col,
126-
\ 'text': s:get_symbol_text_from_kind(l:symbol['kind']) . ' : ' . l:symbol['name'],
128+
\ 'text': s:get_symbol_text_from_kind(a:server, l:symbol['kind']) . ' : ' . l:symbol['name'],
127129
\ })
128130
endif
129131
endfor
@@ -176,12 +178,22 @@ function! s:is_file_uri(uri) abort
176178
return stridx(a:uri, 'file:///') == 0
177179
endfunction
178180

179-
function! s:get_symbol_text_from_kind(kind) abort
180-
return has_key(s:symbol_kinds, a:kind) ? s:symbol_kinds[a:kind] : 'unknown symbol ' . a:kind
181+
function! s:get_symbol_text_from_kind(server, kind) abort
182+
if !has_key(s:symbol_kinds, a:server)
183+
let l:server_info = lsp#get_server_info(a:server)
184+
if has_key (l:server_info, 'config') && has_key(l:server_info['config'], 'symbol_kinds')
185+
let s:symbol_kinds[a:server] = s:default_symbol_kinds
186+
call extend(s:symbol_kinds[a:server], l:server_info['config']['symbol_kinds'])
187+
else
188+
let s:symbol_kinds[a:server] = s:default_symbol_kinds
189+
endif
190+
endif
191+
let l:symbol_kinds = s:symbol_kinds[a:server]
192+
return has_key(l:symbol_kinds, a:kind) ? l:symbol_kinds[a:kind] : 'unknown symbol ' . a:kind
181193
endfunction
182194

183195
function! lsp#ui#vim#utils#get_symbol_kinds() abort
184-
return map(keys(s:symbol_kinds), {idx, key -> str2nr(key)})
196+
return map(keys(s:default_symbol_kinds), {idx, key -> str2nr(key)})
185197
endfunction
186198

187199
function! s:get_diagnostic_severity_text(severity) abort

doc/vim-lsp.txt

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,15 @@ The vim |dict| containing information about the server.
625625
'whitelist': ['*']
626626
'blacklist': ['javascript', 'typescript']
627627

628+
* workspace_config:
629+
optional vim |dict|
630+
Used to pass workspace configuration to the server after
631+
initialization. Configuration settings are language-server specific.
632+
633+
Example:
634+
'workspace_config': {'pyls': {'plugins': \
635+
{'pydocstyle': {'enabled': v:true}}}}
636+
628637
* config:
629638
optional vim |dict|
630639
Used to pass additional custom config.
@@ -646,24 +655,39 @@ The vim |dict| containing information about the server.
646655

647656
'cmd': function('s:myserver_cmd')
648657

649-
* workspace_config:
650-
optional vim |dict|
651-
Used to pass workspace configuration to the server after
652-
initialization. Configuration settings are language-server specific.
658+
The following per-server configuration options are supported by vim-lsp.
653659

654-
Example:
655-
'workspace_config': {'pyls': {'plugins': \
656-
{'pydocstyle': {'enabled': v:true}}}}
660+
* hover_conceal
661+
Type: |Boolean|
662+
Default: |g:lsp_hover_conceal|
657663

658-
hover_conceal *vim-lsp_hover_conceal*
659-
Type: |Boolean|
660-
Default: |g:lsp_hover_conceal|
664+
This takes precendence over the value of |g:lsp_hover_conceal|, to
665+
allow overriding this setting per server.
661666

662-
This takes precendence over the value of |g:lsp_hover_conceal|, to allow
663-
overriding this setting per server.
667+
Example:
668+
'config': { 'hover_conceal': 1 }
664669

665-
Example:
666-
'config': { 'hover_conceal': 1 }
670+
* symbol_kinds
671+
Type: |Dict|
672+
Default: |{}|
673+
674+
This allows overriding the default text mappings for symbol kinds
675+
(e.g., 'module', 'method') per server. Useful for abbreviating or
676+
removing the kind text.
677+
678+
Example:
679+
'config': { 'symbol_kinds': {'26': 'type' } }
680+
681+
* completion_item_kinds
682+
Type: |Dict|
683+
Default: |{}|
684+
685+
This allows overriding the default text mappings for completion item
686+
kinds (e.g., 'module', 'method') per server. Useful for abbreviating
687+
or removing the kind text.
688+
689+
Example:
690+
'config': { 'completion_item_kinds': {'26': 'type' } }
667691

668692
lsp#stop_server *vim-lsp-stop_server*
669693

0 commit comments

Comments
 (0)