mirror of
https://github.com/jdhao/nvim-config.git
synced 2025-06-08 14:14:33 +02:00
Separate diagnostic config from lsp ones (#389)
This commit is contained in:
parent
12147bec86
commit
0816faee76
9
init.lua
9
init.lua
@ -29,5 +29,12 @@ require("custom-autocmd")
|
|||||||
require("mappings")
|
require("mappings")
|
||||||
-- all the plugins installed and their configurations
|
-- all the plugins installed and their configurations
|
||||||
vim.cmd("source " .. vim.fs.joinpath(config_dir, "viml_conf/plugins.vim"))
|
vim.cmd("source " .. vim.fs.joinpath(config_dir, "viml_conf/plugins.vim"))
|
||||||
|
|
||||||
|
-- diagnostic related config
|
||||||
|
require("diagnostic-conf")
|
||||||
|
|
||||||
-- colorscheme settings
|
-- colorscheme settings
|
||||||
require("colorschemes")
|
local color_scheme = require("colorschemes")
|
||||||
|
|
||||||
|
-- Load a random colorscheme
|
||||||
|
color_scheme.rand_colorscheme()
|
||||||
|
|||||||
@ -6,18 +6,6 @@ local lspconfig = require("lspconfig")
|
|||||||
|
|
||||||
local utils = require("utils")
|
local utils = require("utils")
|
||||||
|
|
||||||
-- set quickfix list from diagnostics in a certain buffer, not the whole workspace
|
|
||||||
local set_qflist = function(buf_num, severity)
|
|
||||||
local diagnostics = nil
|
|
||||||
diagnostics = diagnostic.get(buf_num, { severity = severity })
|
|
||||||
|
|
||||||
local qf_items = diagnostic.toqflist(diagnostics)
|
|
||||||
vim.fn.setqflist({}, " ", { title = "Diagnostics", items = qf_items })
|
|
||||||
|
|
||||||
-- open quickfix by default
|
|
||||||
vim.cmd([[copen]])
|
|
||||||
end
|
|
||||||
|
|
||||||
local custom_attach = function(client, bufnr)
|
local custom_attach = function(client, bufnr)
|
||||||
-- Mappings.
|
-- Mappings.
|
||||||
local map = function(mode, l, r, opts)
|
local map = function(mode, l, r, opts)
|
||||||
@ -30,23 +18,10 @@ local custom_attach = function(client, bufnr)
|
|||||||
map("n", "gd", vim.lsp.buf.definition, { desc = "go to definition" })
|
map("n", "gd", vim.lsp.buf.definition, { desc = "go to definition" })
|
||||||
map("n", "<C-]>", vim.lsp.buf.definition)
|
map("n", "<C-]>", vim.lsp.buf.definition)
|
||||||
map("n", "K", function()
|
map("n", "K", function()
|
||||||
vim.lsp.buf.hover { border = "single", max_height = 25 }
|
vim.lsp.buf.hover { border = "single", max_height = 25, max_width = 120 }
|
||||||
end)
|
end)
|
||||||
map("n", "<C-k>", vim.lsp.buf.signature_help)
|
map("n", "<C-k>", vim.lsp.buf.signature_help)
|
||||||
map("n", "<space>rn", vim.lsp.buf.rename, { desc = "varialbe rename" })
|
map("n", "<space>rn", vim.lsp.buf.rename, { desc = "varialbe rename" })
|
||||||
map("n", "gr", vim.lsp.buf.references, { desc = "show references" })
|
|
||||||
map("n", "[d", function()
|
|
||||||
diagnostic.jump { count = -1 }
|
|
||||||
end, { desc = "previous diagnostic" })
|
|
||||||
map("n", "]d", function()
|
|
||||||
diagnostic.jump { count = 1 }
|
|
||||||
end, { desc = "next diagnostic" })
|
|
||||||
-- this puts diagnostics from opened files to quickfix
|
|
||||||
map("n", "<space>qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" })
|
|
||||||
-- this puts diagnostics from current buffer to quickfix
|
|
||||||
map("n", "<space>qb", function()
|
|
||||||
set_qflist(bufnr)
|
|
||||||
end, { desc = "put buffer diagnostics to qf" })
|
|
||||||
map("n", "<space>ca", vim.lsp.buf.code_action, { desc = "LSP code action" })
|
map("n", "<space>ca", vim.lsp.buf.code_action, { desc = "LSP code action" })
|
||||||
map("n", "<space>wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" })
|
map("n", "<space>wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" })
|
||||||
map("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" })
|
map("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" })
|
||||||
@ -65,12 +40,6 @@ local custom_attach = function(client, bufnr)
|
|||||||
|
|
||||||
-- The blow command will highlight the current variable and its usages in the buffer.
|
-- The blow command will highlight the current variable and its usages in the buffer.
|
||||||
if client.server_capabilities.documentHighlightProvider then
|
if client.server_capabilities.documentHighlightProvider then
|
||||||
vim.cmd([[
|
|
||||||
hi! link LspReferenceRead Visual
|
|
||||||
hi! link LspReferenceText Visual
|
|
||||||
hi! link LspReferenceWrite Visual
|
|
||||||
]])
|
|
||||||
|
|
||||||
local gid = api.nvim_create_augroup("lsp_document_highlight", { clear = true })
|
local gid = api.nvim_create_augroup("lsp_document_highlight", { clear = true })
|
||||||
api.nvim_create_autocmd("CursorHold", {
|
api.nvim_create_autocmd("CursorHold", {
|
||||||
group = gid,
|
group = gid,
|
||||||
@ -255,45 +224,3 @@ if utils.executable("lua-language-server") then
|
|||||||
capabilities = capabilities,
|
capabilities = capabilities,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
-- global config for diagnostic
|
|
||||||
diagnostic.config {
|
|
||||||
underline = false,
|
|
||||||
virtual_text = false,
|
|
||||||
virtual_lines = false,
|
|
||||||
signs = {
|
|
||||||
text = {
|
|
||||||
[diagnostic.severity.ERROR] = "🆇",
|
|
||||||
[diagnostic.severity.WARN] = "⚠️",
|
|
||||||
[diagnostic.severity.INFO] = "ℹ️",
|
|
||||||
[diagnostic.severity.HINT] = "",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
severity_sort = true,
|
|
||||||
float = {
|
|
||||||
source = true,
|
|
||||||
header = "Diagnostics:",
|
|
||||||
prefix = " ",
|
|
||||||
border = "single",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
api.nvim_create_autocmd("CursorHold", {
|
|
||||||
pattern = "*",
|
|
||||||
callback = function()
|
|
||||||
if #vim.diagnostic.get(0) == 0 then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if not vim.b.diagnostics_pos then
|
|
||||||
vim.b.diagnostics_pos = { nil, nil }
|
|
||||||
end
|
|
||||||
|
|
||||||
local cursor_pos = api.nvim_win_get_cursor(0)
|
|
||||||
if cursor_pos[1] ~= vim.b.diagnostics_pos[1] or cursor_pos[2] ~= vim.b.diagnostics_pos[2] then
|
|
||||||
diagnostic.open_float()
|
|
||||||
end
|
|
||||||
|
|
||||||
vim.b.diagnostics_pos = cursor_pos
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|||||||
66
lua/diagnostic-conf.lua
Normal file
66
lua/diagnostic-conf.lua
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
local diagnostic = vim.diagnostic
|
||||||
|
local api = vim.api
|
||||||
|
|
||||||
|
-- global config for diagnostic
|
||||||
|
diagnostic.config {
|
||||||
|
underline = false,
|
||||||
|
virtual_text = false,
|
||||||
|
virtual_lines = false,
|
||||||
|
signs = {
|
||||||
|
text = {
|
||||||
|
[diagnostic.severity.ERROR] = "🆇",
|
||||||
|
[diagnostic.severity.WARN] = "⚠️",
|
||||||
|
[diagnostic.severity.INFO] = "ℹ️",
|
||||||
|
[diagnostic.severity.HINT] = "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
severity_sort = true,
|
||||||
|
float = {
|
||||||
|
source = true,
|
||||||
|
header = "Diagnostics:",
|
||||||
|
prefix = " ",
|
||||||
|
border = "single",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
-- set quickfix list from diagnostics in a certain buffer, not the whole workspace
|
||||||
|
local set_qflist = function(buf_num, severity)
|
||||||
|
local diagnostics = nil
|
||||||
|
diagnostics = diagnostic.get(buf_num, { severity = severity })
|
||||||
|
|
||||||
|
local qf_items = diagnostic.toqflist(diagnostics)
|
||||||
|
vim.fn.setqflist({}, " ", { title = "Diagnostics", items = qf_items })
|
||||||
|
|
||||||
|
-- open quickfix by default
|
||||||
|
vim.cmd([[copen]])
|
||||||
|
end
|
||||||
|
|
||||||
|
-- this puts diagnostics from opened files to quickfix
|
||||||
|
vim.keymap.set("n", "<space>qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" })
|
||||||
|
|
||||||
|
-- this puts diagnostics from current buffer to quickfix
|
||||||
|
vim.keymap.set("n", "<space>qb", function()
|
||||||
|
set_qflist(0)
|
||||||
|
end, { desc = "put buffer diagnostics to qf" })
|
||||||
|
|
||||||
|
-- automatically show diagnostic in float win for current line
|
||||||
|
api.nvim_create_autocmd("CursorHold", {
|
||||||
|
pattern = "*",
|
||||||
|
callback = function()
|
||||||
|
if #vim.diagnostic.get(0) == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if not vim.b.diagnostics_pos then
|
||||||
|
vim.b.diagnostics_pos = { nil, nil }
|
||||||
|
end
|
||||||
|
|
||||||
|
local cursor_pos = api.nvim_win_get_cursor(0)
|
||||||
|
|
||||||
|
if not vim.deep_equal(cursor_pos, vim.b.diagnostics_pos) then
|
||||||
|
diagnostic.open_float { width = 100 }
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.b.diagnostics_pos = cursor_pos
|
||||||
|
end,
|
||||||
|
})
|
||||||
Loading…
x
Reference in New Issue
Block a user