diff options
author | Zi How Poh <z@pzpz.dev> | 2021-09-08 23:00:15 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-08 17:00:15 +0200 |
commit | c1f573fbc94aecd0f5841f7eb671be1a0a29758c (patch) | |
tree | f0873a8e127974cad8b703a06be27087666130d6 /runtime/lua/vim/lsp/buf.lua | |
parent | 11289ad733e7b3f72ee583ccbfe982a78e099c6c (diff) | |
download | rneovim-c1f573fbc94aecd0f5841f7eb671be1a0a29758c.tar.gz rneovim-c1f573fbc94aecd0f5841f7eb671be1a0a29758c.tar.bz2 rneovim-c1f573fbc94aecd0f5841f7eb671be1a0a29758c.zip |
feat(lsp): support textDocument/prepareRename (#15514)
Diffstat (limited to 'runtime/lua/vim/lsp/buf.lua')
-rw-r--r-- | runtime/lua/vim/lsp/buf.lua | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua index 264d7c0247..8bfcd90f12 100644 --- a/runtime/lua/vim/lsp/buf.lua +++ b/runtime/lua/vim/lsp/buf.lua @@ -249,13 +249,34 @@ end ---@param new_name (string) If not provided, the user will be prompted for a new ---name using |input()|. function M.rename(new_name) - -- TODO(ashkan) use prepareRename - -- * result: [`Range`](#range) \| `{ range: Range, placeholder: string }` \| `null` describing the range of the string to rename and optionally a placeholder text of the string content to be renamed. If `null` is returned then it is deemed that a 'textDocument/rename' request is not valid at the given position. local params = util.make_position_params() - new_name = new_name or npcall(vfn.input, "New Name: ", vfn.expand('<cword>')) - if not (new_name and #new_name > 0) then return end - params.newName = new_name - request('textDocument/rename', params) + local function prepare_rename(err, result) + if err == nil and result == nil then + vim.notify('nothing to rename', vim.log.levels.INFO) + return + end + if result and result.placeholder then + new_name = new_name or npcall(vfn.input, "New Name: ", result.placeholder) + elseif result and result.start and result['end'] and + result.start.line == result['end'].line then + local line = vfn.getline(result.start.line+1) + local start_char = result.start.character+1 + local end_char = result['end'].character + new_name = new_name or npcall(vfn.input, "New Name: ", string.sub(line, start_char, end_char)) + else + -- fallback to guessing symbol using <cword> + -- + -- this can happen if the language server does not support prepareRename, + -- returns an unexpected response, or requests for "default behavior" + -- + -- see https://microsoft.github.io/language-server-protocol/specification#textDocument_prepareRename + new_name = new_name or npcall(vfn.input, "New Name: ", vfn.expand('<cword>')) + end + if not (new_name and #new_name > 0) then return end + params.newName = new_name + request('textDocument/rename', params) + end + request('textDocument/prepareRename', params, prepare_rename) end --- Lists all the references to the symbol under the cursor in the quickfix window. |