fix(lsp): prevent duplicate lines seperated by newlines (#15284)

This commit is contained in:
Folke Lemaitre 2021-08-06 23:15:49 +02:00 committed by GitHub
parent f22326ef09
commit e7bde44d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1160,6 +1160,8 @@ function M.stylize_markdown(bufnr, contents, opts)
-- Clean up
contents = M._trim(contents, opts)
-- Insert blank line separator after code block?
local add_sep = opts.separator == nil and true or opts.separator
local stripped = {}
local highlights = {}
-- keep track of lnums that contain markdown
@ -1186,9 +1188,24 @@ function M.stylize_markdown(bufnr, contents, opts)
start = start + 1;
finish = #stripped;
})
-- add a separator, but not on the last line
if add_sep and i < #contents then
table.insert(stripped, "---")
markdown_lines[#stripped] = true
end
else
table.insert(stripped, line)
markdown_lines[#stripped] = true
-- strip any emty lines or separators prior to this separator in actual markdown
if line:match("^---+$") then
while markdown_lines[#stripped] and (stripped[#stripped]:match("^%s*$") or stripped[#stripped]:match("^---+$")) do
markdown_lines[#stripped] = false
table.remove(stripped, #stripped)
end
end
-- add the line if its not an empty line following a separator
if not (line:match("^%s*$") and markdown_lines[#stripped] and stripped[#stripped]:match("^---+$")) then
table.insert(stripped, line)
markdown_lines[#stripped] = true
end
i = i + 1
end
end
@ -1196,7 +1213,7 @@ function M.stylize_markdown(bufnr, contents, opts)
-- Compute size of float needed to show (wrapped) lines
opts.wrap_at = opts.wrap_at or (vim.wo["wrap"] and api.nvim_win_get_width(0))
local width, height = M._make_floating_popup_size(stripped, opts)
local width = M._make_floating_popup_size(stripped, opts)
local sep_line = string.rep("", math.min(width, opts.wrap_at or width))
@ -1206,26 +1223,6 @@ function M.stylize_markdown(bufnr, contents, opts)
end
end
-- Insert blank line separator after code block
local insert_separator = opts.separator
if insert_separator == nil then insert_separator = true end
if insert_separator then
local offset = 0
for _, h in ipairs(highlights) do
h.start = h.start + offset
h.finish = h.finish + offset
-- check if a seperator already exists and use that one instead of creating a new one
if h.finish + 1 <= #stripped then
if stripped[h.finish + 1] ~= sep_line then
table.insert(stripped, h.finish + 1, sep_line)
offset = offset + 1
height = height + 1
end
end
end
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, stripped)
local idx = 1