2019-09-12 20:53:23 +00:00
|
|
|
" Author: Jerko Steiner <jerko.steiner@gmail.com>
|
|
|
|
" Description: Rename symbol support for LSP / tsserver
|
|
|
|
|
|
|
|
let s:rename_map = {}
|
|
|
|
|
|
|
|
" Used to get the rename map in tests.
|
|
|
|
function! ale#rename#GetMap() abort
|
|
|
|
return deepcopy(s:rename_map)
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
" Used to set the rename map in tests.
|
|
|
|
function! ale#rename#SetMap(map) abort
|
|
|
|
let s:rename_map = a:map
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#rename#ClearLSPData() abort
|
|
|
|
let s:rename_map = {}
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
let g:ale_rename_tsserver_find_in_comments = get(g:, 'ale_rename_tsserver_find_in_comments')
|
|
|
|
let g:ale_rename_tsserver_find_in_strings = get(g:, 'ale_rename_tsserver_find_in_strings')
|
|
|
|
|
|
|
|
function! s:message(message) abort
|
|
|
|
call ale#util#Execute('echom ' . string(a:message))
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#rename#HandleTSServerResponse(conn_id, response) abort
|
|
|
|
if get(a:response, 'command', '') isnot# 'rename'
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
if !has_key(s:rename_map, a:response.request_seq)
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
let l:options = remove(s:rename_map, a:response.request_seq)
|
|
|
|
|
|
|
|
let l:old_name = l:options.old_name
|
|
|
|
let l:new_name = l:options.new_name
|
2019-09-12 20:53:23 +00:00
|
|
|
|
|
|
|
if get(a:response, 'success', v:false) is v:false
|
|
|
|
let l:message = get(a:response, 'message', 'unknown')
|
|
|
|
call s:message('Error renaming "' . l:old_name . '" to: "' . l:new_name
|
|
|
|
\ . '". Reason: ' . l:message)
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:changes = []
|
|
|
|
|
|
|
|
for l:response_item in a:response.body.locs
|
|
|
|
let l:filename = l:response_item.file
|
|
|
|
let l:text_changes = []
|
|
|
|
|
|
|
|
for l:loc in l:response_item.locs
|
|
|
|
call add(l:text_changes, {
|
|
|
|
\ 'start': {
|
|
|
|
\ 'line': l:loc.start.line,
|
|
|
|
\ 'offset': l:loc.start.offset,
|
|
|
|
\ },
|
|
|
|
\ 'end': {
|
|
|
|
\ 'line': l:loc.end.line,
|
|
|
|
\ 'offset': l:loc.end.offset,
|
|
|
|
\ },
|
|
|
|
\ 'newText': l:new_name,
|
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
call add(l:changes, {
|
|
|
|
\ 'fileName': l:filename,
|
|
|
|
\ 'textChanges': l:text_changes,
|
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if empty(l:changes)
|
|
|
|
call s:message('Error renaming "' . l:old_name . '" to: "' . l:new_name . '"')
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
call ale#code_action#HandleCodeAction(
|
|
|
|
\ {
|
|
|
|
\ 'description': 'rename',
|
|
|
|
\ 'changes': l:changes,
|
|
|
|
\ },
|
|
|
|
\ {
|
Dispatch textDocument/didChange after rename (2) (#4049)
* Dispatch textDocument/didChange after rename
Previously whenever we renamed a symbol that was referenced from other
files we'd just edit those files in the background, and the LSP wouldn't
know about these changes. If we tried to rename the same symbol again,
the renaming would fail. In some scenarios, the operation would just be
wrong. Here is an attempt to fix this issue.
I also noticed another bug when using Go with `gopls` LSP and the `gofmt`
fixer. Whenever the file was saved, the `gofmt` would run and reformat
the file. But it seems there was some kind of a race condition so I
disabled saving for now, and all of the modified files will be unsaved,
so the user should call `:wa` to save them. I personally like this even
better because I can inspect exactly what changes happened, and I
instantly see them in the other opened buffers, which was previously not
the case.
Fixes #3343, #3642, #3781.
* Address PR comments
* Remove mode tests in corner case tests
* Address PR comments
* Save after ALERename and ALEOrganizeImports
Also provide options to disable automatic saving, as well as instructions to
enable `set hidden` before doing that.
* Fix broken test
* Save only when !&hidden
* Update doc
* Update doc
* Add silent
2022-02-08 11:07:39 +00:00
|
|
|
\ 'conn_id': a:conn_id,
|
|
|
|
\ 'should_save': !&hidden,
|
2020-10-15 20:56:21 +00:00
|
|
|
\ },
|
|
|
|
\)
|
2019-09-12 20:53:23 +00:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale#rename#HandleLSPResponse(conn_id, response) abort
|
|
|
|
if has_key(a:response, 'id')
|
|
|
|
\&& has_key(s:rename_map, a:response.id)
|
2020-10-15 20:56:21 +00:00
|
|
|
let l:options = remove(s:rename_map, a:response.id)
|
2019-09-12 20:53:23 +00:00
|
|
|
|
|
|
|
if !has_key(a:response, 'result')
|
|
|
|
call s:message('No rename result received from server')
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2020-11-14 10:15:17 +00:00
|
|
|
let l:changes_map = ale#code_action#GetChanges(a:response.result)
|
2019-09-12 20:53:23 +00:00
|
|
|
|
2020-02-22 06:52:50 +00:00
|
|
|
if empty(l:changes_map)
|
2019-09-12 20:53:23 +00:00
|
|
|
call s:message('No changes received from server')
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
2020-11-14 10:15:17 +00:00
|
|
|
let l:changes = ale#code_action#BuildChangesList(l:changes_map)
|
2019-09-12 20:53:23 +00:00
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
call ale#code_action#HandleCodeAction(
|
|
|
|
\ {
|
|
|
|
\ 'description': 'rename',
|
|
|
|
\ 'changes': l:changes,
|
|
|
|
\ },
|
|
|
|
\ {
|
Dispatch textDocument/didChange after rename (2) (#4049)
* Dispatch textDocument/didChange after rename
Previously whenever we renamed a symbol that was referenced from other
files we'd just edit those files in the background, and the LSP wouldn't
know about these changes. If we tried to rename the same symbol again,
the renaming would fail. In some scenarios, the operation would just be
wrong. Here is an attempt to fix this issue.
I also noticed another bug when using Go with `gopls` LSP and the `gofmt`
fixer. Whenever the file was saved, the `gofmt` would run and reformat
the file. But it seems there was some kind of a race condition so I
disabled saving for now, and all of the modified files will be unsaved,
so the user should call `:wa` to save them. I personally like this even
better because I can inspect exactly what changes happened, and I
instantly see them in the other opened buffers, which was previously not
the case.
Fixes #3343, #3642, #3781.
* Address PR comments
* Remove mode tests in corner case tests
* Address PR comments
* Save after ALERename and ALEOrganizeImports
Also provide options to disable automatic saving, as well as instructions to
enable `set hidden` before doing that.
* Fix broken test
* Save only when !&hidden
* Update doc
* Update doc
* Add silent
2022-02-08 11:07:39 +00:00
|
|
|
\ 'conn_id': a:conn_id,
|
|
|
|
\ 'should_save': !&hidden,
|
2020-10-15 20:56:21 +00:00
|
|
|
\ },
|
|
|
|
\)
|
2019-09-12 20:53:23 +00:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
function! s:OnReady(line, column, options, linter, lsp_details) abort
|
2019-09-12 20:53:23 +00:00
|
|
|
let l:id = a:lsp_details.connection_id
|
|
|
|
|
|
|
|
if !ale#lsp#HasCapability(l:id, 'rename')
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:buffer = a:lsp_details.buffer
|
|
|
|
|
|
|
|
let l:Callback = a:linter.lsp is# 'tsserver'
|
|
|
|
\ ? function('ale#rename#HandleTSServerResponse')
|
|
|
|
\ : function('ale#rename#HandleLSPResponse')
|
|
|
|
|
|
|
|
call ale#lsp#RegisterCallback(l:id, l:Callback)
|
|
|
|
|
|
|
|
if a:linter.lsp is# 'tsserver'
|
|
|
|
let l:message = ale#lsp#tsserver_message#Rename(
|
|
|
|
\ l:buffer,
|
|
|
|
\ a:line,
|
|
|
|
\ a:column,
|
|
|
|
\ g:ale_rename_tsserver_find_in_comments,
|
|
|
|
\ g:ale_rename_tsserver_find_in_strings,
|
|
|
|
\)
|
|
|
|
else
|
|
|
|
" Send a message saying the buffer has changed first, or the
|
|
|
|
" rename position probably won't make sense.
|
|
|
|
call ale#lsp#NotifyForChanges(l:id, l:buffer)
|
|
|
|
|
|
|
|
let l:message = ale#lsp#message#Rename(
|
|
|
|
\ l:buffer,
|
|
|
|
\ a:line,
|
|
|
|
\ a:column,
|
2020-10-15 20:56:21 +00:00
|
|
|
\ a:options.new_name
|
2019-09-12 20:53:23 +00:00
|
|
|
\)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:request_id = ale#lsp#Send(l:id, l:message)
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
let s:rename_map[l:request_id] = a:options
|
2019-09-12 20:53:23 +00:00
|
|
|
endfunction
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
function! s:ExecuteRename(linter, options) abort
|
2019-09-12 20:53:23 +00:00
|
|
|
let l:buffer = bufnr('')
|
|
|
|
let [l:line, l:column] = getpos('.')[1:2]
|
|
|
|
|
|
|
|
if a:linter.lsp isnot# 'tsserver'
|
|
|
|
let l:column = min([l:column, len(getline(l:line))])
|
|
|
|
endif
|
|
|
|
|
2020-10-15 20:56:21 +00:00
|
|
|
let l:Callback = function('s:OnReady', [l:line, l:column, a:options])
|
2019-09-12 20:53:23 +00:00
|
|
|
call ale#lsp_linter#StartLSP(l:buffer, a:linter, l:Callback)
|
|
|
|
endfunction
|
|
|
|
|
2020-11-21 19:00:53 +00:00
|
|
|
function! ale#rename#Execute() abort
|
2019-09-12 20:53:23 +00:00
|
|
|
let l:lsp_linters = []
|
|
|
|
|
|
|
|
for l:linter in ale#linter#Get(&filetype)
|
|
|
|
if !empty(l:linter.lsp)
|
|
|
|
call add(l:lsp_linters, l:linter)
|
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
|
|
|
if empty(l:lsp_linters)
|
|
|
|
call s:message('No active LSPs')
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:old_name = expand('<cword>')
|
|
|
|
let l:new_name = ale#util#Input('New name: ', l:old_name)
|
|
|
|
|
|
|
|
if empty(l:new_name)
|
|
|
|
call s:message('New name cannot be empty!')
|
|
|
|
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
|
|
|
|
for l:lsp_linter in l:lsp_linters
|
2020-10-15 20:56:21 +00:00
|
|
|
call s:ExecuteRename(l:lsp_linter, {
|
|
|
|
\ 'old_name': l:old_name,
|
|
|
|
\ 'new_name': l:new_name,
|
|
|
|
\})
|
2019-09-12 20:53:23 +00:00
|
|
|
endfor
|
|
|
|
endfunction
|