From 6d9fb1242cd782edfb77ac06b12d72ed24e3888f Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 28 Oct 2022 16:04:37 +0200 Subject: [PATCH] feat: added overrides for default lsp and cmp formatters :) Enable with config.lsp.override --- README.md | 51 ++++++++++++++++++++++++++++++------- lua/noice/config/init.lua | 8 ++++++ lua/noice/lsp/format.lua | 14 +++++++--- lua/noice/lsp/init.lua | 35 +++++++++++++++++++++++++ lua/noice/lsp/signature.lua | 2 +- 5 files changed, 96 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 99223df1..1d567e69 100644 --- a/README.md +++ b/README.md @@ -46,9 +46,9 @@ use({ "rcarriga/nvim-notify", } }) -``` -
vim-plug +< +``` ```viml " vim-plug @@ -60,8 +60,6 @@ call plug#end() lua require("noice").setup() ``` -
- ## ⚙️ Configuration **noice.nvim** comes with the following defaults: @@ -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` }, @@ -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 = { @@ -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 @@ -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 @@ -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", diff --git a/lua/noice/config/init.lua b/lua/noice/config/init.lua index 1940a896..7ff14e7b 100644 --- a/lua/noice/config/init.lua +++ b/lua/noice/config/init.lua @@ -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 diff --git a/lua/noice/lsp/format.lua b/lua/noice/lsp/format.lua index fabefec9..925aad90 100644 --- a/lua/noice/lsp/format.lua +++ b/lua/noice/lsp/format.lua @@ -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 @@ -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 @@ -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 diff --git a/lua/noice/lsp/init.lua b/lua/noice/lsp/init.lua index d74ae15d..020c583e 100644 --- a/lua/noice/lsp/init.lua +++ b/lua/noice/lsp/init.lua @@ -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") @@ -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 diff --git a/lua/noice/lsp/signature.lua b/lua/noice/lsp/signature.lua index ee5955ef..41a9f5af 100644 --- a/lua/noice/lsp/signature.lua +++ b/lua/noice/lsp/signature.lua @@ -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", {