Skip to content

Commit

Permalink
feat(lsp): fallback to buffer filetype for code blocks without lang. F…
Browse files Browse the repository at this point in the history
…ixes folke#378
  • Loading branch information
folke authored and willothy committed Aug 19, 2023
1 parent 660e051 commit bd8465d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lua/noice/lsp/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ end
-- Formats the content and adds it to the message
---@param contents MarkupContents Markup content
---@param message NoiceMessage Noice message
function M.format(message, contents)
---@param opts? MarkdownFormatOptions
function M.format(message, contents, opts)
local text = table.concat(M.format_markdown(contents), "\n")
Markdown.format(message, text)
Markdown.format(message, text, opts)
return message
end

Expand Down
4 changes: 2 additions & 2 deletions lua/noice/lsp/hover.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function M.setup()
vim.lsp.handlers["textDocument/hover"] = M.on_hover
end

function M.on_hover(_, result)
function M.on_hover(_, result, ctx)
if not (result and result.contents) then
vim.notify("No information available")
return
Expand All @@ -19,7 +19,7 @@ function M.on_hover(_, result)
local message = Docs.get("hover")

if not message:focus() then
Format.format(message, result.contents)
Format.format(message, result.contents, { ft = vim.bo[ctx.bufnr].filetype })
if message:is_empty() then
vim.notify("No information available")
return
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/lsp/signature.lua
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function M:format_signature(sig_index, sig)

if sig.documentation then
Markdown.horizontal_line(self.message)
Format.format(self.message, sig.documentation)
Format.format(self.message, sig.documentation, { ft = self.ft })
end
end

Expand Down
15 changes: 11 additions & 4 deletions lua/noice/text/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ function M.conceal_escape_characters(buf, ns, range)
end

---@param text string
function M.parse(text)
---@param opts? MarkdownFormatOptions
function M.parse(text, opts)
opts = opts or {}
---@type string
text = text:gsub("</?pre>", "```")
text = M.html_entities(text)
Expand Down Expand Up @@ -85,7 +87,7 @@ function M.parse(text)
end
elseif M.is_code_block(line) then
---@type string
local lang = line:match("```(%S+)") or "text"
local lang = line:match("```(%S+)") or opts.ft or "text"
local block = { lang = lang, code = {} }
while lines[l + 1] and not M.is_code_block(lines[l + 1]) do
table.insert(block.code, lines[l + 1])
Expand Down Expand Up @@ -147,15 +149,20 @@ function M.get_highlights(line)
return ret
end

---@alias MarkdownFormatOptions {ft?: string}

---@param message NoiceMessage
---@param text string
---@param opts? MarkdownFormatOptions
--```lua
--local a = 1
--local b = true
--```
--foo tex
function M.format(message, text)
local blocks = M.parse(text)
function M.format(message, text, opts)
opts = opts or {}

local blocks = M.parse(text, opts)

local md_lines = 0

Expand Down

0 comments on commit bd8465d

Please sign in to comment.