Skip to content

Commit

Permalink
feat: support rename close #35
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Jan 27, 2021
1 parent 78292f3 commit 3ab8e56
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lua/lspsaga/codeaction.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ local render_code_action_window = function (response)
local truncate_line = wrap.add_truncate_line(contents)
table.insert(contents,2,truncate_line)

contents_bufnr,contents_winid,_,border_winid = window.create_float_window(contents,'LspSagaCodeAction',config.border_style,true)
local border_opts = { border = config.border_style }
contents_bufnr,contents_winid,_,border_winid = window.create_float_window(contents,'LspSagaCodeAction',border_opts,true)
api.nvim_command('autocmd CursorMoved <buffer> lua require("lspsaga.codeaction").set_cursor()')

api.nvim_buf_add_highlight(contents_bufnr,-1,"LspSagaCodeActionTitle",0,0,-1)
Expand Down
3 changes: 2 additions & 1 deletion lua/lspsaga/diagnostic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ local function jump_to_entry(entry)
end

-- set curosr
local border_opts = { border = config.border_style }
api.nvim_win_set_cursor(0, {entry_line, entry_character})
local fb,fw,_,bw = window.create_float_window(diagnostic_message,'markdown',config.border_style,false)
local fb,fw,_,bw = window.create_float_window(diagnostic_message,'markdown',border_opts,false)

-- use a variable to control diagnostic floatwidnow
api.nvim_buf_set_var(0,"diagnostic_float_window",{fw,bw})
Expand Down
9 changes: 6 additions & 3 deletions lua/lspsaga/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ local render_finder_result= function ()
relative = "cursor",
style = "minimal",
}
M.contents_buf,M.contents_win,M.border_bufnr,M.border_win = window.create_float_window(contents,'plaintext',config.border_style,true,opts)
local border_opts = { border = config.border_style }
M.contents_buf,M.contents_win,M.border_bufnr,M.border_win = window.create_float_window(contents,'plaintext',border_opts,true,opts)
-- api.nvim_win_set_cursor(M.contens_buf,{2,1})
api.nvim_command('autocmd CursorMoved <buffer> lua require("lspsaga.provider").set_cursor()')
api.nvim_command('autocmd CursorMoved <buffer> lua require("lspsaga.provider").auto_open_preview()')
Expand Down Expand Up @@ -237,9 +238,10 @@ function M.auto_open_preview()
end
end

