aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike <10135646+mikesmithgh@users.noreply.github.com>2023-07-16 06:11:45 -0400
committerGitHub <noreply@github.com>2023-07-16 03:11:45 -0700
commit251ca45ac94851c896db0d27685622fb78a73b3e (patch)
tree6a81137cddaba404603235c2490d1208be069a4b
parentabe39f2b243dc813456225a779fbeb7ae6affc27 (diff)
downloadrneovim-251ca45ac94851c896db0d27685622fb78a73b3e.tar.gz
rneovim-251ca45ac94851c896db0d27685622fb78a73b3e.tar.bz2
rneovim-251ca45ac94851c896db0d27685622fb78a73b3e.zip
fix(lsp): markdown code fence should allow space before info string #24364
Problem: Bash language server returns "hover" markdown content that starts with a code fence and info string of `man` preceded by whitespace, which Nvim does not render properly. See https://github.com/bash-lsp/bash-language-server/blob/0ee73c53cebdc18311d4a4ad9367185ea4d98a03/server/src/server.ts#L821C15-L821C15 ```typescript function getMarkdownContent(documentation: string, language?: string): LSP.MarkupContent { return { value: language ? // eslint-disable-next-line prefer-template ['``` ' + language, documentation, '```'].join('\n') : documentation, kind: LSP.MarkupKind.Markdown, } } ``` For example, ``` ``` man NAME git - the stupid content tracker ``` ``` If I remove the white space, then it is properly formatted. ``` ```man instead of ``` man ``` Per CommonMark Spec https://spec.commonmark.org/0.30/#info-string whitespace is allowed before and after the `info string` which identifies the language in a codeblock. > The line with the opening code fence may optionally contain some text > following the code fence; this is trimmed of leading and trailing > spaces or tabs and called the [info > string](https://spec.commonmark.org/0.30/#info-string). If the [info > string](https://spec.commonmark.org/0.30/#info-string) comes after > a backtick fence, it may not contain any backtick characters. (The > reason for this restriction is that otherwise some inline code would > be incorrectly interpreted as the beginning of a fenced code block.) Solution: Adjust stylize_markdown() to allow whitespace before codeblock info.
-rw-r--r--runtime/lua/vim/lsp/util.lua2
-rw-r--r--test/functional/plugin/lsp/utils_spec.lua13
2 files changed, 14 insertions, 1 deletions
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 8d6f88bb2c..0da88f800e 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -1347,7 +1347,7 @@ function M.stylize_markdown(bufnr, contents, opts)
-- table of fence types to {ft, begin, end}
-- when ft is nil, we get the ft from the regex match
local matchers = {
- block = { nil, '```+([a-zA-Z0-9_]*)', '```+' },
+ block = { nil, '```+%s*([a-zA-Z0-9_]*)', '```+' },
pre = { nil, '<pre>([a-z0-9]*)', '</pre>' },
code = { '', '<code>', '</code>' },
text = { 'text', '<text>', '</text>' },
diff --git a/test/functional/plugin/lsp/utils_spec.lua b/test/functional/plugin/lsp/utils_spec.lua
index 3e53b6d574..c91fffa90f 100644
--- a/test/functional/plugin/lsp/utils_spec.lua
+++ b/test/functional/plugin/lsp/utils_spec.lua
@@ -34,6 +34,19 @@ describe('vim.lsp.util', function()
eq(expected, stylize_markdown(lines, opts))
end)
+ it('code fences with whitespace surrounded info string', 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",