Created
August 6, 2024 15:26
-
-
Save aca/a1a2762957f2aa3487b26b1324cf69a9 to your computer and use it in GitHub Desktop.
diagnostic_rightalign.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local strdisplaywidth = require("plenary").strings.strdisplaywidth | |
local rightAlignFormat = function(diagnostic) | |
local msg = diagnostic.message | |
local line = diagnostic.lnum | |
local line_length = strdisplaywidth(vim.api.nvim_buf_get_lines(0, line, line + 1, false)[1] or "") | |
local msg_length = strdisplaywidth(msg) | |
local wininfo = vim.fn.getwininfo(vim.api.nvim_get_current_win())[1] | |
-- https://github.com/neovim/neovim/issues/17229 | |
local textoff = wininfo.textoff | |
local wwidth = wininfo.width | |
local sp = "" | |
-- -4: -2 for minimal default spacing that i can't control, -2 for prefix | |
local maxrightwidth = wwidth - textoff - line_length - 4 | |
if msg_length > maxrightwidth then | |
msg = string.sub(msg, 1, maxrightwidth - 1) .. "…" | |
else | |
local splen = wwidth - textoff - line_length - msg_length - 4 | |
sp = string.rep(" ", splen) | |
end | |
return string.format("%s» %s", sp, msg) | |
end | |
-- vim.diagnostic.config({ | |
-- virtual_text = { | |
-- format = rightAlignFormat, | |
-- spacing = 0, | |
-- prefix = "", | |
-- suffix = "", | |
-- }, | |
-- }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment