Skip to content

Commit 2b65ba4

Browse files
committed
rename functionality
1 parent 6d46e76 commit 2b65ba4

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

lua/cosmic-ui/rename.lua

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,45 @@
1+
local lsp = vim.lsp
12
local utils = require('cosmic-ui.utils')
23

4+
local function rename_handler(...)
5+
local result
6+
local method
7+
local err = select(1, ...)
8+
local is_new = not select(4, ...) or type(select(4, ...)) ~= 'number'
9+
if is_new then
10+
method = select(3, ...).method
11+
result = select(2, ...)
12+
else
13+
method = select(2, ...)
14+
result = select(3, ...)
15+
end
16+
17+
if err then
18+
vim.notify(("Error running LSP query '%s': %s"):format(method, err), vim.log.levels.ERROR, {
19+
title = 'Cosmic-UI',
20+
})
21+
return
22+
end
23+
24+
local new_word = ''
25+
if result and result.changes then
26+
local msg = {}
27+
for f, c in pairs(result.changes) do
28+
new_word = c[1].newText
29+
table.insert(msg, ('%d changes -> %s'):format(#c, utils.get_relative_path(f)))
30+
end
31+
local currName = vim.fn.expand('<cword>')
32+
vim.notify(msg, vim.log.levels.INFO, { title = ('Rename: %s -> %s'):format(currName, new_word) })
33+
end
34+
35+
vim.lsp.handlers[method](...)
36+
end
37+
338
local function rename(popup_opts, opts)
439
opts = opts or {}
540
local Input = require('nui.input')
641
local event = require('nui.utils.autocmd').event
42+
local curr_name = vim.fn.expand('<cword>')
743
local default_popup_opts = {
844
position = {
945
row = 1,
@@ -29,12 +65,14 @@ local function rename(popup_opts, opts)
2965

3066
local default_opts = {
3167
prompt = '> ',
32-
default_value = vim.fn.expand('<cword>'),
33-
on_close = function()
34-
print('Input closed!')
35-
end,
36-
on_submit = function(value)
37-
print('You are ' .. value .. ' years old')
68+
default_value = curr_name,
69+
on_submit = function(new_name)
70+
if not (new_name and #new_name > 0) or new_name == curr_name then
71+
return
72+
end
73+
local params = lsp.util.make_position_params()
74+
params.newName = new_name
75+
lsp.buf_request(0, 'textDocument/rename', params, rename_handler)
3876
end,
3977
}
4078

lua/cosmic-ui/utils.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ M.merge = function(defaults, opts)
44
return vim.tbl_deep_extend('force', defaults, opts or {})
55
end
66

7+
M.get_relative_path = function(file_path)
8+
local plenary_path = require('plenary.path')
9+
local parsed_path, _ = file_path:gsub('file://', '')
10+
local path = plenary_path:new(parsed_path)
11+
local relative_path = path:make_relative(vim.fn.getcwd())
12+
return './' .. relative_path
13+
end
14+
715
-- Default backspace has inconsistent behavior, have to make our own (for now)
816
-- Taken from here:
917
-- https://github.com/neovim/neovim/issues/14116#issuecomment-976069244

readme.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
<h1 align="center">💫 Cosmic-UI</h1>
2+
3+
## Installation
4+
5+
```lua
6+
use({
7+
'CosmicNvim/cosmic-ui',
8+
requires = { 'MunifTanjim/nui.nvim', 'nvim-lua/plenary.nvim' },
9+
})
10+
11+
```

0 commit comments

Comments
 (0)