From d8a673515a8ec9fd102d6da8d8fa499ae2770f11 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 22 Apr 2018 12:28:12 +0100 Subject: Close #1162 - Implement completion support with LSP servers --- autoload/ale/completion.vim | 39 ++++++++++++++++++++++++++++++++------ autoload/ale/completion/python.vim | 3 +++ autoload/ale/linter.vim | 8 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 autoload/ale/completion/python.vim (limited to 'autoload') diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index 7ad7f9da..29b1df1f 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -28,6 +28,7 @@ let s:LSP_COMPLETION_REFERENCE_KIND = 18 " the insert cursor is. If one of these matches, we'll check for completions. let s:should_complete_map = { \ '': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$|\.$', +\ 'rust': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$|\.$|::$', \} " Regular expressions for finding the start column to replace with completion. @@ -38,6 +39,7 @@ let s:omni_start_map = { " A map of exact characters for triggering LSP completions. let s:trigger_character_map = { \ '': ['.'], +\ 'rust': ['.', '::'], \} function! s:GetFiletypeValue(map, filetype) abort @@ -215,7 +217,21 @@ function! ale#completion#ParseTSServerCompletionEntryDetails(response) abort return l:results endfunction +function! ale#completion#NullFilter(buffer, item) abort + return 1 +endfunction + function! ale#completion#ParseLSPCompletions(response) abort + let l:buffer = bufnr('') + let l:info = get(b:, 'ale_completion_info', {}) + let l:Filter = get(l:info, 'completion_filter', v:null) + + if l:Filter is v:null + let l:Filter = function('ale#completion#NullFilter') + else + let l:Filter = ale#util#GetFunction(l:Filter) + endif + let l:item_list = [] if type(get(a:response, 'result')) is type([]) @@ -228,6 +244,16 @@ function! ale#completion#ParseLSPCompletions(response) abort let l:results = [] for l:item in l:item_list + if !call(l:Filter, [l:buffer, l:item]) + continue + endif + + let l:word = matchstr(l:item.label, '\v^[^(]+') + + if empty(l:word) + continue + endif + " See :help complete-items for Vim completion kinds if l:item.kind is s:LSP_COMPLETION_METHOD_KIND let l:kind = 'm' @@ -244,11 +270,11 @@ function! ale#completion#ParseLSPCompletions(response) abort endif call add(l:results, { - \ 'word': l:item.label, + \ 'word': l:word, \ 'kind': l:kind, \ 'icase': 1, \ 'menu': l:item.detail, - \ 'info': l:item.documentation, + \ 'info': get(l:item, 'documentation', ''), \}) endfor @@ -349,6 +375,10 @@ function! s:GetLSPCompletions(linter) abort if l:request_id let b:ale_completion_info.conn_id = l:id let b:ale_completion_info.request_id = l:request_id + + if has_key(a:linter, 'completion_filter') + let b:ale_completion_info.completion_filter = a:linter.completion_filter + endif endif endfunction @@ -378,10 +408,7 @@ function! ale#completion#GetCompletions() abort for l:linter in ale#linter#Get(&filetype) if !empty(l:linter.lsp) - if l:linter.lsp is# 'tsserver' - \|| get(g:, 'ale_completion_experimental_lsp_support', 0) - call s:GetLSPCompletions(l:linter) - endif + call s:GetLSPCompletions(l:linter) endif endfor endfunction diff --git a/autoload/ale/completion/python.vim b/autoload/ale/completion/python.vim new file mode 100644 index 00000000..6b65c5b5 --- /dev/null +++ b/autoload/ale/completion/python.vim @@ -0,0 +1,3 @@ +function! ale#completion#python#CompletionItemFilter(buffer, item) abort + return a:item.label !~# '\v^__[a-z_]+__' +endfunction diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index d059a12d..7e32b7bc 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -194,6 +194,14 @@ function! ale#linter#PreProcess(linter) abort if !s:IsCallback(l:obj.project_root_callback) throw '`project_root_callback` must be a callback for LSP linters' endif + + if has_key(a:linter, 'completion_filter') + let l:obj.completion_filter = a:linter.completion_filter + + if !s:IsCallback(l:obj.completion_filter) + throw '`completion_filter` must be a callback' + endif + endif endif let l:obj.output_stream = get(a:linter, 'output_stream', 'stdout') -- cgit v1.2.3