Skip to content

Commit

Permalink
feat(menu/entry): extract function to get documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
max397574 committed Nov 9, 2024
1 parent c609221 commit 36a5ffc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
31 changes: 31 additions & 0 deletions lua/care/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,35 @@ function Entry:get_replace_range()
end
end

function Entry:get_documentation()
local completion_item = self.completion_item
local documentation = completion_item.documentation
---@diagnostic disable-next-line: param-type-mismatch
local documentation_text = vim.trim(type(documentation) == "table" and documentation.value or documentation or "")
if (documentation_text):match("^%s*$") and (completion_item.detail or ""):match("^%s*$") then
return nil, nil
end
local format = "markdown"
local contents
if documentation_text ~= "" then
if type(documentation) == "table" and documentation.kind == "plaintext" then
format = "plaintext"
contents = vim.split(documentation.value or "", "\n", { trimempty = true })
else
contents = vim.lsp.util.convert_input_to_markdown_lines(documentation_text)
end
end

if completion_item.detail and completion_item.detail ~= "" then
if not contents then
contents = {}
end
table.insert(contents, 1, vim.trim(completion_item.detail))
if documentation_text ~= "" then
table.insert(contents, 2, "---")
end
end
return contents, format
end

return Entry
39 changes: 9 additions & 30 deletions lua/care/menu/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,9 @@ function Menu:draw_docs(entry)
return
end

---@param doc_entry care.entry
local function open_docs_window(doc_entry)
local config = self.config.ui.docs_view or {}
local completion_item = doc_entry.completion_item
local documentation = completion_item.documentation
local documentation_text =
vim.trim(type(documentation) == "table" and documentation.value or documentation or "")
if (documentation_text):match("^%s*$") and (completion_item.detail or ""):match("^%s*$") then
self.docs_window:close()
return
end
local format = "markdown"
local contents
if documentation_text ~= "" then
if type(documentation) == "table" and documentation.kind == "plaintext" then
format = "plaintext"
contents = vim.split(documentation.value or "", "\n", { trimempty = true })
else
contents = vim.lsp.util.convert_input_to_markdown_lines(documentation_text)
end
end

if completion_item.detail and completion_item.detail ~= "" then
if not contents then
contents = {}
end
table.insert(contents, 1, vim.trim(completion_item.detail))
if documentation_text ~= "" then
table.insert(contents, 2, "---")
end
end

local menu_border = self.config.ui.menu.border
local menu_has_border = menu_border and menu_border ~= "none"
Expand Down Expand Up @@ -107,10 +80,16 @@ function Menu:draw_docs(entry)
local border = self.config.ui.docs_view.border
local has_border = border and border ~= "none"

local do_stylize = format == "markdown" and vim.g.syntax_on ~= nil

width = width - (has_border and 2 or 0)

local contents, format = doc_entry:get_documentation()

if contents == nil then
self.docs_window:close()
return
end

local do_stylize = format == "markdown" and vim.g.syntax_on ~= nil
if do_stylize then
contents = vim.lsp.util._normalize_markdown(
vim.split(table.concat(contents, "\n"), "\n", { trimempty = true }),
Expand Down
2 changes: 2 additions & 0 deletions lua/care/types/entry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
---@field get_replace_range fun(self: care.entry): lsp.Range
--- Gets the default range for entry (if there is no textEdit)
---@field _get_default_range fun(self: care.entry): lsp.Range
--- Gets the documentation for an entry. This includes adding the detail.
---@field get_documentation fun(self: care.entry) : string[]?, string?

0 comments on commit 36a5ffc

Please sign in to comment.