summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-11-26 22:27:08 +0000
committerw0rp <devw0rp@gmail.com>2017-11-26 22:27:08 +0000
commit21b460bb1dcb099786ebe57d8cd6ec4fd9618861 (patch)
tree6c65fa3b3408187e5cd9aa67baa5ca6fab75428b
parent01318b6930c536cb63f09541bde11a9d48d4a12e (diff)
downloadale-21b460bb1dcb099786ebe57d8cd6ec4fd9618861.zip
Implement go to defintion for LSP linters
-rw-r--r--autoload/ale/definition.vim61
-rw-r--r--autoload/ale/lsp/message.vim9
-rw-r--r--test/lsp/test_lsp_client_messages.vader14
-rw-r--r--test/test_go_to_definition.vader161
4 files changed, 227 insertions, 18 deletions
diff --git a/autoload/ale/definition.vim b/autoload/ale/definition.vim
index 4b062b3e..b20c01a0 100644
--- a/autoload/ale/definition.vim
+++ b/autoload/ale/definition.vim
@@ -44,15 +44,40 @@ function! ale#definition#HandleTSServerResponse(conn_id, response) abort
endif
endfunction
+function! ale#definition#HandleLSPResponse(conn_id, response) abort
+ if has_key(a:response, 'id')
+ \&& has_key(s:go_to_definition_map, a:response.id)
+ let l:options = remove(s:go_to_definition_map, a:response.id)
+
+ " The result can be a Dictionary item, a List of the same, or null.
+ let l:result = get(a:response, 'result', v:null)
+
+ if type(l:result) is type({})
+ let l:result = [l:result]
+ elseif type(l:result) isnot type([])
+ let l:result = []
+ endif
+
+ for l:item in l:result
+ let l:filename = ale#path#FromURI(l:item.uri)
+ let l:line = l:item.range.start.line + 1
+ let l:column = l:item.range.start.character
+
+ call ale#definition#Open(l:options, l:filename, l:line, l:column)
+ break
+ endfor
+ endif
+endfunction
+
function! s:GoToLSPDefinition(linter, options) abort
let l:buffer = bufnr('')
let [l:line, l:column] = getcurpos()[1:2]
- let l:lsp_details = ale#linter#StartLSP(
- \ l:buffer,
- \ a:linter,
- \ function('ale#definition#HandleTSServerResponse'),
- \)
+ let l:Callback = a:linter.lsp is# 'tsserver'
+ \ ? function('ale#definition#HandleTSServerResponse')
+ \ : function('ale#definition#HandleLSPResponse')
+
+ let l:lsp_details = ale#linter#StartLSP(l:buffer, a:linter, l:Callback)
if empty(l:lsp_details)
return 0
@@ -61,11 +86,25 @@ function! s:GoToLSPDefinition(linter, options) abort
let l:id = l:lsp_details.connection_id
let l:root = l:lsp_details.project_root
- let l:message = ale#lsp#tsserver_message#Definition(
- \ l:buffer,
- \ l:line,
- \ l:column
- \)
+ if a:linter.lsp is# 'tsserver'
+ let l:message = ale#lsp#tsserver_message#Definition(
+ \ l:buffer,
+ \ l:line,
+ \ l:column
+ \)
+ else
+ " Send a message saying the buffer has changed first, or the
+ " definition position probably won't make sense.
+ call ale#lsp#Send(l:id, ale#lsp#message#DidChange(l:buffer), l:root)
+
+ let l:column = min([l:column, len(getline(l:line))])
+
+ " 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#Definition(l:buffer, l:line, l:column)
+ endif
+
let l:request_id = ale#lsp#Send(l:id, l:message, l:root)
let s:go_to_definition_map[l:request_id] = {
@@ -75,7 +114,7 @@ endfunction
function! ale#definition#GoTo(options) abort
for l:linter in ale#linter#Get(&filetype)
- if l:linter.lsp is# 'tsserver'
+ if !empty(l:linter.lsp)
call s:GoToLSPDefinition(l:linter, a:options)
endif
endfor
diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim
index b3a039ce..0b73cfc2 100644
--- a/autoload/ale/lsp/message.vim
+++ b/autoload/ale/lsp/message.vim
@@ -107,3 +107,12 @@ function! ale#lsp#message#Completion(buffer, line, column, trigger_character) ab
return l:message
endfunction
+
+function! ale#lsp#message#Definition(buffer, line, column) abort
+ return [0, 'textDocument/definition', {
+ \ 'textDocument': {
+ \ 'uri': ale#path#ToURI(expand('#' . a:buffer . ':p')),
+ \ },
+ \ 'position': {'line': a:line - 1, 'character': a:column},
+ \}]
+endfunction
diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader
index 346f79ca..3abdd8a1 100644
--- a/test/lsp/test_lsp_client_messages.vader
+++ b/test/lsp/test_lsp_client_messages.vader
@@ -129,6 +129,20 @@ Execute(ale#lsp#message#Completion() should return correct messages with a trigg
\ }
\ ],
\ ale#lsp#message#Completion(bufnr(''), 12, 34, '.')
+ \
+Execute(ale#lsp#message#Definition() should return correct messages):
+ AssertEqual
+ \ [
+ \ 0,
+ \ 'textDocument/definition',
+ \ {
+ \ 'textDocument': {
+ \ 'uri': ale#path#ToURI(g:dir . '/foo/bar.ts'),
+ \ },
+ \ 'position': {'line': 11, 'character': 34},
+ \ }
+ \ ],
+ \ ale#lsp#message#Definition(bufnr(''), 12, 34)
Execute(ale#lsp#tsserver_message#Open() should return correct messages):
AssertEqual
diff --git a/test/test_go_to_definition.vader b/test/test_go_to_definition.vader
index c6e87712..dd0e9903 100644
--- a/test/test_go_to_definition.vader
+++ b/test/test_go_to_definition.vader
@@ -4,7 +4,7 @@ Before:
let g:old_filename = expand('%:p')
let g:Callback = 0
- let g:message = []
+ let g:message_list = []
let g:expr_list = []
runtime autoload/ale/definition.vim
@@ -21,7 +21,7 @@ Before:
endfunction
function! ale#lsp#Send(conn_id, message, root) abort
- let g:message = a:message
+ call add(g:message_list, a:message)
return 42
endfunction
@@ -36,8 +36,9 @@ After:
unlet! g:old_filename
unlet! g:Callback
- unlet! g:message
+ unlet! g:message_list
unlet! g:expr_list
+ unlet! b:ale_linters
runtime autoload/ale/definition.vim
runtime autoload/ale/linter.vim
@@ -119,8 +120,8 @@ Execute(tsserver completion requests should be sent):
\ 'function(''ale#definition#HandleTSServerResponse'')',
\ string(g:Callback)
AssertEqual
- \ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}],
- \ g:message
+ \ [[0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]],
+ \ g:message_list
AssertEqual {'42': {'open_in_tab': 0}}, ale#definition#GetMap()
Execute(tsserver tab completion requests should be sent):
@@ -133,6 +134,152 @@ Execute(tsserver tab completion requests should be sent):
\ 'function(''ale#definition#HandleTSServerResponse'')',
\ string(g:Callback)
AssertEqual
- \ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}],
- \ g:message
+ \ [[0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]],
+ \ g:message_list
+ AssertEqual {'42': {'open_in_tab': 1}}, ale#definition#GetMap()
+
+Given python(Some Python file):
+ foo
+ somelongerline
+ bazxyzxyzxyz
+
+Execute(Other files should be jumped to for LSP definition responses):
+ call ale#definition#SetMap({3: {'open_in_tab': 0}})
+ call ale#definition#HandleLSPResponse(
+ \ 1,
+ \ {
+ \ 'id': 3,
+ \ 'result': {
+ \ 'uri': ale#path#ToURI(g:dir . '/completion_dummy_file'),
+ \ 'range': {
+ \ 'start': {'line': 2, 'character': 7},
+ \ },
+ \ },
+ \ }
+ \)
+
+ AssertEqual
+ \ [
+ \ 'edit ' . fnameescape(g:dir . '/completion_dummy_file'),
+ \ ],
+ \ g:expr_list
+ AssertEqual [3, 7], getpos('.')[1:2]
+ AssertEqual {}, ale#definition#GetMap()
+
+Execute(Other files should be jumped to in tabs for LSP definition responses):
+ call ale#definition#SetMap({3: {'open_in_tab': 1}})
+ call ale#definition#HandleLSPResponse(
+ \ 1,
+ \ {
+ \ 'id': 3,
+ \ 'result': {
+ \ 'uri': ale#path#ToURI(g:dir . '/completion_dummy_file'),
+ \ 'range': {
+ \ 'start': {'line': 2, 'character': 7},
+ \ },
+ \ },
+ \ }
+ \)
+
+ AssertEqual
+ \ [
+ \ 'tabedit ' . fnameescape(g:dir . '/completion_dummy_file'),
+ \ ],
+ \ g:expr_list
+ AssertEqual [3, 7], getpos('.')[1:2]
+ AssertEqual {}, ale#definition#GetMap()
+
+Execute(Definition responses with lists should be handled):
+ call ale#definition#SetMap({3: {'open_in_tab': 0}})
+ call ale#definition#HandleLSPResponse(
+ \ 1,
+ \ {
+ \ 'id': 3,
+ \ 'result': [
+ \ {
+ \ 'uri': ale#path#ToURI(g:dir . '/completion_dummy_file'),
+ \ 'range': {
+ \ 'start': {'line': 2, 'character': 7},
+ \ },
+ \ },
+ \ {
+ \ 'uri': ale#path#ToURI(g:dir . '/other_file'),
+ \ 'range': {
+ \ 'start': {'line': 20, 'character': 3},
+ \ },
+ \ },
+ \ ],
+ \ }
+ \)
+
+ AssertEqual
+ \ [
+ \ 'edit ' . fnameescape(g:dir . '/completion_dummy_file'),
+ \ ],
+ \ g:expr_list
+ AssertEqual [3, 7], getpos('.')[1:2]
+ AssertEqual {}, ale#definition#GetMap()
+
+Execute(Definition responses with null response should be handled):
+ call ale#definition#SetMap({3: {'open_in_tab': 0}})
+ call ale#definition#HandleLSPResponse(1, {'id': 3, 'result': v:null})
+
+ AssertEqual [], g:expr_list
+
+Execute(LSP completion requests should be sent):
+ runtime ale_linters/python/pyls.vim
+ let b:ale_linters = ['pyls']
+ call setpos('.', [bufnr(''), 1, 5, 0])
+
+ ALEGoToDefinition
+
+ AssertEqual
+ \ 'function(''ale#definition#HandleLSPResponse'')',
+ \ string(g:Callback)
+
+ AssertEqual
+ \ [
+ \ [1, 'textDocument/didChange', {
+ \ 'textDocument': {
+ \ 'uri': ale#path#ToURI(expand('%:p')),
+ \ 'version': g:ale_lsp_next_version_id - 1,
+ \ },
+ \ 'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}]
+ \ }],
+ \ [0, 'textDocument/definition', {
+ \ 'textDocument': {'uri': ale#path#ToURI(expand('%:p'))},
+ \ 'position': {'line': 0, 'character': 3},
+ \ }],
+ \ ],
+ \ g:message_list
+
+ AssertEqual {'42': {'open_in_tab': 0}}, ale#definition#GetMap()
+
+Execute(LSP tab completion requests should be sent):
+ runtime ale_linters/python/pyls.vim
+ let b:ale_linters = ['pyls']
+ call setpos('.', [bufnr(''), 1, 5, 0])
+
+ ALEGoToDefinitionInTab
+
+ AssertEqual
+ \ 'function(''ale#definition#HandleLSPResponse'')',
+ \ string(g:Callback)
+
+ AssertEqual
+ \ [
+ \ [1, 'textDocument/didChange', {
+ \ 'textDocument': {
+ \ 'uri': ale#path#ToURI(expand('%:p')),
+ \ 'version': g:ale_lsp_next_version_id - 1,
+ \ },
+ \ 'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}]
+ \ }],
+ \ [0, 'textDocument/definition', {
+ \ 'textDocument': {'uri': ale#path#ToURI(expand('%:p'))},
+ \ 'position': {'line': 0, 'character': 3},
+ \ }],
+ \ ],
+ \ g:message_list
+
AssertEqual {'42': {'open_in_tab': 1}}, ale#definition#GetMap()