From 9bcf8a2336ff6f98e5032d30e17c2671d66946fe Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Mon, 7 Jan 2019 23:23:33 +0100 Subject: [PATCH 1/2] Manually trigger autocomplete even when prefix is "" --- autoload/ale/completion.vim | 9 +++++---- test/completion/test_completion_filtering.vader | 3 +++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index 3bf29191..b0981f37 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -106,7 +106,7 @@ function! ale#completion#Filter(buffer, filetype, suggestions, prefix) abort " foo. " ^ " We need to include all of the given suggestions. - if index(l:triggers, a:prefix) >= 0 + if index(l:triggers, a:prefix) >= 0 || empty(a:prefix) let l:filtered_suggestions = a:suggestions else let l:filtered_suggestions = [] @@ -509,17 +509,18 @@ function! ale#completion#GetCompletions() abort return endif - call ale#completion#AlwaysGetCompletions() + call ale#completion#AlwaysGetCompletions(1) endfunction " This function can be used to manually trigger autocomplete, even when " g:ale_completion_enabled is set to false -function! ale#completion#AlwaysGetCompletions() abort +function! ale#completion#AlwaysGetCompletions(...) abort + let l:need_prefix = get(a:, 1, 0) let [l:line, l:column] = getcurpos()[1:2] let l:prefix = ale#completion#GetPrefix(&filetype, l:line, l:column) - if empty(l:prefix) + if l:need_prefix && empty(l:prefix) return endif diff --git a/test/completion/test_completion_filtering.vader b/test/completion/test_completion_filtering.vader index ffb313ef..1122ab60 100644 --- a/test/completion/test_completion_filtering.vader +++ b/test/completion/test_completion_filtering.vader @@ -16,6 +16,9 @@ Execute(Prefix filtering should work for Lists of strings): AssertEqual \ ['FooBar', 'FongBar', 'baz', 'foo'], \ ale#completion#Filter(bufnr(''), '', ['FooBar', 'FongBar', 'baz', 'foo'], '.') + AssertEqual + \ ['FooBar', 'FongBar', 'baz', 'foo'], + \ ale#completion#Filter(bufnr(''), '', ['FooBar', 'FongBar', 'baz', 'foo'], '') Execute(Prefix filtering should work for completion items): AssertEqual From b1b05e6e66f84e93ebf521444e3d21500a87bcdf Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Tue, 8 Jan 2019 15:09:59 +0100 Subject: [PATCH 2/2] Optimize ale#completion#Filter when prefix is "" --- autoload/ale/completion.vim | 58 +++++++++++-------- plugin/ale.vim | 2 +- .../test_completion_filtering.vader | 3 + test/test_ale_complete_command.vader | 6 +- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index b0981f37..157cac36 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -89,6 +89,10 @@ function! ale#completion#GetPrefix(filetype, line, column) abort endfunction function! ale#completion#GetTriggerCharacter(filetype, prefix) abort + if empty(a:prefix) + return '' + endif + let l:char_list = s:GetFiletypeValue(s:trigger_character_map, a:filetype) if index(l:char_list, a:prefix) >= 0 @@ -100,33 +104,38 @@ endfunction function! ale#completion#Filter(buffer, filetype, suggestions, prefix) abort let l:excluded_words = ale#Var(a:buffer, 'completion_excluded_words') - let l:triggers = s:GetFiletypeValue(s:trigger_character_map, a:filetype) - " For completing... - " foo. - " ^ - " We need to include all of the given suggestions. - if index(l:triggers, a:prefix) >= 0 || empty(a:prefix) + if empty(a:prefix) let l:filtered_suggestions = a:suggestions else - let l:filtered_suggestions = [] + let l:triggers = s:GetFiletypeValue(s:trigger_character_map, a:filetype) - " Filter suggestions down to those starting with the prefix we used for - " finding suggestions in the first place. - " - " Some completion tools will include suggestions which don't even start - " with the characters we have already typed. - for l:item in a:suggestions - " A List of String values or a List of completion item Dictionaries - " is accepted here. - let l:word = type(l:item) is v:t_string ? l:item : l:item.word + " For completing... + " foo. + " ^ + " We need to include all of the given suggestions. + if index(l:triggers, a:prefix) >= 0 || empty(a:prefix) + let l:filtered_suggestions = a:suggestions + else + let l:filtered_suggestions = [] - " Add suggestions if the suggestion starts with a case-insensitive - " match for the prefix. - if l:word[: len(a:prefix) - 1] is? a:prefix - call add(l:filtered_suggestions, l:item) - endif - endfor + " Filter suggestions down to those starting with the prefix we + " used for finding suggestions in the first place. + " + " Some completion tools will include suggestions which don't even + " start with the characters we have already typed. + for l:item in a:suggestions + " A List of String values or a List of completion item + " Dictionaries is accepted here. + let l:word = type(l:item) is v:t_string ? l:item : l:item.word + + " Add suggestions if the suggestion starts with a + " case-insensitive match for the prefix. + if l:word[: len(a:prefix) - 1] is? a:prefix + call add(l:filtered_suggestions, l:item) + endif + endfor + endif endif if !empty(l:excluded_words) @@ -514,13 +523,12 @@ endfunction " This function can be used to manually trigger autocomplete, even when " g:ale_completion_enabled is set to false -function! ale#completion#AlwaysGetCompletions(...) abort - let l:need_prefix = get(a:, 1, 0) +function! ale#completion#AlwaysGetCompletions(need_prefix) abort let [l:line, l:column] = getcurpos()[1:2] let l:prefix = ale#completion#GetPrefix(&filetype, l:line, l:column) - if l:need_prefix && empty(l:prefix) + if a:need_prefix && empty(l:prefix) return endif diff --git a/plugin/ale.vim b/plugin/ale.vim index 4af59a91..15e50e9f 100644 --- a/plugin/ale.vim +++ b/plugin/ale.vim @@ -204,7 +204,7 @@ command! -bar ALEDocumentation :call ale#hover#ShowDocumentationAtCursor() " Search for appearances of a symbol, such as a type name or function name. command! -nargs=1 ALESymbolSearch :call ale#symbol#Search() -command! -bar ALEComplete :call ale#completion#AlwaysGetCompletions() +command! -bar ALEComplete :call ale#completion#AlwaysGetCompletions(0) " mappings for commands nnoremap (ale_previous) :ALEPrevious diff --git a/test/completion/test_completion_filtering.vader b/test/completion/test_completion_filtering.vader index 1122ab60..c5f14266 100644 --- a/test/completion/test_completion_filtering.vader +++ b/test/completion/test_completion_filtering.vader @@ -105,6 +105,9 @@ Execute(Excluding words from completion results should work with lists of String AssertEqual \ ['Deutsch'], \ ale#completion#Filter(bufnr(''), '', ['describe', 'Deutsch'], '.') + AssertEqual + \ ['Deutsch'], + \ ale#completion#Filter(bufnr(''), '', ['Deutsch'], '') Execute(Filtering shouldn't modify the original list): let b:ale_completion_excluded_words = ['it', 'describe'] diff --git a/test/test_ale_complete_command.vader b/test/test_ale_complete_command.vader index c6ef8bf5..11f781c2 100644 --- a/test/test_ale_complete_command.vader +++ b/test/test_ale_complete_command.vader @@ -1,9 +1,11 @@ Before: function! MockAlwaysGetCompletions() abort let g:get_completions_called = 0 + let g:always_get_completions_argument = -1 - function! ale#completion#AlwaysGetCompletions() abort + function! ale#completion#AlwaysGetCompletions(need_prefix) abort let g:get_completions_called = 1 + let g:always_get_completions_argument = a:need_prefix endfunction endfunction @@ -11,6 +13,7 @@ Before: After: unlet! g:get_completions_called + unlet! g:always_get_completions_argument delfunction MockAlwaysGetCompletions delfunction ale#completion#AlwaysGetCompletions @@ -20,3 +23,4 @@ Execute(ale#completion#AlwaysGetCompletions should be called when ALEComplete is AssertEqual 0, g:get_completions_called ALEComplete AssertEqual 1, g:get_completions_called + AssertEqual 0, g:always_get_completions_argument