From d52ebe317d5f912ffd060a75bf02a7276567337a Mon Sep 17 00:00:00 2001 From: John Reid Date: Sat, 13 Jun 2026 04:20:20 -0400 Subject: [PATCH] feat(diagnostic): add virt_lines_overflow option for virtual_lines #40178 Problem: Diagnostic virtual lines are hardcoded with "scroll" for virt_lines_overflow option. Solution: Add a `overflow` option to `virtual_lines` config to support "wrap", "scroll", "trunc", and "auto". --- runtime/doc/diagnostic.txt | 3 +++ runtime/doc/news.txt | 2 ++ runtime/lua/vim/diagnostic.lua | 4 ++++ runtime/lua/vim/diagnostic/_handlers.lua | 13 ++++++++----- test/functional/lua/diagnostic_spec.lua | 6 ++++-- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/runtime/doc/diagnostic.txt b/runtime/doc/diagnostic.txt index f90cbcd842..d325941b3b 100644 --- a/runtime/doc/diagnostic.txt +++ b/runtime/doc/diagnostic.txt @@ -718,6 +718,9 @@ Lua module: vim.diagnostic *diagnostic-api* a string or nil. If the return value is nil, the diagnostic is not displayed by the handler. Else the output text is used to display the diagnostic. + • {overflow}? (`'trunc'|'scroll'|'wrap'|'auto'`, default: `auto`) + See `virt_lines_overflow` in + |nvim_buf_set_extmark()|. • {severity}? (`vim.diagnostic.SeverityFilter`) Only show virtual lines for diagnostics matching the given severity |diagnostic-severity| diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index ad8ca7ef2a..627c25f248 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -152,6 +152,8 @@ DIAGNOSTICS • |vim.diagnostic.status()| uses the severity names defined by the `signs` field of |vim.diagnostic.config()|, if any. +• `virtual_lines.overflow` in |vim.diagnostic.config()| controls + how virtual lines wider than the window are displayed. EDITOR diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 5e0c19018c..9a68c7d313 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -284,6 +284,10 @@ local M = vim._defer_require('vim.diagnostic', { --- If the return value is nil, the diagnostic is not displayed by the handler. --- Else the output text is used to display the diagnostic. --- @field format? fun(diagnostic:vim.Diagnostic): string? +--- +--- See `virt_lines_overflow` in |nvim_buf_set_extmark()|. +--- (default: `auto`) +--- @field overflow? 'trunc'|'scroll'|'wrap'|'auto' --- @class vim.diagnostic.Opts.Signs --- diff --git a/runtime/lua/vim/diagnostic/_handlers.lua b/runtime/lua/vim/diagnostic/_handlers.lua index a7cdac96c1..ef1dff9562 100644 --- a/runtime/lua/vim/diagnostic/_handlers.lua +++ b/runtime/lua/vim/diagnostic/_handlers.lua @@ -470,7 +470,8 @@ end --- @param namespace integer --- @param bufnr integer --- @param diagnostics vim.Diagnostic[] -local function render_virtual_lines(namespace, bufnr, diagnostics) +--- @param opts vim.diagnostic.Opts.VirtualLines +local function render_virtual_lines(namespace, bufnr, diagnostics, opts) table.sort(diagnostics, function(d1, d2) return diagnostic_shared.diagnostic_cmp(d1, d2, 'lnum', false) end) @@ -628,7 +629,7 @@ local function render_virtual_lines(namespace, bufnr, diagnostics) end api.nvim_buf_set_extmark(bufnr, namespace, line_anchor[lnum] or lnum, 0, { - virt_lines_overflow = 'scroll', + virt_lines_overflow = opts.overflow or 'auto', virt_lines = virt_lines, }) end @@ -685,7 +686,8 @@ function M.virtual_lines.show(namespace, bufnr, diagnostics, opts) render_virtual_lines( ns.user_data.virt_lines_ns, bufnr, - diagnostic_shared.diagnostics_at_cursor(line_diagnostics) + diagnostic_shared.diagnostics_at_cursor(line_diagnostics), + vopts ) end) @@ -693,10 +695,11 @@ function M.virtual_lines.show(namespace, bufnr, diagnostics, opts) render_virtual_lines( ns.user_data.virt_lines_ns, bufnr, - diagnostic_shared.diagnostics_at_cursor(line_diagnostics) + diagnostic_shared.diagnostics_at_cursor(line_diagnostics), + vopts ) else - render_virtual_lines(ns.user_data.virt_lines_ns, bufnr, diagnostics) + render_virtual_lines(ns.user_data.virt_lines_ns, bufnr, diagnostics, vopts) end save_extmarks(ns.user_data.virt_lines_ns, bufnr) diff --git a/test/functional/lua/diagnostic_spec.lua b/test/functional/lua/diagnostic_spec.lua index 64fc06b0f6..9bbf96f84f 100644 --- a/test/functional/lua/diagnostic_spec.lua +++ b/test/functional/lua/diagnostic_spec.lua @@ -2386,10 +2386,12 @@ describe('vim.diagnostic', function() }) local extmarks = _G.get_virt_lines_extmarks(_G.diagnostic_ns) - return extmarks[1][4].virt_lines + return extmarks end) - eq('miss-symbol: Missed symbol `,`', result[1][3][1]) + eq(1, #result) + eq('auto', result[1][4].virt_lines_overflow) + eq('miss-symbol: Missed symbol `,`', result[1][4].virt_lines[1][3][1]) end) it('adds space to the left of the diagnostic', function()