1+ local lsp = vim .lsp
12local 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+
338local 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
0 commit comments