Skip to content

Commit

Permalink
feat(nvim): LspFloatToggle
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyinzuo committed Apr 8, 2024
1 parent de64441 commit 8359e8c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
33 changes: 33 additions & 0 deletions root/.config/nvim/lua/lsp/diagnostic.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--- 自动浮窗展示diagnostic
local function create_floating_window_autocmd()
vim.api.nvim_create_augroup("nvim_lsp_floating_window", {})
vim.api.nvim_create_autocmd("CursorHold", {
group = "nvim_lsp_floating_window",
callback = function()
local opts = {
focusable = false,
close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
border = 'rounded',
source = 'always',
prefix = ' ',
scope = 'cursor',
}
vim.diagnostic.open_float(nil, opts)
end
})
end

vim.g.nvim_lsp_diagnostic_enable_auto_floating_window = vim.fn.get(vim.g, 'nvim_lsp_diagnostic_enable_auto_floating_window', false)
local function toggle_auto_floating_window()
if vim.g.nvim_lsp_diagnostic_enable_auto_floating_window then
vim.api.nvim_del_augroup_by_name("nvim_lsp_floating_window")
else
create_floating_window_autocmd()
end
vim.g.nvim_lsp_diagnostic_enable_auto_floating_window = not vim.g.nvim_lsp_diagnostic_enable_auto_floating_window
end
vim.api.nvim_create_user_command('LspFloatToggle', toggle_auto_floating_window, {})

if vim.g.nvim_lsp_diagnostic_enable_auto_floating_window then
create_floating_window_autocmd()
end
17 changes: 2 additions & 15 deletions root/.config/nvim/lua/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ local function setup_lsp(on_attach, capabilities)
end

function M.lspconfig()
require('lsp.diagnostic')

-- Register the command
vim.api.nvim_create_user_command('InlayHintsToggle', function(_)
vim.lsp.inlay_hint.enable(0, not vim.lsp.inlay_hint.is_enabled())
Expand All @@ -155,21 +157,6 @@ function M.lspconfig()
if vim.g.nvim_enable_inlayhints == 1 and client.server_capabilities.inlayHintProvider then
vim.lsp.inlay_hint.enable(bufnr, true)
end
-- 自动浮窗展示diagnostic
vim.api.nvim_create_autocmd("CursorHold", {
buffer = bufnr,
callback = function()
local opts = {
focusable = false,
close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
border = 'rounded',
source = 'always',
prefix = ' ',
scope = 'cursor',
}
vim.diagnostic.open_float(nil, opts)
end
})
end

-- NOTE: 某个不知名的地方会重新设置diagnostic,故在此重新设置一遍
Expand Down
5 changes: 5 additions & 0 deletions root/.vim/doc/neovim.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*neovim.txt*

Toggle lsp diagnostic auto floating window *:LspFloatToggle*

Enable lsp diagnostic auto floating window *g:nvim_lsp_diagnostic_enable_auto_floating_window*

0 comments on commit 8359e8c

Please sign in to comment.