Skip to content

Commit 04ef607

Browse files
heavenshellmattn
andauthored
Fix completion item text selection priority (#1614)
* Fix completion item text selection priority According to the Language Server Protocol 3.17 specification: - filterText is only used for filtering and sorting completion items - When textEdit is provided, insertText must be ignored * Fix filtering should be performed on filterText If `filterText` exists, the filtering target has been changed from `word`. The filter is defined as follows in the LSP specification: ``` A string that should be used when filtering a set of completion items. When omitted the label is used as the filter text for this item. ``` * Fix remove unnecessary empty check condition --------- Co-authored-by: mattn <[email protected]>
1 parent be06c95 commit 04ef607

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

autoload/lsp/omni.vim

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,22 @@ function! s:get_filter_label(item) abort
8585
return lsp#utils#_trim(a:item['word'])
8686
endfunction
8787

88+
function! s:get_filter_text(item) abort
89+
let l:completed_item = lsp#omni#get_managed_user_data_from_completed_item(a:item)
90+
if !has_key(l:completed_item, 'completion_item')
91+
return ''
92+
endif
93+
94+
if !has_key(l:completed_item['completion_item'], 'filterText')
95+
return ''
96+
endif
97+
98+
return l:completed_item['completion_item']['filterText']
99+
endfunction
100+
88101
function! s:prefix_filter(item, last_typed_word) abort
89-
let l:label = s:get_filter_label(a:item)
102+
let l:filter_text = s:get_filter_text(a:item)
103+
let l:label = empty(l:filter_text) ? s:get_filter_label(a:item) : l:filter_text
90104

91105
if g:lsp_ignorecase
92106
return stridx(tolower(l:label), tolower(a:last_typed_word)) == 0
@@ -96,7 +110,8 @@ function! s:prefix_filter(item, last_typed_word) abort
96110
endfunction
97111

98112
function! s:contains_filter(item, last_typed_word) abort
99-
let l:label = s:get_filter_label(a:item)
113+
let l:filter_text = s:get_filter_text(a:item)
114+
let l:label = empty(l:filter_text) ? s:get_filter_label(a:item) : l:filter_text
100115

101116
if g:lsp_ignorecase
102117
return stridx(tolower(l:label), tolower(a:last_typed_word)) >= 0
@@ -312,15 +327,7 @@ function! lsp#omni#get_vim_completion_items(options) abort
312327
let l:range = lsp#utils#text_edit#get_range(get(l:completion_item, 'textEdit', {}))
313328
let l:complete_word = ''
314329
if has_key(l:completion_item, 'textEdit') && type(l:completion_item['textEdit']) == s:t_dict && !empty(l:range) && has_key(l:completion_item['textEdit'], 'newText')
315-
let l:text_edit_new_text = l:completion_item['textEdit']['newText']
316-
if has_key(l:completion_item, 'filterText') && !empty(l:completion_item['filterText']) && matchstr(l:text_edit_new_text, '^' . l:refresh_pattern) ==# ''
317-
" Use filterText as word.
318-
let l:complete_word = l:completion_item['filterText']
319-
else
320-
" Use textEdit.newText as word.
321-
let l:complete_word = l:text_edit_new_text
322-
endif
323-
330+
let l:complete_word = l:completion_item['textEdit']['newText']
324331
let l:item_start_character = l:range['start']['character']
325332
let l:start_character = min([l:item_start_character, l:start_character])
326333
let l:start_characters += [l:item_start_character]

0 commit comments

Comments
 (0)