diff --git a/lua/sense/utils/init.lua b/lua/sense/utils/init.lua index d3b3ba5..bd2f042 100644 --- a/lua/sense/utils/init.lua +++ b/lua/sense/utils/init.lua @@ -2,8 +2,10 @@ local M = {} ---@param text string ---@param width number +---@return string text offset-applied text +---@return number offset offset amount (unsigned) function M.align_right(text, width) - local offset = width - vim.fn.strdisplaywidth(text) + local offset = math.max(width - vim.fn.strdisplaywidth(text), 0) text = string.rep(" ", offset) .. text return text, offset end diff --git a/lua/sense/utils/uiutils.lua b/lua/sense/utils/uiutils.lua index df5905e..05a1313 100644 --- a/lua/sense/utils/uiutils.lua +++ b/lua/sense/utils/uiutils.lua @@ -15,7 +15,6 @@ function M.set_lines(buf, lines, highlights, width) if width then -- Right justify each line by padding spaces for l, line in ipairs(lines) do - -- FIXME: handle when offset is negative (line is longer than width) local text, offset = utils.align_right(line, width) lines[l] = text offsets[l] = offset diff --git a/spec/ui/virtual_spec.lua b/spec/ui/virtual_spec.lua index d359d03..ac75e5b 100644 --- a/spec/ui/virtual_spec.lua +++ b/spec/ui/virtual_spec.lua @@ -15,26 +15,6 @@ vim.g.sense_nvim = { } local TEST_NS = vim.api.nvim_create_namespace("sense.test") -local diags = { - { - code = "UndeclaredName", - col = 4, - end_col = 7, - end_lnum = 3, - lnum = 3, - message = "undefined: fmt", - severity = 1, - source = "compiler", - user_data = { - lsp = { - code = "UndeclaredName", - codeDescription = { - href = "https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName", - }, - }, - }, - }, -} describe("UI component - virtual", function() -- setup options to test with @@ -42,6 +22,27 @@ describe("UI component - virtual", function() vim.o.relativenumber = true vim.o.signcolumn = "yes" vim.o.splitright = true + local diags = { + { + code = "UndeclaredName", + col = 4, + end_col = 7, + end_lnum = 3, + lnum = 3, + message = "undefined: fmt", + severity = 1, + source = "compiler", + user_data = { + lsp = { + code = "UndeclaredName", + codeDescription = { + href = "https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#UndeclaredName", + }, + }, + }, + }, + } + before_each(function () -- clear all buffers/windows vim.cmd("silent! %bwipeout") @@ -65,4 +66,24 @@ describe("UI component - virtual", function() }) assert.same(2, #testutils.list_visible_wins()) end) + it("with long message", function() + vim.diagnostic.reset(TEST_NS, 0) + vim.diagnostic.set(TEST_NS, 0, { + { + code = "UndeclaredName", + col = 4, + end_col = 7, + end_lnum = 3, + lnum = 3, + message = "some really really long long long long message that easily exceeds 80 columns I need to put bit more text here to make it long enough", + severity = 1, + source = "compiler", + }, + }) + vim.cmd.normal("G") + testutils.emulate_missing_events("WinScrolled", { + match = vim.api.nvim_get_current_win(), + }) + assert.same(1, #testutils.list_visible_float_wins()) + end) end)