|
|
@ -1,3 +1,8 @@
|
|
|
|
|
|
|
|
-- the preview and cache are copy from
|
|
|
|
|
|
|
|
-- smjonas/inc-renamer.nvim
|
|
|
|
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
|
|
|
|
|
|
|
-- inplace rename are from neovim vim.lsp.buf.rename
|
|
|
|
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
local M = {}
|
|
|
|
local util = require('navigator.util')
|
|
|
|
local util = require('navigator.util')
|
|
|
|
local log = util.log
|
|
|
|
local log = util.log
|
|
|
@ -7,8 +12,6 @@ local vfn = vim.fn
|
|
|
|
local make_position_params = vim.lsp.util.make_position_params
|
|
|
|
local make_position_params = vim.lsp.util.make_position_params
|
|
|
|
local rename_group = api.nvim_create_augroup('nav-rename', {})
|
|
|
|
local rename_group = api.nvim_create_augroup('nav-rename', {})
|
|
|
|
|
|
|
|
|
|
|
|
-- local rename_prompt = 'Rename -> '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
M.default_config = {
|
|
|
|
M.default_config = {
|
|
|
|
cmd_name = 'IncRename',
|
|
|
|
cmd_name = 'IncRename',
|
|
|
|
hl_group = 'Substitute',
|
|
|
|
hl_group = 'Substitute',
|
|
|
@ -85,6 +88,7 @@ local function hash(a, b, c)
|
|
|
|
return cantor(a, cantor(b, c))
|
|
|
|
return cantor(a, cantor(b, c))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
local function cache_lines(result)
|
|
|
|
local function cache_lines(result)
|
|
|
|
local cached_lines = {}
|
|
|
|
local cached_lines = {}
|
|
|
|
local exists = {}
|
|
|
|
local exists = {}
|
|
|
@ -127,6 +131,8 @@ local function cache_lines(result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Get positions of LSP reference symbols
|
|
|
|
-- Get positions of LSP reference symbols
|
|
|
|
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
|
|
|
local function fetch_lsp_references(bufnr, lsp_params, callback)
|
|
|
|
local function fetch_lsp_references(bufnr, lsp_params, callback)
|
|
|
|
local clients = vim.lsp.get_active_clients({
|
|
|
|
local clients = vim.lsp.get_active_clients({
|
|
|
|
bufnr = bufnr,
|
|
|
|
bufnr = bufnr,
|
|
|
@ -163,6 +169,8 @@ local function fetch_lsp_references(bufnr, lsp_params, callback)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
|
|
|
local function teardown(switch_buffer)
|
|
|
|
local function teardown(switch_buffer)
|
|
|
|
state.should_fetch_references = true
|
|
|
|
state.should_fetch_references = true
|
|
|
|
state.cached_lines = nil
|
|
|
|
state.cached_lines = nil
|
|
|
@ -187,6 +195,7 @@ end
|
|
|
|
|
|
|
|
|
|
|
|
-- Called when the user is still typing the command or the command arguments
|
|
|
|
-- Called when the user is still typing the command or the command arguments
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
|
|
|
local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
log(opts, preview_ns, preview_buf)
|
|
|
|
log(opts, preview_ns, preview_buf)
|
|
|
|
local new_name = opts.args
|
|
|
|
local new_name = opts.args
|
|
|
@ -194,15 +203,6 @@ local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
state.new_name = new_name
|
|
|
|
state.new_name = new_name
|
|
|
|
vim.v.errmsg = ''
|
|
|
|
vim.v.errmsg = ''
|
|
|
|
|
|
|
|
|
|
|
|
-- if state.input_win_id and api.nvim_win_is_valid(state.input_win_id) then
|
|
|
|
|
|
|
|
-- -- Add a space so the cursor can be placed after the last character
|
|
|
|
|
|
|
|
-- api.nvim_buf_set_lines(state.input_bufnr, 0, -1, false, { new_name .. ' ' })
|
|
|
|
|
|
|
|
-- local _, cmd_prefix_len = vim.fn.getcmdline():find('^%s*' .. M.config.cmd_name .. '%s*')
|
|
|
|
|
|
|
|
-- local cursor_pos = vim.fn.getcmdpos() - cmd_prefix_len - 1
|
|
|
|
|
|
|
|
-- -- Create a fake cursor in the input buffer
|
|
|
|
|
|
|
|
-- api.nvim_buf_add_highlight(state.input_bufnr, preview_ns, 'Visual', 0, cursor_pos, cursor_pos + 1)
|
|
|
|
|
|
|
|
-- end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if state.should_fetch_references then
|
|
|
|
if state.should_fetch_references then
|
|
|
|
fetch_lsp_references(preview_buf, state.lsp_params, function()
|
|
|
|
fetch_lsp_references(preview_buf, state.lsp_params, function()
|
|
|
|
incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
@ -211,7 +211,7 @@ local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
|
|
|
|
|
|
|
|
if not state.cached_lines then
|
|
|
|
if not state.cached_lines then
|
|
|
|
log('lsp references not fetched yet')
|
|
|
|
log('lsp references not fetched yet')
|
|
|
|
return M.input_buffer ~= nil and 2
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function apply_highlights_fn(bufnr, line_nr, line_info)
|
|
|
|
local function apply_highlights_fn(bufnr, line_nr, line_info)
|
|
|
@ -235,7 +235,14 @@ local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
api.nvim_buf_set_lines(bufnr or opts.bufnr, line_nr, line_nr + 1, false, { updated_line })
|
|
|
|
api.nvim_buf_set_lines(bufnr or opts.bufnr, line_nr, line_nr + 1, false, { updated_line })
|
|
|
|
|
|
|
|
|
|
|
|
for _, hl_pos in ipairs(highlight_positions) do
|
|
|
|
for _, hl_pos in ipairs(highlight_positions) do
|
|
|
|
api.nvim_buf_add_highlight(bufnr or opts.bufnr, preview_ns, 'Visual', line_nr, hl_pos.start_col, hl_pos.end_col)
|
|
|
|
api.nvim_buf_add_highlight(
|
|
|
|
|
|
|
|
bufnr or opts.bufnr,
|
|
|
|
|
|
|
|
preview_ns,
|
|
|
|
|
|
|
|
M.default_config.hl_group,
|
|
|
|
|
|
|
|
line_nr,
|
|
|
|
|
|
|
|
hl_pos.start_col,
|
|
|
|
|
|
|
|
hl_pos.end_col
|
|
|
|
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
@ -246,11 +253,10 @@ local function incremental_rename_preview(opts, preview_ns, preview_buf)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
state.preview_ns = preview_ns
|
|
|
|
state.preview_ns = preview_ns
|
|
|
|
return 2
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Sends a LSP rename request and optionally displays a message to the user showing
|
|
|
|
-- a function from smjonas/inc-rename.nvim
|
|
|
|
-- how many instances were renamed in how many files
|
|
|
|
-- https://github.com/smjonas/inc-rename.nvim/blob/main/lua/inc_rename/init.lua
|
|
|
|
local function perform_lsp_rename(new_name, params)
|
|
|
|
local function perform_lsp_rename(new_name, params)
|
|
|
|
params = params or make_position_params()
|
|
|
|
params = params or make_position_params()
|
|
|
|
params.newName = new_name
|
|
|
|
params.newName = new_name
|
|
|
@ -379,7 +385,8 @@ M.rename_preview = function()
|
|
|
|
vim.lsp.buf.rename()
|
|
|
|
vim.lsp.buf.rename()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- rename withou floating window
|
|
|
|
-- rename without floating window
|
|
|
|
|
|
|
|
-- a moodify version of neovim vim.lsp.buf.rename
|
|
|
|
function M.rename_inplace(new_name, options)
|
|
|
|
function M.rename_inplace(new_name, options)
|
|
|
|
options = options or {}
|
|
|
|
options = options or {}
|
|
|
|
state.confrim = nil
|
|
|
|
state.confrim = nil
|
|
|
|