Skip to content

Commit

Permalink
add config for rename and init rename
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Aug 6, 2023
1 parent a21de64 commit eb3f31e
Show file tree
Hide file tree
Showing 13 changed files with 374 additions and 24 deletions.
23 changes: 0 additions & 23 deletions lua/LSPUI/lib/lsp.lua

This file was deleted.

5 changes: 5 additions & 0 deletions lua/LspUI/api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- note: This file is used to expose api externally

local M = {}

return M
67 changes: 67 additions & 0 deletions lua/LspUI/command.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
local api, fn = vim.api, vim.fn
local notify = require("LspUI.lib.notify")
local util = require("LspUI.lib.util")

local command_store = {}

-- get command keys
local function command_keys()
return vim.tbl_keys(command_store)
end

-- the default function when command `LspUI` execute
local function default_exec()
notify.Info(string.format("Hello, version is %s", util.version()))
end

-- exec run function
local function exec(key, args)
if key == nil then
default_exec()
else
if command_store[key] then
pcall(command_store[key].run, args)
else
notify.Warn(string.format("command %s not exist!"))
end
end
end

local M = {}

-- init for the command
M.init = function()
api.nvim_create_user_command("LspUI", function(args)
exec(unpack(args.fargs))
end, {
range = true,
nargs = "*",
complete = function(_, cmdline, _)
local cmd = fn.split(cmdline)
local key_list = command_keys()

if #cmd <= 1 then
return key_list
end

local args = vim.tbl_get(command_store, cmd[2], "args")
if not args then
return {}
end

return args
end,
})
end

-- this function register command
--- @param command_key string
--- @param run function
--- @param args table
M.register_command = function(command_key, run, args)
command_store[command_key] = command_store[command_key] or {}
command_store[command_key].run = run
command_store[command_key].args = args
end

return M
38 changes: 38 additions & 0 deletions lua/LspUI/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local lib_notify = require("LspUI.lib.notify")

local default_rename_config = {
enable = true,
command_enable = true,
auto_select = true,
key_binding = {
exec = "<CR>",
quit = "<ESC>",
},
}

local default_config = {
rename = default_rename_config,
}

-- Prevent plugins from being initialized multiple times
local is_already_init = false

local M = {}

-- LspUI plugin init function
-- you need to pass a table
--- @param config table
M.setup = function(config)
-- check plugin whether has initialized
if is_already_init then
-- TODO:whether retain this
lib_notify.Warn("you have already initialized the plugin config!")
return
end

config = config or {}
M.options = vim.tbl_deep_extend("force", default_config, config)
is_already_init = true
end

return M
17 changes: 17 additions & 0 deletions lua/LspUI/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local config = require("LspUI.config")
local api = require("LspUI.api")
local modules = require("LspUI.modules")
local command = require("LspUI.command")

return {
-- init `LspUI` plugin
--- @param user_config table user's plugin config
setup = function(user_config)
config.setup(user_config)
command.init()
for _, module in pairs(modules) do
module.init()
end
end,
api = api,
}
File renamed without changes.
28 changes: 28 additions & 0 deletions lua/LspUI/lib/lsp.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local lsp, api = vim.lsp, vim.api

local notify = require("LspUI.lib.notify")

local M = {}

-- check whether there is an active lsp client
-- note: this function now should not be called!!
--- @param is_notify boolean whether notify, default not
M.is_lsp_active = function(is_notify)
is_notify = is_notify or false
local current_buf = api.nvim_get_current_buf()

local clients = lsp.get_clients({
bufnr = current_buf,
})

if vim.tbl_isempty(clients) then
if is_notify then
local message = string.format("not found lsp client on this buffer, id is %d", current_buf)
notify.Warn(message)
end
return false
end
return true
end

return M
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion lua/LSPUI/lib/windows.lua → lua/LspUI/lib/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ end

-- create a windows config,
-- here use similar c processing logic
--- @param buffer_id integer buffer's id, if nil, it will be current buffer
--- @param buffer_id integer|nil buffer's id, if nil, it will be current buffer
--- @return table window_wrap a windows wrap config for other function to use
M.new_window = function(buffer_id)
local window_wrap = {
Expand Down
3 changes: 3 additions & 0 deletions lua/LspUI/modules.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return {
rename = require("LspUI.rename"),
}
88 changes: 88 additions & 0 deletions lua/LspUI/rename/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
local api, fn, lsp = vim.api, vim.fn, vim.lsp
local config = require("LspUI.config")
local lib_notify = require("LspUI.lib.notify")
local util = require("LspUI.rename.util")
local windows = require("LspUI.lib.windows")
local command = require("LspUI.command")

local M = {}

-- whether this module has initialized
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

if is_initialized then
return
end

is_initialized = true

-- register command
if config.options.rename.command_enable then
command.register_command("rename", M.run, {})
end
end

-- run of rename
M.run = function()
if not config.options.rename.enable then
return
end

local clients = util.get_clients()
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>")

-- TODO: this func maybe should not pass win id ?
local position_param = lsp.util.make_position_params(current_win)

-- Here we need to define window

local new_buffer = api.nvim_create_buf(false, true)

-- note: this must set before modifiable, when modifiable is false, this function will fail
api.nvim_buf_set_lines(new_buffer, 0, -1, false, { old_name })
api.nvim_buf_set_option(new_buffer, "filetype", "LspUI-rename")
api.nvim_buf_set_option(new_buffer, "modifiable", true)
api.nvim_buf_set_option(new_buffer, "bufhidden", "wipe")

local new_window_wrap = windows.new_window(new_buffer)

windows.set_width_window(new_window_wrap, 20)
windows.set_height_window(new_window_wrap, 1)
windows.set_enter_window(new_window_wrap, true)
windows.set_anchor_window(new_window_wrap, "NW")
windows.set_border_window(new_window_wrap, "rounded")
windows.set_focusable_window(new_window_wrap, true)
windows.set_relative_window(new_window_wrap, "cursor")
windows.set_col_window(new_window_wrap, 1)
windows.set_row_window(new_window_wrap, 1)
windows.set_style_window(new_window_wrap, "minimal")

local window_id = windows.display_window(new_window_wrap)

api.nvim_win_set_option(window_id, "winhighlight", "Normal:Normal")

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)
end

-- keybinding and autocommand
util.keybinding_autocmd(window_id, old_name, clients, current_buffer, new_buffer, position_param)
end

return M

0 comments on commit eb3f31e

Please sign in to comment.