From e7bde44d6d2a88e67d68d5a1b9f0d037179dd14e Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 6 Aug 2021 23:15:49 +0200 Subject: [PATCH] fix(lsp): prevent duplicate lines seperated by newlines (#15284) --- runtime/lua/vim/lsp/util.lua | 43 +++++++++++++++++------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua index dc15d67e1c..4c3ceaf503 100644 --- a/runtime/lua/vim/lsp/util.lua +++ b/runtime/lua/vim/lsp/util.lua @@ -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