From 4062b056691daca9593e01f67026e4199cd8da87 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Wed, 15 Jan 2020 16:45:38 +0100 Subject: [PATCH 1/3] Fix completion with langserver (autoimport in go) --- autoload/ale/completion.vim | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index 2b5756e4..a173a0d4 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -523,13 +523,45 @@ function! ale#completion#ParseLSPCompletions(response) abort let l:doc = l:doc.value endif - call add(l:results, { + let l:result = { \ 'word': l:word, \ 'kind': ale#completion#GetCompletionSymbols(get(l:item, 'kind', '')), \ 'icase': 1, \ 'menu': get(l:item, 'detail', ''), \ 'info': (type(l:doc) is v:t_string ? l:doc : ''), - \}) + \} + + if has_key(l:item, 'additionalTextEdits') + let l:text_changes = [] + for l:edit in l:item.additionalTextEdits + let l:range = l:edit.range + call add(l:text_changes, { + \ 'start': { + \ 'line': l:range.start.line + 1, + \ 'offset': l:range.start.character + 1, + \ }, + \ 'end': { + \ 'line': l:range.end.line + 1, + \ 'offset': l:range.end.character + 1, + \ }, + \ 'newText': l:edit.newText, + \}) + endfor + + let l:changes = [{ + \ 'fileName': expand('%:p'), + \ 'textChanges': l:text_changes, + \}] + \ + let l:result.user_data = json_encode({ + \ 'codeActions': [{ + \ 'description': 'completion', + \ 'changes': l:changes, + \ }], + \ }) + endif + + call add(l:results, l:result) endfor if has_key(l:info, 'prefix') From b339a8bfa0ebbe42e3f824e85f6900101cee8244 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Sat, 22 Feb 2020 07:52:50 +0100 Subject: [PATCH 2/3] Add support for rename (documentChanges) --- autoload/ale/rename.vim | 32 ++++++++++-- test/test_rename.vader | 109 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 4 deletions(-) diff --git a/autoload/ale/rename.vim b/autoload/ale/rename.vim index fbd6c2ad..36a334d2 100644 --- a/autoload/ale/rename.vim +++ b/autoload/ale/rename.vim @@ -83,6 +83,30 @@ function! ale#rename#HandleTSServerResponse(conn_id, response) abort \}, v:true) endfunction +function! s:getChanges(workspace_edit) abort + let l:changes = {} + + if has_key(a:workspace_edit, 'changes') && !empty(a:workspace_edit.changes) + return a:workspace_edit.changes + elseif has_key(a:workspace_edit, 'documentChanges') + let l:document_changes = [] + if type(a:workspace_edit.documentChanges) is v:t_dict && + \ has_key(a:workspace_edit.documentChanges, 'edits') + call add(l:document_changes, a:workspace_edit.documentChanges) + elseif type(a:workspace_edit.documentChanges) is v:t_list + let l:document_changes = a:workspace_edit.documentChanges + endif + + for l:text_document_edit in l:document_changes + let l:filename = l:text_document_edit.textDocument.uri + let l:edits = l:text_document_edit.edits + let l:changes[l:filename] = l:edits + endfor + endif + + return l:changes +endfunction + function! ale#rename#HandleLSPResponse(conn_id, response) abort if has_key(a:response, 'id') \&& has_key(s:rename_map, a:response.id) @@ -94,9 +118,9 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort return endif - let l:workspace_edit = a:response.result + let l:changes_map = s:getChanges(a:response.result) - if !has_key(l:workspace_edit, 'changes') || empty(l:workspace_edit.changes) + if empty(l:changes_map) call s:message('No changes received from server') return @@ -104,8 +128,8 @@ function! ale#rename#HandleLSPResponse(conn_id, response) abort let l:changes = [] - for l:file_name in keys(l:workspace_edit.changes) - let l:text_edits = l:workspace_edit.changes[l:file_name] + for l:file_name in keys(l:changes_map) + let l:text_edits = l:changes_map[l:file_name] let l:text_changes = [] for l:edit in l:text_edits diff --git a/test/test_rename.vader b/test/test_rename.vader index 3600df59..34d9e32e 100644 --- a/test/test_rename.vader +++ b/test/test_rename.vader @@ -327,6 +327,115 @@ Execute(Code actions from LSP should be handled): \ ], \ g:code_actions +Execute(DocumentChanges from LSP should be handled): + call ale#rename#HandleLSPResponse(1, { + \ 'id': 3, + \ 'result': { + \ 'documentChanges': [ + \ { + \ 'textDocument': { + \ 'version': 1.0, + \ 'uri': 'file:///foo/bar/file1.ts', + \ }, + \ 'edits': [ + \ { + \ 'range': { + \ 'start': { + \ 'line': 1, + \ 'character': 2, + \ }, + \ 'end': { + \ 'line': 3, + \ 'character': 4, + \ }, + \ }, + \ 'newText': 'bla123', + \ }, + \ ], + \ }, + \ ], + \ }, + \}) + + AssertEqual + \ [ + \ { + \ 'description': 'rename', + \ 'changes': [ + \ { + \ 'fileName': '/foo/bar/file1.ts', + \ 'textChanges': [ + \ { + \ 'start': { + \ 'line': 2, + \ 'offset': 3, + \ }, + \ 'end': { + \ 'line': 4, + \ 'offset': 5, + \ }, + \ 'newText': 'bla123', + \ }, + \ ], + \ }, + \ ], + \ } + \ ], + \ g:code_actions + +Execute(Single DocumentChange from LSP should be handled): + call ale#rename#HandleLSPResponse(1, { + \ 'id': 3, + \ 'result': { + \ 'documentChanges': { + \ 'textDocument': { + \ 'version': 1.0, + \ 'uri': 'file:///foo/bar/file1.ts', + \ }, + \ 'edits': [ + \ { + \ 'range': { + \ 'start': { + \ 'line': 1, + \ 'character': 2, + \ }, + \ 'end': { + \ 'line': 3, + \ 'character': 4, + \ }, + \ }, + \ 'newText': 'bla123', + \ }, + \ ], + \ }, + \ }, + \}) + + AssertEqual + \ [ + \ { + \ 'description': 'rename', + \ 'changes': [ + \ { + \ 'fileName': '/foo/bar/file1.ts', + \ 'textChanges': [ + \ { + \ 'start': { + \ 'line': 2, + \ 'offset': 3, + \ }, + \ 'end': { + \ 'line': 4, + \ 'offset': 5, + \ }, + \ 'newText': 'bla123', + \ }, + \ ], + \ }, + \ ], + \ } + \ ], + \ g:code_actions Execute(LSP should perform no action when no result): call ale#rename#HandleLSPResponse(1, { \ 'id': 3, From b29e9867e8729ee507197bb016e8e2c74b33dfc7 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Sun, 31 May 2020 11:17:13 +0200 Subject: [PATCH 3/3] Add test for LSP autoimport --- autoload/ale/completion.vim | 3 +- autoload/ale/rename.vim | 5 +- .../test_lsp_completion_parsing.vader | 70 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/autoload/ale/completion.vim b/autoload/ale/completion.vim index a173a0d4..b00fc6a3 100644 --- a/autoload/ale/completion.vim +++ b/autoload/ale/completion.vim @@ -533,6 +533,7 @@ function! ale#completion#ParseLSPCompletions(response) abort if has_key(l:item, 'additionalTextEdits') let l:text_changes = [] + for l:edit in l:item.additionalTextEdits let l:range = l:edit.range call add(l:text_changes, { @@ -549,7 +550,7 @@ function! ale#completion#ParseLSPCompletions(response) abort endfor let l:changes = [{ - \ 'fileName': expand('%:p'), + \ 'fileName': expand('#' . l:buffer . ':p'), \ 'textChanges': l:text_changes, \}] \ diff --git a/autoload/ale/rename.vim b/autoload/ale/rename.vim index 36a334d2..64952e63 100644 --- a/autoload/ale/rename.vim +++ b/autoload/ale/rename.vim @@ -90,8 +90,9 @@ function! s:getChanges(workspace_edit) abort return a:workspace_edit.changes elseif has_key(a:workspace_edit, 'documentChanges') let l:document_changes = [] - if type(a:workspace_edit.documentChanges) is v:t_dict && - \ has_key(a:workspace_edit.documentChanges, 'edits') + + if type(a:workspace_edit.documentChanges) is v:t_dict + \ && has_key(a:workspace_edit.documentChanges, 'edits') call add(l:document_changes, a:workspace_edit.documentChanges) elseif type(a:workspace_edit.documentChanges) is v:t_list let l:document_changes = a:workspace_edit.documentChanges diff --git a/test/completion/test_lsp_completion_parsing.vader b/test/completion/test_lsp_completion_parsing.vader index 1fdbbd96..e233c955 100644 --- a/test/completion/test_lsp_completion_parsing.vader +++ b/test/completion/test_lsp_completion_parsing.vader @@ -526,3 +526,73 @@ Execute(Should handle completion messages with the deprecated insertText attribu \ ], \ }, \ }) + +Execute(Should handle completion messages with additionalTextEdits): + AssertEqual + \ [ + \ { + \ 'word': 'next_callback', + \ 'menu': 'PlayTimeCallback', + \ 'info': '', + \ 'kind': 'v', + \ 'icase': 1, + \ 'user_data': json_encode({ + \ 'codeActions': [ + \ { + \ 'description': 'completion', + \ 'changes': [ + \ { + \ 'fileName': expand('#' . bufnr('') . ':p'), + \ 'textChanges': [ + \ { + \ 'start': { + \ 'line': 11, + \ 'offset': 2, + \ }, + \ 'end': { + \ 'line': 13, + \ 'offset': 4, + \ }, + \ 'newText': 'from "module" import next_callback', + \ }, + \ ], + \ }, + \ ], + \ }, + \ ], + \ }), + \ }, + \ ], + \ ale#completion#ParseLSPCompletions({ + \ 'id': 226, + \ 'jsonrpc': '2.0', + \ 'result': { + \ 'isIncomplete': v:false, + \ 'items': [ + \ { + \ 'detail': 'PlayTimeCallback', + \ 'filterText': 'next_callback', + \ 'insertText': 'next_callback', + \ 'insertTextFormat': 1, + \ 'kind': 6, + \ 'label': ' next_callback', + \ 'sortText': '3ee19999next_callback', + \ 'additionalTextEdits': [ + \ { + \ 'range': { + \ 'start': { + \ 'line': 10, + \ 'character': 1, + \ }, + \ 'end': { + \ 'line': 12, + \ 'character': 3, + \ }, + \ }, + \ 'newText': 'from "module" import next_callback', + \ }, + \ ], + \ }, + \ ], + \ }, + \ })