local border_opts = { border = config.border_style }
vim.defer_fn(function ()
close_auto_preview_win()
local _,cw,_,bw = window.create_float_window(content,buf_filetype,config.border_style,false,opts)
local _,cw,_,bw = window.create_float_window(content,buf_filetype,border_opts,false,opts)
api.nvim_win_set_var(0,'saga_finder_preview',{cw,bw})
end,10)
end
Expand Down Expand Up @@ -372,7 +374,8 @@ function M.preview_definition(timeout_ms)
relative = "cursor",
style = "minimal",
}
local contents_buf,contents_winid,_,border_winid = window.create_float_window(content,filetype,config.border_style,false,opts)
local border_opts = { border = config.border_style }
local contents_buf,contents_winid,_,border_winid = window.create_float_window(content,filetype,border_opts,false,opts)
vim.lsp.util.close_preview_autocmd({"CursorMoved", "CursorMovedI", "BufHidden", "BufLeave"},
border_winid)
vim.lsp.util.close_preview_autocmd({"CursorMoved", "CursorMovedI", "BufHidden", "BufLeave"},
Expand Down
56 changes: 56 additions & 0 deletions lua/lspsaga/rename.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
local lsp,util,api = vim.lsp,vim.lsp.util,vim.api
local window = require('lspsaga.window')
local config = require('lspsaga').config_values

local unique_name = 'textDocument-rename'
local pos = {}

local close_rename_win = function()
local has,winids = pcall(api.nvim_win_get_var,0,unique_name)
if has then
window.nvim_close_valid_window(winids)
api.nvim_command('stopinsert')
api.nvim_win_set_cursor(0,pos)
pos = {}
end
end

local rename = function()
-- if exist a rename float win close it.
close_rename_win()
pos[1],pos[2] = vim.fn.getpos('.')[2],vim.fn.getpos('.')[3]
local opts = {
height = 2,
width = 20,
border_text = ''
}
local border_opts = {
border = config.border_style,
title = 'New name'
}
local cb,cw,_,bw = window.create_float_window({},'',border_opts,true,opts)

api.nvim_buf_set_option(cb,'modifiable',true)
api.nvim_command('startinsert')
api.nvim_win_set_var(0,unique_name,{cw,bw})
api.nvim_command('inoremap <buffer><silent><cr> <cmd>lua require("lspsaga.rename").do_rename()<CR>')
end

local do_rename = function()
local new_name = vim.fn.getline('.')
close_rename_win()
local params = util.make_position_params()
local current_name = vim.fn.expand('<cword>')
if not (new_name and #new_name > 0) or new_name == current_name then
return
end
params.newName = new_name
lsp.buf_request(0,'textDocument/rename', params)
end


return {
rename = rename,
do_rename = do_rename
}

3 changes: 2 additions & 1 deletion lua/lspsaga/signaturehelp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ local function focusable_preview(unique_name, fn)
local truncate_line = wrap.add_truncate_line(contents)
table.insert(contents,2,truncate_line)
end
local cb,cw,_,bw = window.create_float_window(contents,filetype,config.border_style,false)
local border_opts = { border = config.border_style }
local cb,cw,_,bw = window.create_float_window(contents,filetype,border_opts,false)
util.close_preview_autocmd({"CursorMoved", "CursorMovedI", "BufHidden", "BufLeave"}, cw)
util.close_preview_autocmd({"CursorMoved", "CursorMovedI", "BufHidden", "BufLeave"}, bw)
return cb,cw
Expand Down
1 change: 1 addition & 0 deletions lua/lspsaga/syntax.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local api = vim.api

function M.add_highlight()
api.nvim_command("hi LspFloatWinBorder guifg=black")
api.nvim_command("hi LspSagaBorderTitle guifg=orange gui=bold")
api.nvim_command("hi def link TargetWord Error")
api.nvim_command("hi def link ReferencesCount Title")
api.nvim_command("hi def link DefinitionCount Title")
Expand Down
22 changes: 16 additions & 6 deletions lua/lspsaga/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ local function open_shadow_win()
return shadow_bufnr,shadow_winid
end

local function create_float_boder(contents,border,opts)
local function create_float_boder(contents,border_opts,opts)
local win_width,win_height,border_option = make_border_option(contents,opts)
local border = border_opts.border or 1
local title = border_opts.title or ''

local top_left = border_style[border].top_left
local top_mid = border_style[border].top_mid
Expand All @@ -117,7 +119,7 @@ local function create_float_boder(contents,border,opts)
local bottom_left= border_style[border].bottom_left
local bottom_right = border_style[border].bottom_right
-- set border
local top = top_left .. vim.fn["repeat"](top_mid, win_width-2) ..top_right
local top = top_left .. title .. vim.fn["repeat"](top_mid, win_width-2-#title) ..top_right
local mid = mid_line .. vim.fn["repeat"](" ", win_width-2) .. mid_line
local bot = bottom_left .. vim.fn["repeat"](top_mid, win_width-2) .. bottom_right
local lines = {top}
Expand All @@ -133,6 +135,10 @@ local function create_float_boder(contents,border,opts)
api.nvim_buf_set_option(border_bufnr, 'modifiable', false)
api.nvim_buf_set_option(border_bufnr, 'bufhidden', 'wipe')

if string.len(title) ~= 0 then
api.nvim_buf_add_highlight(border_bufnr,-1,'LspSagaBorderTitle',0,3,#title+3)
end

-- create border
local border_winid = api.nvim_open_win(border_bufnr, false, border_option)
api.nvim_win_set_option(border_winid,"winhl","Normal:LspFloatWinBorder")
Expand Down Expand Up @@ -165,7 +171,7 @@ function M.create_float_contents(contents,filetype,enter,opts)
return contents_bufnr, contents_winid
end

function M.create_float_window(contents,filetype,border,enter,opts)
function M.create_float_window(contents,filetype,border_opts,enter,opts)
local _,_,border_option = make_border_option(contents,opts)
local contents_option= border_option
contents_option.width = border_option.width - 2
Expand All @@ -179,11 +185,11 @@ function M.create_float_window(contents,filetype,border,enter,opts)

if not enter then
local contents_bufnr,contents_winid = M.create_float_contents(contents,filetype,enter,contents_option)
local border_bufnr,border_winid = create_float_boder(contents,border,opts)
local border_bufnr,border_winid = create_float_boder(contents,border_opts,opts)
return contents_bufnr,contents_winid,border_bufnr,border_winid
end

local border_bufnr,border_winid = create_float_boder(contents,border,opts)
local border_bufnr,border_winid = create_float_boder(contents,border_opts,opts)
local contents_bufnr,contents_winid = M.create_float_contents(contents,filetype,enter,border_option)
return contents_bufnr,contents_winid,border_bufnr,border_winid
end
Expand Down Expand Up @@ -258,8 +264,12 @@ function M.fancy_floating_markdown(contents, opts)
table.insert(stripped,2,truncate_line)
end

local border_opts = {
border = opts.border_style
}

-- Make the floating window.
local contents_bufnr,contents_winid,border_bufnr,border_winid = M.create_float_window(stripped,'sagahover',opts.border_style,false,opts)
local contents_bufnr,contents_winid,border_bufnr,border_winid = M.create_float_window(stripped,'sagahover',border_opts,false,opts)

api.nvim_buf_add_highlight(contents_bufnr,-1,'LspSagaDocTruncateLine',1,0,-1)

Expand Down

0 comments on commit 3ab8e56

Please sign in to comment.