Skip to content

Commit

Permalink
optimize rename logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Aug 7, 2023
1 parent eb3f31e commit 63e7cb3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lua/LspUI/rename/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ local is_initialized = false
-- init for the rename
M.init = function()
if not config.options.rename.enable then
lib_notify.Info("rename is not enabled!")
return
end

Expand All @@ -32,17 +31,18 @@ end
-- run of rename
M.run = function()
if not config.options.rename.enable then
lib_notify.Info("rename is not enabled!")
return
end

local clients = util.get_clients()
local current_buffer = api.nvim_get_current_buf()
local clients = util.get_clients(current_buffer)
if clients == nil then
-- if no valid client, step into here
return
end

local current_win = api.nvim_get_current_win()
local current_buffer = api.nvim_get_current_buf()

local old_name = fn.expand("<cword>")

Expand All @@ -61,7 +61,7 @@ M.run = function()

local new_window_wrap = windows.new_window(new_buffer)

windows.set_width_window(new_window_wrap, 20)
windows.set_width_window(new_window_wrap, fn.strcharlen(old_name) + 3)
windows.set_height_window(new_window_wrap, 1)
windows.set_enter_window(new_window_wrap, true)
windows.set_anchor_window(new_window_wrap, "NW")
Expand Down
7 changes: 5 additions & 2 deletions lua/LspUI/rename/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ local lib_util = require("LspUI.lib.util")
local M = {}

-- get all valid clients of rename
--- @param buffer_id integer
--- @return table|nil clients array or nil
M.get_clients = function()
local clients = lsp.get_clients({ method = rename_feature })
M.get_clients = function(buffer_id)
-- note: we need get lsp clients attached to current buffer
local clients = lsp.get_clients({ bufnr = buffer_id, method = rename_feature })
return #clients == 0 and nil or clients
end

Expand Down Expand Up @@ -54,6 +56,7 @@ M.do_rename = function(id, clients, buffer, position_param)
if not client then
return
end
-- TODO: client.supports_method is not listed by document
if client.supports_method(prepare_rename_feature) then
M.prepare_rename(
client,
Expand Down

0 comments on commit 63e7cb3

Please sign in to comment.