#1162 Add unfinished experimental code for supporting LSP completion, clean up the tests, and make the completion cancelling better

This commit is contained in:
w0rp 2017-11-26 12:24:18 +00:00
parent 2e50aadd56
commit b1a6abdda6
9 changed files with 677 additions and 384 deletions

View File

@ -2,8 +2,45 @@
" Description: Completion support for LSP linters
let s:timer_id = -1
let s:last_done_pos = []
function! s:GetRegex(map, filetype) abort
" CompletionItemKind values from the LSP protocol.
let s:LSP_COMPLETION_TEXT_KIND = 1
let s:LSP_COMPLETION_METHOD_KIND = 2
let s:LSP_COMPLETION_FUNCTION_KIND = 3
let s:LSP_COMPLETION_CONSTRUCTOR_KIND = 4
let s:LSP_COMPLETION_FIELD_KIND = 5
let s:LSP_COMPLETION_VARIABLE_KIND = 6
let s:LSP_COMPLETION_CLASS_KIND = 7
let s:LSP_COMPLETION_INTERFACE_KIND = 8
let s:LSP_COMPLETION_MODULE_KIND = 9
let s:LSP_COMPLETION_PROPERTY_KIND = 10
let s:LSP_COMPLETION_UNIT_KIND = 11
let s:LSP_COMPLETION_VALUE_KIND = 12
let s:LSP_COMPLETION_ENUM_KIND = 13
let s:LSP_COMPLETION_KEYWORD_KIND = 14
let s:LSP_COMPLETION_SNIPPET_KIND = 15
let s:LSP_COMPLETION_COLOR_KIND = 16
let s:LSP_COMPLETION_FILE_KIND = 17
let s:LSP_COMPLETION_REFERENCE_KIND = 18
" Regular expressions for checking the characters in the line before where
" the insert cursor is. If one of these matches, we'll check for completions.
let s:should_complete_map = {
\ '<default>': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$|\.$',
\}
" Regular expressions for finding the start column to replace with completion.
let s:omni_start_map = {
\ '<default>': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$',
\}
" A map of exact characters for triggering LSP completions.
let s:trigger_character_map = {
\ '<default>': ['.'],
\}
function! s:GetFiletypeValue(map, filetype) abort
for l:part in reverse(split(a:filetype, '\.'))
let l:regex = get(a:map, l:part, [])
@ -13,18 +50,12 @@ function! s:GetRegex(map, filetype) abort
endfor
" Use the default regex for other files.
return s:should_complete_map['<default>']
return a:map['<default>']
endfunction
" Regular expressions for checking the characters in the line before where
" the insert cursor is. If one of these matches, we'll check for completions.
let s:should_complete_map = {
\ '<default>': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$|\.$',
\}
" Check if we should look for completions for a language.
function! ale#completion#GetPrefix(filetype, line, column) abort
let l:regex = s:GetRegex(s:should_complete_map, a:filetype)
let l:regex = s:GetFiletypeValue(s:should_complete_map, a:filetype)
" The column we're using completions for is where we are inserting text,
" like so:
" abc
@ -33,11 +64,15 @@ function! ale#completion#GetPrefix(filetype, line, column) abort
return matchstr(getline(a:line)[: a:column - 2], l:regex)
endfunction
" Regular expressions for finding the start column to replace with completion.
let s:omni_start_map = {
\ 'javascript': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$',
\ 'typescript': '\v[a-zA-Z$_][a-zA-Z$_0-9]*$',
\}
function! ale#completion#GetTriggerCharacter(filetype, prefix) abort
let l:char_list = s:GetFiletypeValue(s:trigger_character_map, a:filetype)
if index(l:char_list, a:prefix) >= 0
return a:prefix
endif
return ''
endfunction
function! ale#completion#Filter(suggestions, prefix) abort
" For completing...
@ -82,7 +117,7 @@ function! ale#completion#OmniFunc(findstart, base) abort
if a:findstart
let l:line = b:ale_completion_info.line
let l:column = b:ale_completion_info.column
let l:regex = s:GetRegex(s:omni_start_map, &filetype)
let l:regex = s:GetFiletypeValue(s:omni_start_map, &filetype)
let l:up_to_column = getline(l:line)[: l:column - 2]
let l:match = matchstr(l:up_to_column, l:regex)
@ -180,7 +215,47 @@ function! ale#completion#ParseTSServerCompletionEntryDetails(response) abort
return l:results
endfunction
function! ale#completion#HandleTSServerLSPResponse(conn_id, response) abort
function! ale#completion#ParseLSPCompletions(response) abort
let l:item_list = []
if type(get(a:response, 'result')) is type([])
let l:item_list = a:response.result
elseif type(get(a:response, 'result')) is type({})
\&& type(get(a:response.result, 'items')) is type([])
let l:item_list = a:response.result.items
endif
let l:results = []
for l:item in l:item_list
" See :help complete-items for Vim completion kinds
if l:item.kind is s:LSP_COMPLETION_METHOD_KIND
let l:kind = 'm'
elseif l:item.kind is s:LSP_COMPLETION_CONSTRUCTOR_KIND
let l:kind = 'm'
elseif l:item.kind is s:LSP_COMPLETION_FUNCTION_KIND
let l:kind = 'f'
elseif l:item.kind is s:LSP_COMPLETION_CLASS_KIND
let l:kind = 'f'
elseif l:item.kind is s:LSP_COMPLETION_INTERFACE_KIND
let l:kind = 'f'
else
let l:kind = 'v'
endif
call add(l:results, {
\ 'word': l:item.label,
\ 'kind': l:kind,
\ 'icase': 1,
\ 'menu': l:item.detail,
\ 'info': l:item.documentation,
\})
endfor
return l:results
endfunction
function! ale#completion#HandleTSServerResponse(conn_id, response) abort
if !s:CompletionStillValid(get(a:response, 'request_seq'))
return
endif
@ -216,28 +291,55 @@ function! ale#completion#HandleTSServerLSPResponse(conn_id, response) abort
endif
endfunction
function! ale#completion#HandleLSPResponse(conn_id, response) abort
if !s:CompletionStillValid(get(a:response, 'id'))
return
endif
call ale#completion#Show(
\ a:response,
\ 'ale#completion#ParseLSPCompletions',
\)
endfunction
function! s:GetLSPCompletions(linter) abort
let l:buffer = bufnr('')
let l:lsp_details = ale#linter#StartLSP(
\ l:buffer,
\ a:linter,
\ function('ale#completion#HandleTSServerLSPResponse'),
\)
let l:Callback = a:linter.lsp is# 'tsserver'
\ ? function('ale#completion#HandleTSServerResponse')
\ : function('ale#completion#HandleLSPResponse')
let l:lsp_details = ale#linter#StartLSP(l:buffer, a:linter, l:Callback)
if empty(l:lsp_details)
return 0
endif
let l:id = l:lsp_details.connection_id
let l:command = l:lsp_details.command
let l:root = l:lsp_details.project_root
let l:message = ale#lsp#tsserver_message#Completions(
\ l:buffer,
\ b:ale_completion_info.line,
\ b:ale_completion_info.column,
\ b:ale_completion_info.prefix,
\)
if a:linter.lsp is# 'tsserver'
let l:message = ale#lsp#tsserver_message#Completions(
\ l:buffer,
\ b:ale_completion_info.line,
\ b:ale_completion_info.column,
\ b:ale_completion_info.prefix,
\)
else
" For LSP completions, we need to clamp the column to the length of
" the line. python-language-server and perhaps others do not implement
" this correctly.
let l:message = ale#lsp#message#Completion(
\ l:buffer,
\ b:ale_completion_info.line,
\ min([
\ b:ale_completion_info.line_length,
\ b:ale_completion_info.column
\ ]),
\ '',
\)
endif
let l:request_id = ale#lsp#Send(l:id, l:message, l:root)
if l:request_id
@ -247,6 +349,10 @@ function! s:GetLSPCompletions(linter) abort
endfunction
function! ale#completion#GetCompletions() abort
if !g:ale_completion_enabled
return
endif
let [l:line, l:column] = getcurpos()[1:2]
let l:prefix = ale#completion#GetPrefix(&filetype, l:line, l:column)
@ -255,8 +361,11 @@ function! ale#completion#GetCompletions() abort
return
endif
let l:line_length = len(getline('.'))
let b:ale_completion_info = {
\ 'line': l:line,
\ 'line_length': l:line_length,
\ 'column': l:column,
\ 'prefix': l:prefix,
\ 'conn_id': 0,
@ -264,8 +373,11 @@ function! ale#completion#GetCompletions() abort
\}
for l:linter in ale#linter#Get(&filetype)
if l:linter.lsp is# 'tsserver'
call s:GetLSPCompletions(l:linter)
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
endif
endfor
endfunction
@ -292,15 +404,18 @@ function! ale#completion#StopTimer() abort
endfunction
function! ale#completion#Queue() abort
let l:time = get(b:, 'ale_complete_done_time', 0)
if l:time && ale#util#ClockMilliseconds() - l:time < 100
" Do not ask for completions shortly after we just closed the menu.
if !g:ale_completion_enabled
return
endif
let s:timer_pos = getcurpos()[1:2]
if s:timer_pos == s:last_done_pos
" Do not ask for completions if the cursor rests on the position we
" last completed on.
return
endif
" If we changed the text again while we're still waiting for a response,
" then invalidate the requests before the timer ticks again.
if exists('b:ale_completion_info')
@ -317,7 +432,10 @@ function! ale#completion#Done() abort
" Reset settings when completion is done.
if exists('b:ale_old_omnifunc')
let &l:omnifunc = b:ale_old_omnifunc
if b:ale_old_omnifunc isnot# 'pythoncomplete#Complete'
let &l:omnifunc = b:ale_old_omnifunc
endif
unlet b:ale_old_omnifunc
endif
@ -326,8 +444,7 @@ function! ale#completion#Done() abort
unlet b:ale_old_completopt
endif
" Set a timestamp, so we can avoid requesting completions again.
let b:ale_complete_done_time = ale#util#ClockMilliseconds()
let s:last_done_pos = getcurpos()[1:2]
endfunction
function! s:Setup(enabled) abort

