Skip to content

Commit 080104e

Browse files
fix: disable line wrapping in LSP hover docs in some cases
## Details Issue: #408 For narrow windows adding the language icon above code blocks can lead to text wrapping which will lead to an extra line under the code block. There are really only 2 ways to fix this: 1. Increase the window width such that it is wide enough to avoid wrapping. 2. Disable line wrapping. In the first case we end up needing to accurately calculate the width we need to avoid the line wrapping and also check that the new width does not cause the window to flow out of bounds. There's lots of edge cases to these and the end result doesn't look particularly great since it'll add extra space off the side of these narrow doc buffers. So instead we take the much simpler approach of disabling line wrapping on these buffers, however we do this only when the content in the buffer does not need line wrapping, i.e. the width of the longest line is <= the width of the window. This should work for most situations but I'm sure there will be some edge cases to it.
1 parent 0d1b065 commit 080104e

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

lua/render-markdown/core/ui.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function M.run_update(buf, win, change)
158158
local hidden = config:hidden(mode, row)
159159
local extmarks = buffer:get_marks()
160160
if initial then
161-
Compat.lsp_window_height(win, extmarks)
161+
Compat.lsp_window_height(buf, win, extmarks)
162162
state.on.initial({ buf = buf, win = win })
163163
end
164164
for _, extmark in ipairs(extmarks) do

lua/render-markdown/health.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local state = require('render-markdown.state')
55
local M = {}
66

77
---@private
8-
M.version = '8.3.6'
8+
M.version = '8.3.7'
99

1010
function M.check()
1111
M.start('version')

lua/render-markdown/lib/compat.lua

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,40 @@ M.uv = vim.uv or vim.loop
55
M.has_10 = vim.fn.has('nvim-0.10') == 1
66
M.has_11 = vim.fn.has('nvim-0.11') == 1
77

8+
---@param buf integer
89
---@param win integer
910
---@param extmarks render.md.Extmark[]
1011
---@see vim.lsp.util.open_floating_preview
11-
function M.lsp_window_height(win, extmarks)
12-
-- This is a fragile way of identifying whether this is a floating LSP buffer,
13-
-- comes from the implementation and not from any documentation
14-
local lsp_buf = pcall(vim.api.nvim_win_get_var, win, 'lsp_floating_bufnr')
15-
if not lsp_buf then
12+
function M.lsp_window_height(buf, win, extmarks)
13+
-- this is a fragile way of identifying whether this is a floating LSP
14+
-- window, comes from the implementation and not from any documentation
15+
local has_lsp = pcall(vim.api.nvim_win_get_var, win, 'lsp_floating_bufnr')
16+
if not has_lsp then
1617
return
1718
end
18-
local concealed = 0
19+
20+
local Env = require('render-markdown.lib.env')
21+
local Str = require('render-markdown.lib.str')
22+
23+
-- account for conceal lines marks allowing us to reduce window height
24+
local height = vim.api.nvim_win_text_height(win, {}).all
1925
for _, extmark in ipairs(extmarks) do
2026
if extmark:get().opts.conceal_lines ~= nil then
21-
concealed = concealed + 1
27+
height = height - 1
2228
end
2329
end
24-
if concealed == 0 then
25-
return
26-
end
27-
local height = vim.api.nvim_win_text_height(win, {}).all - concealed
2830
if height < vim.api.nvim_win_get_height(win) then
2931
vim.api.nvim_win_set_height(win, height)
3032
end
33+
34+
-- disable line wrapping if it is not needed to contain the text
35+
local width = 0
36+
for _, line in ipairs(vim.api.nvim_buf_get_lines(buf, 0, -1, false)) do
37+
width = math.max(width, Str.width(line))
38+
end
39+
if width <= vim.api.nvim_win_get_width(win) then
40+
Env.win.set(win, 'wrap', false)
41+
end
3142
end
3243

3344
---@param cause string

0 commit comments

Comments
 (0)