Skip to content

Commit

Permalink
rename code simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Feb 4, 2025
1 parent 10fa547 commit 955f757
Showing 1 changed file with 71 additions and 67 deletions.
138 changes: 71 additions & 67 deletions lua/LspUI/rename/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ local lib_notify = require("LspUI.lib.notify")
local lib_util = require("LspUI.lib.util")
local lib_windows = require("LspUI.lib.windows")

--- @alias LspUI_prepare_rename_res lsp.Range | { range: lsp.Range, placeholder: string } | { defaultBehavior: boolean } | nil

-- TODO: add progress support !!
-- maybe no need to implemention, because rename is a simple operation

local M = {}

-- get all valid clients of rename
--- @param buffer_id integer
--- @return vim.lsp.Client[]? clients array or nil
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 })
local clients = lsp.get_clients({
bufnr = buffer_id,
method = rename_feature,
})
if vim.tbl_isempty(clients) then
return nil
end
Expand Down Expand Up @@ -43,22 +50,34 @@ end
--- @param client vim.lsp.Client lsp client instance, must be element of func `get_clients`
--- @param buffer_id integer buffer id
--- @param position_param lsp.PrepareRenameParams this param must be generated by `vim.lsp.util.make_position_params`
--- @param callback function
--- @param callback fun(result:LspUI_prepare_rename_res?)
M.prepare_rename = function(client, buffer_id, position_param, callback)
vim.schedule(function()
local function __tmp()
client.request(
prepare_rename_feature,
position_param,
function(err, result)
if err or result == nil then
callback(false)
--- @type lsp.Handler
--- @diagnostic disable-next-line: unused-local
function(err, result, ctx, _)
if err then
lib_notify.Error(
"error code:" .. err.code .. ", " .. err.message
)

return
end
--- @type LspUI_prepare_rename_res
local res = result
if result == nil then
callback()
return
end
callback(true)
callback(res)
end,
buffer_id
)
end)
end
vim.schedule(__tmp)
end

-- do rename, a wrap function for prepare_rename and rename
Expand All @@ -67,45 +86,35 @@ end
--- @param buffer_id integer buffer id
--- @param position_param lsp.PrepareRenameParams|lsp.RenameParams this param must be generated by `vim.lsp.util.make_position_params`, has newname attribute
M.do_rename = function(id, clients, buffer_id, position_param)
local function next_rename()
local next_id, _ = next(clients, id)
M.do_rename(next_id, clients, buffer_id, position_param)
end

local async = uv.new_async(function()
local client = clients[id]
-- when client is nil, return
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,
buffer_id,
--- @cast position_param lsp.PrepareRenameParams
position_param,
-- result is true, that is preparename is ok
--- @param result boolean
-- we no need to resuse the result, we just use it todetect whether we can do rename
function(result)
if result then
--- @cast position_param lsp.RenameParams
M.rename(client, buffer_id, position_param, function()
local next_id, _ = next(clients, id)
M.do_rename(
next_id,
clients,
buffer_id,
position_param
)
end)
else
local next_id, _ = next(clients, id)
M.do_rename(next_id, clients, buffer_id, position_param)
if not result then
next_rename()
return
end
--- @cast position_param lsp.RenameParams
M.rename(client, buffer_id, position_param, next_rename)
end
)
else
--- @cast position_param lsp.RenameParams
M.rename(client, buffer_id, position_param, function()
local next_id, _ = next(clients, id)
M.do_rename(next_id, clients, buffer_id, position_param)
end)
M.rename(client, buffer_id, position_param, next_rename)
end
end)

Expand Down Expand Up @@ -150,15 +159,10 @@ M.render = function(clients, buffer_id, current_win, old_name)
--- @cast old_name string
old_name,
})
api.nvim_set_option_value("filetype", "LspUI-rename", {
buf = new_buffer,
})
api.nvim_set_option_value("modifiable", true, {
buf = new_buffer,
})
api.nvim_set_option_value("bufhidden", "wipe", {
buf = new_buffer,
})

api.nvim_set_option_value("filetype", "LspUI-rename", { buf = new_buffer })
api.nvim_set_option_value("modifiable", true, { buf = new_buffer })
api.nvim_set_option_value("bufhidden", "wipe", { buf = new_buffer })

local new_window_wrap = lib_windows.new_window(new_buffer)

Expand Down Expand Up @@ -187,12 +191,14 @@ M.render = function(clients, buffer_id, current_win, old_name)
})

if config.options.rename.auto_select then
vim.cmd([[normal! V]])
api.nvim_feedkeys(
api.nvim_replace_termcodes("<C-g>", true, true, true),
"n",
true
)
api.nvim_win_call(window_id, function()
vim.cmd([[normal! V]])
api.nvim_feedkeys(
api.nvim_replace_termcodes("<C-g>", true, true, true),
"n",
true
)
end)
end

-- keybinding and autocommand
Expand Down Expand Up @@ -260,34 +266,32 @@ M.keybinding_autocmd = function(
}
)

local insert_change_id = -1
insert_change_id = api.nvim_create_autocmd(
{ "TextChanged", "TextChangedI" },
{
buffer = new_buffer,
callback = function()
local now_name = api.nvim_get_current_line()
local len = calculate_length(now_name)
api.nvim_win_set_config(window_id, {
width = len,
})
end,
local autocmd_group_id =
vim.api.nvim_create_augroup("LspUI-rename_autocmd_group", {
clear = true,
})

desc = lib_util.command_desc(
"automatically lengthen the rename input box"
),
}
)
api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
group = autocmd_group_id,
buffer = new_buffer,
callback = function()
local now_name = api.nvim_get_current_line()
local len = calculate_length(now_name)
api.nvim_win_set_config(window_id, {
width = len,
})
end,
desc = lib_util.command_desc(
"automatically lengthen the rename input box"
),
})

-- auto command: auto close window, when focus leave rename float window
api.nvim_create_autocmd("WinLeave", {
group = autocmd_group_id,
buffer = new_buffer,
once = true,
callback = function()
-- delete insert change event
pcall(api.nvim_del_autocmd, insert_change_id)

-- close window
close_window(window_id)
end,
desc = lib_util.command_desc(
Expand Down

0 comments on commit 955f757

Please sign in to comment.