diff options
author | Jerko Steiner <jerko.steiner@gmail.com> | 2020-02-22 07:52:50 +0100 |
---|---|---|
committer | Jerko Steiner <jerko.steiner@gmail.com> | 2020-05-31 11:00:53 +0200 |
commit | b339a8bfa0ebbe42e3f824e85f6900101cee8244 (patch) | |
tree | 7047f99b260b8b1644306c615705ca649bd5c197 | |
parent | 4062b056691daca9593e01f67026e4199cd8da87 (diff) | |
download | ale-b339a8bfa0ebbe42e3f824e85f6900101cee8244.zip |
Add support for rename (documentChanges)
-rw-r--r-- | autoload/ale/rename.vim | 32 | ||||
-rw-r--r-- | 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, |