summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lua/ache/diagnostics.lua55
-rw-r--r--lua/ache/lazy.lua7
2 files changed, 62 insertions, 0 deletions
diff --git a/lua/ache/diagnostics.lua b/lua/ache/diagnostics.lua
new file mode 100644
index 0000000..5f70177
--- /dev/null
+++ b/lua/ache/diagnostics.lua
@@ -0,0 +1,55 @@
+local M = {}
+
+local serverity_map = {
+ "DiagnosticError",
+ "DiagnosticWarn",
+ "DiagnosticInfo",
+ "DiagnosticHint",
+}
+local icon_map = {
+ "  ",
+ "  ",
+ "  ",
+ "  ",
+}
+
+local function source_string(source, code)
+ if code then
+ return string.format(" [%s %s]", source, code)
+ else
+ return string.format(" [%s]", source)
+ end
+end
+
+M.line_diagnostics = function()
+ local bufnr, lnum = unpack(vim.fn.getcurpos())
+ local diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr, lnum - 1, {})
+ if vim.tbl_isempty(diagnostics) then
+ return
+ end
+
+ local lines = {}
+
+ for _, diagnostic in ipairs(diagnostics) do
+ table.insert(
+ lines,
+ icon_map[diagnostic.severity]
+ .. " "
+ .. diagnostic.message:gsub("\n", " ")
+ .. source_string(diagnostic.source, diagnostic.code)
+ )
+ end
+
+ local floating_bufnr, _ = vim.lsp.util.open_floating_preview(lines, "plaintext", {
+ border = vim.g.floating_window_border_dark,
+ focus_id = "line",
+ })
+
+ for i, diagnostic in ipairs(diagnostics) do
+ local message_length = #lines[i] - #source_string(diagnostic.source, diagnostic.code)
+ vim.api.nvim_buf_add_highlight(floating_bufnr, -1, serverity_map[diagnostic.severity], i - 1, 0, message_length)
+ vim.api.nvim_buf_add_highlight(floating_bufnr, -1, "DiagnosticSource", i - 1, message_length, -1)
+ end
+end
+
+return M
diff --git a/lua/ache/lazy.lua b/lua/ache/lazy.lua
index e01d3cb..1800f12 100644
--- a/lua/ache/lazy.lua
+++ b/lua/ache/lazy.lua
@@ -160,5 +160,12 @@ vim.api.nvim_create_autocmd("LspAttach", {
vim.lsp.buf.code_action,
{ buffer = args.buf, desc = "Lsp: See available code actions" }
)
+
+ vim.keymap.set(
+ "n",
+ "<leader>lt",
+ require("ache.diagnostics").line_diagnostics,
+ { buffer = args.buf, silent = true, desc = "Show diagnostics tools used" }
+ )
end,
})