View File

@ -86,3 +86,24 @@ function! ale#lsp#message#DidClose(buffer) abort
\ },
\}]
endfunction
let s:COMPLETION_TRIGGER_INVOKED = 1
let s:COMPLETION_TRIGGER_CHARACTER = 2
function! ale#lsp#message#Completion(buffer, line, column, trigger_character) abort
let l:message = [0, 'textDocument/completion', {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
\ },
\ 'position': {'line': a:line - 1, 'character': a:column - 1},
\}]
if !empty(a:trigger_character)
let l:message[2].context = {
\ 'triggerKind': s:COMPLETION_TRIGGER_CHARACTER,
\ 'triggerCharacter': a:trigger_character,
\}
endif
return l:message
endfunction

View File

@ -0,0 +1,172 @@
Before:
Save g:ale_completion_enabled
Save g:ale_completion_delay
Save g:ale_completion_max_suggestions
Save g:ale_completion_experimental_lsp_support
Save &l:omnifunc
Save &l:completeopt
unlet! g:ale_completion_experimental_lsp_support
let g:ale_completion_enabled = 1
let g:get_completions_called = 0
let g:feedkeys_calls = []
runtime autoload/ale/util.vim
function! ale#util#FeedKeys(string, mode) abort
call add(g:feedkeys_calls, [a:string, a:mode])
endfunction
function! CheckCompletionCalled(expect_success) abort
let g:get_completions_called = 0
" We just want to check if the function is called.
function! ale#completion#GetCompletions()
let g:get_completions_called = 1
endfunction
let g:ale_completion_delay = 0
call ale#completion#Queue()
sleep 1m
AssertEqual a:expect_success, g:get_completions_called
endfunction
After:
Restore
unlet! g:get_completions_called
unlet! b:ale_old_omnifunc
unlet! b:ale_old_completopt
unlet! b:ale_completion_info
unlet! b:ale_completion_response
unlet! b:ale_completion_parser
unlet! b:ale_complete_done_time
unlet! g:ale_completion_experimental_lsp_support
delfunction CheckCompletionCalled
" Stop any timers we left behind.
" This stops the tests from failing randomly.
call ale#completion#StopTimer()
runtime autoload/ale/completion.vim
runtime autoload/ale/util.vim
Execute(ale#completion#GetCompletions should be called when the cursor position stays the same):
call CheckCompletionCalled(1)
Given typescript():
let abc = y.
let foo = ab
let foo = (ab)
Execute(ale#completion#GetCompletions should not be called when the cursor position changes):
call setpos('.', [bufnr(''), 1, 2, 0])
" We just want to check if the function is called.
function! ale#completion#GetCompletions()
let g:get_completions_called = 1
endfunction
let g:ale_completion_delay = 0
call ale#completion#Queue()
" Change the cursor position before the callback is triggered.
call setpos('.', [bufnr(''), 2, 2, 0])
sleep 1m
Assert !g:get_completions_called
Execute(Completion should not be done shortly after the CompleteDone function):
call CheckCompletionCalled(1)
call ale#completion#Done()
call CheckCompletionCalled(0)
Execute(ale#completion#Show() should remember the omnifunc setting and replace it):
let &l:omnifunc = 'FooBar'
call ale#completion#Show('Response', 'Parser')
AssertEqual 'FooBar', b:ale_old_omnifunc
AssertEqual 'ale#completion#OmniFunc', &l:omnifunc
Execute(ale#completion#Show() should remember the completeopt setting and replace it):
let &l:completeopt = 'menu'
call ale#completion#Show('Response', 'Parser')
AssertEqual 'menu', b:ale_old_completopt
AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt
Execute(ale#completion#OmniFunc() should also remember the completeopt setting and replace it):
let &l:completeopt = 'menu'
call ale#completion#OmniFunc(0, '')
AssertEqual 'menu', b:ale_old_completopt
AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt
Execute(ale#completion#Show() should make the correct feedkeys() call):
call ale#completion#Show('Response', 'Parser')
AssertEqual [["\<C-x>\<C-o>", 'n']], g:feedkeys_calls
Execute(ale#completion#Show() should set up the response and parser):
call ale#completion#Show('Response', 'Parser')
AssertEqual 'Response', b:ale_completion_response
AssertEqual 'Parser', b:ale_completion_parser
Execute(ale#completion#Done() should restore old omnifunc values):
let b:ale_old_omnifunc = 'FooBar'
call ale#completion#Done()
" We reset the old omnifunc setting and remove the buffer variable.
AssertEqual 'FooBar', &l:omnifunc
Assert !has_key(b:, 'ale_old_omnifunc')
Execute(ale#completion#Done() should restore the old completeopt setting):
let b:ale_old_completopt = 'menu'
let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'
call ale#completion#Done()
AssertEqual 'menu', &l:completeopt
Assert !has_key(b:, 'ale_old_completopt')
Execute(ale#completion#Done() should leave settings alone when none were remembered):
let &l:omnifunc = 'BazBoz'
let &l:completeopt = 'menu'
call ale#completion#Done()
AssertEqual 'BazBoz', &l:omnifunc
AssertEqual 'menu', &l:completeopt
Execute(The completion request_id should be reset when queuing again):
let b:ale_completion_info = {'request_id': 123}
let g:ale_completion_delay = 0
call ale#completion#Queue()
sleep 1m
AssertEqual 0, b:ale_completion_info.request_id
Execute(b:ale_completion_info should be set up correctly when requesting completions):
call setpos('.', [bufnr(''), 3, 14, 0])
call ale#completion#GetCompletions()
AssertEqual
\ {
\ 'request_id': 0,
\ 'conn_id': 0,
\ 'column': 14,
\ 'line_length': 14,
\ 'line': 3,
\ 'prefix': 'ab',
\ },
\ b:ale_completion_info

View File

@ -0,0 +1,36 @@
Execute(Prefix filtering should work for Lists of strings):
AssertEqual
\ ['FooBar', 'foo'],
\ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], 'foo')
AssertEqual
\ ['FooBar', 'FongBar', 'baz', 'foo'],
\ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], '.')
Execute(Prefix filtering should work for completion items):
AssertEqual
\ [{'word': 'FooBar'}, {'word': 'foo'}],
\ ale#completion#Filter(
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ 'foo'
\ )
AssertEqual
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ ale#completion#Filter(
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ '.'
\ )

View File

@ -0,0 +1,19 @@
Given typescript():
let abc = y.
let foo = ab
let foo = (ab)
Execute(Completion should be done after dots in TypeScript):
AssertEqual '.', ale#completion#GetPrefix(&filetype, 1, 13)
Execute(Completion should be done after words in TypeScript):
AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 2, 13)
Execute(Completion should be done after words in parens in TypeScript):
AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 3, 14)
Execute(Completion should not be done after parens in TypeScript):
AssertEqual '', ale#completion#GetPrefix(&filetype, 3, 15)
Execute(Completion prefixes should work for other filetypes):
AssertEqual 'ab', ale#completion#GetPrefix('xxxyyyzzz', 3, 14)

View File

@ -0,0 +1,171 @@
Before:
Save g:ale_completion_delay
Save g:ale_completion_max_suggestions
Save g:ale_completion_info
Save g:ale_completion_experimental_lsp_support
Save &l:omnifunc
Save &l:completeopt
unlet! g:ale_completion_experimental_lsp_support
let g:ale_completion_enabled = 1
call ale#test#SetDirectory('/testplugin/test/completion')
call ale#test#SetFilename('dummy.txt')
runtime autoload/ale/lsp.vim
let g:message = []
let g:Callback = ''
function! ale#linter#StartLSP(buffer, linter, callback) abort
let g:Callback = a:callback
return {
\ 'connection_id': 347,
\ 'project_root': '/foo/bar',
\}
endfunction
" Replace the Send function for LSP, so we can monitor calls to it.
function! ale#lsp#Send(conn_id, message, ...) abort
let g:message = a:message
endfunction
After:
Restore
unlet! g:message
unlet! g:Callback
unlet! b:ale_old_omnifunc
unlet! b:ale_old_completopt
unlet! b:ale_completion_info
unlet! b:ale_completion_response
unlet! b:ale_completion_parser
unlet! b:ale_complete_done_time
unlet! b:ale_linters
unlet! g:ale_completion_experimental_lsp_support
call ale#test#RestoreDirectory()
call ale#linter#Reset()
" Stop any timers we left behind.
" This stops the tests from failing randomly.
call ale#completion#StopTimer()
runtime autoload/ale/completion.vim
runtime autoload/ale/lsp.vim
Given typescript(Some typescript file):
foo
somelongerline
bazxyzxyzxyz
Execute(The right message should be sent for the initial tsserver request):
runtime ale_linters/typescript/tsserver.vim
let b:ale_linters = ['tsserver']
" The cursor position needs to match what was saved before.
call setpos('.', [bufnr(''), 1, 3, 0])
call ale#completion#GetCompletions()
" We should send the right callback.
AssertEqual
\ 'function(''ale#completion#HandleTSServerResponse'')',
\ string(g:Callback)
" We should send the right message.
AssertEqual
\ [0, 'ts@completions', {'file': expand('%:p'), 'line': 1, 'offset': 3, 'prefix': 'fo'}],
\ g:message
" We should set up the completion info correctly.
AssertEqual
\ {
\ 'line_length': 3,
\ 'conn_id': 0,
\ 'column': 3,
\ 'request_id': 0,
\ 'line': 1,
\ 'prefix': 'fo',
\ },
\ get(b:, 'ale_completion_info', {})
Execute(The right message sent to the tsserver LSP when the first completion message is received):
" The cursor position needs to match what was saved before.
call setpos('.', [bufnr(''), 1, 1, 0])
let b:ale_completion_info = {
\ 'conn_id': 123,
\ 'prefix': 'f',
\ 'request_id': 4,
\ 'line': 1,
\ 'column': 1,
\}
" We should only show up to this many suggestions.
let g:ale_completion_max_suggestions = 3
" Handle the response for completions.
call ale#completion#HandleTSServerResponse(123, {
\ 'request_seq': 4,
\ 'command': 'completions',
\ 'body': [
\ {'name': 'Baz'},
\ {'name': 'dingDong'},
\ {'name': 'Foo'},
\ {'name': 'FooBar'},
\ {'name': 'frazzle'},
\ {'name': 'FFS'},
\ ],
\})
" The entry details messages should have been sent.
AssertEqual
\ [
\ 0,
\ 'ts@completionEntryDetails',
\ {
\ 'file': expand('%:p'),
\ 'entryNames': ['Foo', 'FooBar', 'frazzle'],
\ 'offset': 1,
\ 'line': 1,
\ },
\ ],
\ g:message
Given python(Some Python file):
foo
somelongerline
bazxyzxyzxyz
Execute(The right message should be sent for the initial LSP request):
let g:ale_completion_experimental_lsp_support = 1
runtime ale_linters/python/pyls.vim
let b:ale_linters = ['pyls']
" The cursor position needs to match what was saved before.
call setpos('.', [bufnr(''), 1, 5, 0])
call ale#completion#GetCompletions()
" We should send the right callback.
AssertEqual
\ 'function(''ale#completion#HandleLSPResponse'')',
\ string(g:Callback)
" We should send the right message.
" The character index needs to be at most the index of the last character on
" the line, or integration with pyls will be broken.
AssertEqual
\ [0, 'textDocument/completion', {
\ 'textDocument': {'uri': ale#path#ToURI(expand('%:p'))},
\ 'position': {'line': 0, 'character': 2},
\ }],
\ g:message
" We should set up the completion info correctly.
AssertEqual
\ {
\ 'line_length': 3,
\ 'conn_id': 0,
\ 'column': 3,
\ 'request_id': 0,
\ 'line': 1,
\ 'prefix': 'fo',
\ },
\ get(b:, 'ale_completion_info', {})

View File

@ -0,0 +1,75 @@
Execute(TypeScript completions responses should be parsed correctly):
AssertEqual [],
\ ale#completion#ParseTSServerCompletions({
\ 'body': [],
\})
AssertEqual ['foo', 'bar', 'baz'],
\ ale#completion#ParseTSServerCompletions({
\ 'body': [
\ {'name': 'foo'},
\ {'name': 'bar'},
\ {'name': 'baz'},
\ ],
\})
Execute(TypeScript completion details responses should be parsed correctly):
AssertEqual
\ [
\ {
\ 'word': 'abc',
\ 'menu': '(property) Foo.abc: number',
\ 'info': '',
\ 'kind': 'f',
\ 'icase': 1,
\ },
\ {
\ 'word': 'def',
\ 'menu': '(property) Foo.def: number',
\ 'info': 'foo bar baz',
\ 'kind': 'f',
\ 'icase': 1,
\ },
\ ],
\ ale#completion#ParseTSServerCompletionEntryDetails({
\ 'body': [
\ {
\ 'name': 'abc',
\ 'kind': 'parameterName',
\ 'displayParts': [
\ {'text': '('},
\ {'text': 'property'},
\ {'text': ')'},
\ {'text': ' '},
\ {'text': 'Foo'},
\ {'text': '.'},
\ {'text': 'abc'},
\ {'text': ':'},
\ {'text': ' '},
\ {'text': 'number'},
\ ],
\ },
\ {
\ 'name': 'def',
\ 'kind': 'parameterName',
\ 'displayParts': [
\ {'text': '('},
\ {'text': 'property'},
\ {'text': ')'},
\ {'text': ' '},
\ {'text': 'Foo'},
\ {'text': '.'},
\ {'text': 'def'},
\ {'text': ':'},
\ {'text': ' '},
\ {'text': 'number'},
\ ],
\ 'documentation': [
\ {'text': 'foo'},
\ {'text': ' '},
\ {'text': 'bar'},
\ {'text': ' '},
\ {'text': 'baz'},
\ ],
\ },
\ ],
\})

