aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Pommella Alegro <robertoaall@gmail.com>2023-03-25 13:46:07 -0300
committerGitHub <noreply@github.com>2023-03-25 12:46:07 -0400
commit257d894d75bc583bb16f4dbe441907eb273d20ad (patch)
tree8d93aaea761308776789cb2e4b3156f751b0b30e
parent74f05a152db1a6e5fc1e02fd8fbe61cf79e5aa59 (diff)
downloadrneovim-257d894d75bc583bb16f4dbe441907eb273d20ad.tar.gz
rneovim-257d894d75bc583bb16f4dbe441907eb273d20ad.tar.bz2
rneovim-257d894d75bc583bb16f4dbe441907eb273d20ad.zip
feat(lsp): render markdown in docs hover #22766
Problem: LSP docs hover (textDocument/hover) doesn't handle HTML escape seqs in markdown. Solution: Convert common HTML escape seqs to a nicer form, to display in the float. closees #22757 Signed-off-by: Kasama <robertoaall@gmail.com>
-rw-r--r--runtime/lua/vim/lsp/util.lua14
-rw-r--r--test/functional/plugin/lsp/utils_spec.lua75
2 files changed, 89 insertions, 0 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 48faddfce1..ebde7af16c 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -1378,6 +1378,20 @@ function M.stylize_markdown(bufnr, contents, opts)
end
end
+ -- Handle some common html escape sequences
+ stripped = vim.tbl_map(function(line)
+ local escapes = {
+ ['&gt;'] = '>',
+ ['&lt;'] = '<',
+ ['&quot;'] = '"',
+ ['&apos;'] = "'",
+ ['&ensp;'] = ' ',
+ ['&emsp;'] = ' ',
+ ['&amp;'] = '&',
+ }
+ return (string.gsub(line, '&[^ ;]+;', escapes))
+ end, stripped)
+
-- 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 = M._make_floating_popup_size(stripped, opts)
diff --git a/test/functional/plugin/lsp/utils_spec.lua b/test/functional/plugin/lsp/utils_spec.lua
new file mode 100644
index 0000000000..3e53b6d574
--- /dev/null
+++ b/test/functional/plugin/lsp/utils_spec.lua
@@ -0,0 +1,75 @@
+local helpers = require('test.functional.helpers')(after_each)
+
+local eq = helpers.eq
+local exec_lua = helpers.exec_lua
+
+describe('vim.lsp.util', function()
+ before_each(helpers.clear)
+
+ describe('stylize_markdown', function()
+ local stylize_markdown = function(content, opts)
+ return exec_lua([[
+ local bufnr = vim.uri_to_bufnr("file:///fake/uri")
+ vim.fn.bufload(bufnr)
+
+ local args = { ... }
+ local content = args[1]
+ local opts = args[2]
+ local stripped_content = vim.lsp.util.stylize_markdown(bufnr, content, opts)
+
+ return stripped_content
+ ]], content, opts)
+ end
+
+ it('code fences', function()
+ local lines = {
+ "```lua",
+ "local hello = 'world'",
+ "```",
+ }
+ local expected = {
+ "local hello = 'world'",
+ }
+ local opts = {}
+ eq(expected, stylize_markdown(lines, opts))
+ end)
+
+ it('adds separator after code block', function()
+ local lines = {
+ "```lua",
+ "local hello = 'world'",
+ "```",
+ "",
+ "something",
+ }
+ local expected = {
+ "local hello = 'world'",
+ "─────────────────────",
+ "something",
+ }
+ local opts = { separator = true }
+ eq(expected, stylize_markdown(lines, opts))
+ end)
+
+ it('replaces supported HTML entities', function()
+ local lines = {
+ "1 &lt; 2",
+ "3 &gt; 2",
+ "&quot;quoted&quot;",
+ "&apos;apos&apos;",
+ "&ensp; &emsp;",
+ "&amp;",
+ }
+ local expected = {
+ "1 < 2",
+ "3 > 2",
+ '"quoted"',
+ "'apos'",
+ " ",
+ "&",
+ }
+ local opts = {}
+ eq(expected, stylize_markdown(lines, opts))
+ end)
+ end)
+end)