diff --git a/init.lua b/init.lua index aa7d3c3..4beead9 100644 --- a/init.lua +++ b/init.lua @@ -29,5 +29,12 @@ require("custom-autocmd") require("mappings") -- all the plugins installed and their configurations vim.cmd("source " .. vim.fs.joinpath(config_dir, "viml_conf/plugins.vim")) + +-- diagnostic related config +require("diagnostic-conf") + -- colorscheme settings -require("colorschemes") +local color_scheme = require("colorschemes") + +-- Load a random colorscheme +color_scheme.rand_colorscheme() diff --git a/lua/config/lsp.lua b/lua/config/lsp.lua index 32dc0ec..374ab88 100644 --- a/lua/config/lsp.lua +++ b/lua/config/lsp.lua @@ -6,18 +6,6 @@ local lspconfig = require("lspconfig") 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) -- Mappings. 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", "", vim.lsp.buf.definition) 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) map("n", "", vim.lsp.buf.signature_help) map("n", "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", "qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" }) - -- this puts diagnostics from current buffer to quickfix - map("n", "qb", function() - set_qflist(bufnr) - end, { desc = "put buffer diagnostics to qf" }) map("n", "ca", vim.lsp.buf.code_action, { desc = "LSP code action" }) map("n", "wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" }) map("n", "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. 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 }) api.nvim_create_autocmd("CursorHold", { group = gid, @@ -255,45 +224,3 @@ if utils.executable("lua-language-server") then capabilities = capabilities, } 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, -}) diff --git a/lua/diagnostic-conf.lua b/lua/diagnostic-conf.lua new file mode 100644 index 0000000..e50a4be --- /dev/null +++ b/lua/diagnostic-conf.lua @@ -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", "qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" }) + +-- this puts diagnostics from current buffer to quickfix +vim.keymap.set("n", "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, +})