View File

@ -101,6 +101,35 @@ Execute(ale#lsp#message#DidClose() should return correct messages):
\ ],
\ ale#lsp#message#DidClose(bufnr(''))
Execute(ale#lsp#message#Completion() should return correct messages):
AssertEqual
\ [
\ 0,
\ 'textDocument/completion',
\ {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(g:dir . '/foo/bar.ts'),
\ },
\ 'position': {'line': 11, 'character': 33},
\ }
\ ],
\ ale#lsp#message#Completion(bufnr(''), 12, 34, '')
Execute(ale#lsp#message#Completion() should return correct messages with a trigger charaacter):
AssertEqual
\ [
\ 0,
\ 'textDocument/completion',
\ {
\ 'textDocument': {
\ 'uri': ale#path#ToURI(g:dir . '/foo/bar.ts'),
\ },
\ 'position': {'line': 11, 'character': 33},
\ 'context': {'triggerKind': 2, 'triggerCharacter': '.'},
\ }
\ ],
\ ale#lsp#message#Completion(bufnr(''), 12, 34, '.')
Execute(ale#lsp#tsserver_message#Open() should return correct messages):
AssertEqual
\ [

View File

@ -1,347 +0,0 @@
Before:
Save g:ale_completion_enabled
Save g:ale_completion_delay
Save g:ale_completion_max_suggestions
Save &l:omnifunc
Save &l:completeopt
let g:test_vars = {
\ 'feedkeys_calls': [],
\}
function! ale#util#FeedKeys(string, mode) abort
call add(g:test_vars.feedkeys_calls, [a:string, a:mode])
endfunction
function! CheckCompletionCalled(expect_success) abort
let g:test_vars.get_completions_called = 0
" We just want to check if the function is called.
function! ale#completion#GetCompletions()
let g:test_vars.get_completions_called = 1
endfunction
let g:ale_completion_delay = 0
call ale#completion#Queue()
sleep 1m
AssertEqual a:expect_success, g:test_vars.get_completions_called
endfunction
After:
Restore
unlet! g:test_vars
unlet! b:ale_old_omnifunc
unlet! b:ale_old_completopt
unlet! b:ale_completion_info
unlet! b:ale_completion_response
unlet! b:ale_completion_parser
unlet! b:ale_complete_done_time
delfunction CheckCompletionCalled
" Stop any timers we left behind.
" This stops the tests from failing randomly.
call ale#completion#StopTimer()
runtime autoload/ale/completion.vim
runtime autoload/ale/lsp.vim
if g:ale_completion_enabled
call ale#completion#Enable()
else
call ale#completion#Disable()
endif
Execute(TypeScript completions responses should be parsed correctly):
AssertEqual [],
\ ale#completion#ParseTSServerCompletions({
\ 'body': [],
\})
AssertEqual ['foo', 'bar', 'baz'],
\ ale#completion#ParseTSServerCompletions({
\ 'body': [
\ {'name': 'foo'},
\ {'name': 'bar'},
\ {'name': 'baz'},
\ ],
\})
Execute(TypeScript completion details responses should be parsed correctly):
AssertEqual
\ [
\ {
\ 'word': 'abc',
\ 'menu': '(property) Foo.abc: number',
\ 'info': '',
\ 'kind': 'f',
\ 'icase': 1,
\ },
\ {
\ 'word': 'def',
\ 'menu': '(property) Foo.def: number',
\ 'info': 'foo bar baz',
\ 'kind': 'f',
\ 'icase': 1,
\ },
\ ],
\ ale#completion#ParseTSServerCompletionEntryDetails({
\ 'body': [
\ {
\ 'name': 'abc',
\ 'kind': 'parameterName',
\ 'displayParts': [
\ {'text': '('},
\ {'text': 'property'},
\ {'text': ')'},
\ {'text': ' '},
\ {'text': 'Foo'},
\ {'text': '.'},
\ {'text': 'abc'},
\ {'text': ':'},
\ {'text': ' '},
\ {'text': 'number'},
\ ],
\ },
\ {
\ 'name': 'def',
\ 'kind': 'parameterName',
\ 'displayParts': [
\ {'text': '('},
\ {'text': 'property'},
\ {'text': ')'},
\ {'text': ' '},
\ {'text': 'Foo'},
\ {'text': '.'},
\ {'text': 'def'},
\ {'text': ':'},
\ {'text': ' '},
\ {'text': 'number'},
\ ],
\ 'documentation': [
\ {'text': 'foo'},
\ {'text': ' '},
\ {'text': 'bar'},
\ {'text': ' '},
\ {'text': 'baz'},
\ ],
\ },
\ ],
\})
Execute(Prefix filtering should work for Lists of strings):
AssertEqual
\ ['FooBar', 'foo'],
\ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], 'foo')
AssertEqual
\ ['FooBar', 'FongBar', 'baz', 'foo'],
\ ale#completion#Filter(['FooBar', 'FongBar', 'baz', 'foo'], '.')
Execute(Prefix filtering should work for completion items):
AssertEqual
\ [{'word': 'FooBar'}, {'word': 'foo'}],
\ ale#completion#Filter(
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ 'foo'
\ )
AssertEqual
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ ale#completion#Filter(
\ [
\ {'word': 'FooBar'},
\ {'word': 'FongBar'},
\ {'word': 'baz'},
\ {'word': 'foo'},
\ ],
\ '.'
\ )
Execute(The right message sent to the tsserver LSP when the first completion message is received):
" The cursor position needs to match what was saved before.
call setpos('.', [bufnr(''), 1, 1, 0])
let b:ale_completion_info = {
\ 'conn_id': 123,
\ 'prefix': 'f',
\ 'request_id': 4,
\ 'line': 1,
\ 'column': 1,
\}
" We should only show up to this many suggestions.
let g:ale_completion_max_suggestions = 3
" Replace the Send function for LSP, so we can monitor calls to it.
function! ale#lsp#Send(conn_id, message) abort
let g:test_vars.message = a:message
endfunction
" Handle the response for completions.
call ale#completion#HandleTSServerLSPResponse(123, {
\ 'request_seq': 4,
\ 'command': 'completions',
\ 'body': [
\ {'name': 'Baz'},
\ {'name': 'dingDong'},
\ {'name': 'Foo'},
\ {'name': 'FooBar'},
\ {'name': 'frazzle'},
\ {'name': 'FFS'},
\ ],
\})
" The entry details messages should have been sent.
AssertEqual
\ [
\ 0,
\ 'ts@completionEntryDetails',
\ {
\ 'file': expand('%:p'),
\ 'entryNames': ['Foo', 'FooBar', 'frazzle'],
\ 'offset': 1,
\ 'line': 1,
\ },
\ ],
\ g:test_vars.message
Given typescript():
let abc = y.
let foo = ab
let foo = (ab)
Execute(Completion should be done after dots in TypeScript):
AssertEqual '.', ale#completion#GetPrefix(&filetype, 1, 13)
Execute(Completion should be done after words in TypeScript):
AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 2, 13)
Execute(Completion should be done after words in parens in TypeScript):
AssertEqual 'ab', ale#completion#GetPrefix(&filetype, 3, 14)
Execute(Completion should not be done after parens in TypeScript):
AssertEqual '', ale#completion#GetPrefix(&filetype, 3, 15)
Execute(Completion prefixes should work for other filetypes):
AssertEqual 'ab', ale#completion#GetPrefix('xxxyyyzzz', 3, 14)
Execute(ale#completion#Show() should remember the omnifunc setting and replace it):
let &l:omnifunc = 'FooBar'
call ale#completion#Show('Response', 'Parser')
AssertEqual 'FooBar', b:ale_old_omnifunc
AssertEqual 'ale#completion#OmniFunc', &l:omnifunc
Execute(ale#completion#Show() should remember the completeopt setting and replace it):
let &l:completeopt = 'menu'
call ale#completion#Show('Response', 'Parser')
AssertEqual 'menu', b:ale_old_completopt
AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt
Execute(ale#completion#OmniFunc() should also remember the completeopt setting and replace it):
let &l:completeopt = 'menu'
call ale#completion#OmniFunc(0, '')
AssertEqual 'menu', b:ale_old_completopt
AssertEqual 'menu,menuone,preview,noselect,noinsert', &l:completeopt
Execute(ale#completion#Show() should make the correct feedkeys() call):
call ale#completion#Show('Response', 'Parser')
AssertEqual [["\<C-x>\<C-o>", 'n']], g:test_vars.feedkeys_calls
Execute(ale#completion#Show() should set up the response and parser):
call ale#completion#Show('Response', 'Parser')
AssertEqual 'Response', b:ale_completion_response
AssertEqual 'Parser', b:ale_completion_parser
Execute(ale#completion#Done() should restore old omnifunc values):
let b:ale_old_omnifunc = 'FooBar'
call ale#completion#Done()
" We reset the old omnifunc setting and remove the buffer variable.
AssertEqual 'FooBar', &l:omnifunc
Assert !has_key(b:, 'ale_old_omnifunc')
Execute(ale#completion#Done() should restore the old completeopt setting):
let b:ale_old_completopt = 'menu'
let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'
call ale#completion#Done()
AssertEqual 'menu', &l:completeopt
Assert !has_key(b:, 'ale_old_completopt')
Execute(ale#completion#Done() should leave settings alone when none were remembered):
let &l:omnifunc = 'BazBoz'
let &l:completeopt = 'menu'
call ale#completion#Done()
AssertEqual 'BazBoz', &l:omnifunc
AssertEqual 'menu', &l:completeopt
Execute(The completion request_id should be reset when queuing again):
let b:ale_completion_info = {'request_id': 123}
let g:ale_completion_delay = 0
call ale#completion#Queue()
sleep 1m
AssertEqual 0, b:ale_completion_info.request_id
Execute(b:ale_completion_info should be set up correctly when requesting completions):
call setpos('.', [bufnr(''), 3, 14, 0])
call ale#completion#GetCompletions()
AssertEqual
\ {
\ 'request_id': 0,
\ 'conn_id': 0,
\ 'column': 14,
\ 'line': 3,
\ 'prefix': 'ab',
\ },
\ b:ale_completion_info
Execute(ale#completion#GetCompletions should be called when the cursor position stays the same):
call CheckCompletionCalled(1)
Execute(ale#completion#GetCompletions should not be called when the cursor position changes):
call setpos('.', [bufnr(''), 1, 2, 0])
let g:test_vars.get_completions_called = 0
" We just want to check if the function is called.
function! ale#completion#GetCompletions()
let g:test_vars.get_completions_called = 1
endfunction
let g:ale_completion_delay = 0
call ale#completion#Queue()
" Change the cursor position before the callback is triggered.
call setpos('.', [bufnr(''), 2, 2, 0])
sleep 1m
Assert !g:test_vars.get_completions_called
Execute(Completion should not be done shortly after the CompleteDone function):
call CheckCompletionCalled(1)
call ale#completion#Done()
call CheckCompletionCalled(0)