Skip to content

Commit

Permalink
feat: added overrides for default lsp and cmp formatters :) Enable wi…
Browse files Browse the repository at this point in the history
…th config.lsp.override
  • Loading branch information
folke committed Oct 28, 2022
1 parent cdb25b8 commit 6d9fb12
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
51 changes: 42 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ use({
"rcarriga/nvim-notify",
}
})
```

<details><summary>vim-plug</summary>
<
```

```viml
" vim-plug
Expand All @@ -60,8 +60,6 @@ call plug#end()
lua require("noice").setup()
```

</details>

## ⚙️ Configuration

**noice.nvim** comes with the following defaults:
Expand All @@ -86,7 +84,7 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
search_up = { kind = "search", pattern = "^%?", icon = " ", lang = "regex" },
filter = { pattern = "^:%s*!", icon = "$", lang = "bash" },
lua = { pattern = "^:%s*lua%s+", icon = "", lang = "lua" },
help = { pattern = "^:%s*h%s+", icon = "" },
help = { pattern = "^:%s*he?l?p?%s+", icon = "" },
input = {}, -- Used by input()
-- lua = false, -- to disable a format, set to `false`
},
Expand All @@ -98,7 +96,7 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
view = "notify", -- default view for messages
view_error = "notify", -- view for errors
view_warn = "notify", -- view for warnings
view_history = "split", -- view for :messages
view_history = "messages", -- view for :messages
view_search = "virtualtext", -- view for search count messages. Set to `false` to disable
},
popupmenu = {
Expand All @@ -116,13 +114,29 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
-- options for the message history that you get with `:Noice`
view = "split",
opts = { enter = true, format = "details" },
filter = { event = { "msg_show", "notify" }, ["not"] = { kind = { "search_count", "echo" } } },
filter = {
any = {
{ event = "notify" },
{ error = true },
{ warning = true },
{ event = "msg_show", kind = { "" } },
{ event = "lsp", kind = "message" },
},
},
},
-- :Noice last
last = {
view = "popup",
opts = { enter = true, format = "details" },
filter = { event = { "msg_show", "notify" }, ["not"] = { kind = { "search_count", "echo" } } },
filter = {
any = {
{ event = "notify" },
{ error = true },
{ warning = true },
{ event = "msg_show", kind = { "" } },
{ event = "lsp", kind = "message" },
},
},
filter_opts = { count = 1 },
},
-- :Noice errors
Expand Down Expand Up @@ -155,6 +169,14 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
throttle = 1000 / 30, -- frequency to update lsp progress message
view = "mini",
},
override = {
-- override the default lsp markdown formatter with Noice
["vim.lsp.util.convert_input_to_markdown_lines"] = false,
-- override the lsp markdown formatter with Noice
["vim.lsp.util.stylize_markdown"] = false,
-- override cmp documentation with Noice (needs the other options to work)
["cmp.entry.get_documentation"] = false,
},
hover = {
enabled = false,
view = nil, -- when nil, use defaults from documentation
Expand All @@ -163,11 +185,22 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
},
signature = {
enabled = false,
auto_open = true, -- Automatically show signature help when typing a trigger character from the LSP
auto_open = {
enabled = true,
trigger = true, -- Automatically show signature help when typing a trigger character from the LSP
luasnip = true, -- Will open signature help when jumping to Luasnip insert nodes
throttle = 50, -- Debounce lsp signature help request by 50ms
},
view = nil, -- when nil, use defaults from documentation
---@type NoiceViewOptions
opts = {}, -- merged with defaults from documentation
},
message = {
-- Messages shown by lsp servers
enabled = false,
view = "notify",
opts = {},
},
-- defaults for hover and signature help
documentation = {
view = "hover",
Expand Down
8 changes: 8 additions & 0 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ function M.defaults()
throttle = 1000 / 30, -- frequency to update lsp progress message
view = "mini",
},
override = {
-- override the default lsp markdown formatter with Noice
["vim.lsp.util.convert_input_to_markdown_lines"] = false,
-- override the lsp markdown formatter with Noice
["vim.lsp.util.stylize_markdown"] = false,
-- override cmp documentation with Noice (needs the other options to work)
["cmp.entry.get_documentation"] = false,
},
hover = {
enabled = false,
view = nil, -- when nil, use defaults from documentation
Expand Down
14 changes: 10 additions & 4 deletions lua/noice/lsp/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ local M = {}

-- Formats the content and adds it to the message
---@param contents MarkupContents Markup content
---@param message NoiceMessage Noice message
function M.format(message, contents)
function M.format_markdown(contents)
if type(contents) ~= "table" or not vim.tbl_islist(contents) then
contents = { contents }
end
Expand All @@ -20,7 +19,7 @@ function M.format(message, contents)

for _, content in ipairs(contents) do
if type(content) == "string" then
table.insert(parts, content)
table.insert(parts, ("```\n%s\n```"):format(content))
elseif content.language then
table.insert(parts, ("```%s\n%s\n```"):format(content.language, content.value))
elseif content.kind == "markdown" then
Expand All @@ -30,7 +29,14 @@ function M.format(message, contents)
end
end

local text = table.concat(parts, "\n")
return vim.split(table.concat(parts, "\n"), "\n")
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)
local text = table.concat(M.format_markdown(contents), "\n")
Markdown.format(message, text)
return message
end
Expand Down
35 changes: 35 additions & 0 deletions lua/noice/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local require = require("noice.util.lazy")
local Markdown = require("noice.text.markdown")

local Manager = require("noice.message.manager")
local Config = require("noice.config")
Expand Down Expand Up @@ -66,6 +67,40 @@ function M.setup()
vim.defer_fn(M.on_close, 10)
end,
})

if Config.options.lsp.override["cmp.entry.get_documentation"] then
require("cmp.entry").get_documentation = function(self)
local item = self:get_completion_item()
if item.documentation then
return Format.format_markdown(item.documentation)
end
return {}
end
end

if Config.options.lsp.override["vim.lsp.util.convert_input_to_markdown_lines"] then
vim.lsp.util.convert_input_to_markdown_lines = function(input, contents)
contents = contents or {}
local ret = Format.format_markdown(input)
vim.list_extend(contents, ret)
return ret
end
end

if Config.options.lsp.override["vim.lsp.util.stylize_markdown"] then
vim.lsp.util.stylize_markdown = function(buf, contents, _opts)
local text = table.concat(contents, "\n")
local message = Message("lsp")
Markdown.format(message, text)
message:render(buf, Config.ns)
Markdown.keys(buf)
if not vim.b[buf].ts_highlight then
if not pcall(vim.treesitter.start, buf, "markdown") then
vim.bo[buf].syntax = "markdown"
end
end
end
end
end

---@enum MessageType
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 @@ -59,7 +59,7 @@ function M.setup(group)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client.server_capabilities.signatureHelpProvider then
local chars = client.server_capabilities.signatureHelpProvider.triggerCharacters
if #chars > 0 then
if chars and #chars > 0 then
local callback = M.check(buf, chars, client.offset_encoding)
if Config.options.lsp.signature.auto_open.luasnip then
vim.api.nvim_create_autocmd("User", {
Expand Down

0 comments on commit 6d9fb12

Please sign in to comment.