Skip to content

Commit

Permalink
feat: added support for filtering messages generated by a command on …
Browse files Browse the repository at this point in the history
…the commandline
  • Loading branch information
folke committed Nov 13, 2022
1 parent ccb7e56 commit b3ee385
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 50 deletions.
95 changes: 68 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Install the plugin with your preferred package manager:
use({
"folke/noice.nvim",
config = function()
require("noice").setup()
require("noice").setup({
-- add any options here
})
end,
requires = {
-- if you lazy-load any plugin below, make sure to add proper `module="..."` entries
Expand All @@ -57,6 +59,30 @@ use({
})
```

Suggested setup:

```lua
require("noice").setup({
lsp = {
-- override markdown rendering so that **cmp** and other plugins use **Treesitter**
override = {
["vim.lsp.util.convert_input_to_markdown_lines"] = true,
["vim.lsp.util.stylize_markdown"] = true,
["cmp.entry.get_documentation"] = true,
},
},
-- you can enable a preset for easier configuration
presets = {
bottom_search = true, -- use a classic bottom cmdline for search
command_palette = true, -- position the cmdline and popupmenu together
long_message_to_split = true, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim
lsp_doc_border = false, -- add a border to hover docs and signature help
cmdline_output_to_split = false, -- send the output of a command you executed in the cmdline to a split
},
})
```

It's a good idea to run `:checkhealth noice` after installing to check for common issues.

<details><summary>vim-plug</summary>
Expand Down Expand Up @@ -260,6 +286,7 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)
long_message_to_split = false, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim
lsp_doc_border = false, -- add a border to hover docs and signature help
cmdline_output_to_split = false, -- send the output of a command you executed in the cmdline to a split
},
throttle = 1000 / 30, -- how frequently does Noice need to check for ui updates? This has no effect when in blocking mode.
---@type NoiceConfigViews
Expand Down Expand Up @@ -299,22 +326,24 @@ Check the [wiki](https://github.com/folke/noice.nvim/wiki/Configuration-Recipes)

| Name | Type | Description |
| -------------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------ |
| **cleared** | `boolean` | checks if the message is cleared, meaning it's in the history |
| **mode** | `string` | checks if `vim.api.nvim_get_mode()` contains the given mode |
| **any** | `filter[]` | checks that at least one of the filters matches |
| **blocking** | `boolean` | are we in blocking mode? |
| **event** | `string` or `string[]` | any of the events from `ext_messages` or `cmdline`. See [:h ui-messages](https://neovim.io/doc/user/ui.html#ui-messages) |
| **kind** | `string` or `string[]` | any of the kinds from `ext_messages`. See [:h ui-messages](https://neovim.io/doc/user/ui.html#ui-messages) |
| **cleared** | `boolean` | checks if the message is cleared, meaning it's in the history |
| **cmdline** | `boolean` or `string` | checks if the message was generated by executing a cmdline. When `string`, then it is used as a pattern |
| **error** | `boolean` | all error-like kinds from `ext_messages` |
| **warning** | `boolean` | all warning-like kinds from `ext_messages` |
| **event** | `string` or `string[]` | any of the events from `ext_messages` or `cmdline`. See [:h ui-messages](https://neovim.io/doc/user/ui.html#ui-messages) |
| **find** | `string` | uses lua `string.find` to match the pattern |
| **min_height** | `number` | minimum height of the message |
| **has** | `boolean` | checks if the message is exists, meaning it's in the history |
| **kind** | `string` or `string[]` | any of the kinds from `ext_messages`. See [:h ui-messages](https://neovim.io/doc/user/ui.html#ui-messages) |
| **max_height** | `number` | maximum height of the message |
| **min_width** | `number` | minimum width of the message |
| **max_length** | `number` | maximum length of the message (total width of all the lines) |
| **max_width** | `number` | maximum width of the message |
| **min_height** | `number` | minimum height of the message |
| **min_length** | `number` | minimum length of the message (total width of all the lines) |
| **max_length** | `number` | maximum length of the message (total width of all the lines) |
| **min_width** | `number` | minimum width of the message |
| **mode** | `string` | checks if `vim.api.nvim_get_mode()` contains the given mode |
| **not** | `filter` | checks wether the filter matches or not |
| **any** | `filter[]` | checks that at least one of the filters matches |
| **warning** | `boolean` | all warning-like kinds from `ext_messages` |

<details>
<summary>Example:</summary>
Expand All @@ -340,20 +369,25 @@ local filter = {
- **notify**: powered by [nvim-notify](https://github.com/rcarriga/nvim-notify)
- **virtualtext**: shows the message as virtualtext (for example for `search_count`)
- **mini**: similar to [notifier.nvim](https://github.com/vigoux/notifier.nvim) & [fidget.nvim](https://github.com/j-hui/fidget.nvim)
- **notify_send**: generate a desktop notification

A **View** (`config.views`) is a combination of a `backend` and options.
**Noice** comes with the following built-in views with sane defaults:

| View | Backend | Description |
| ----------------- | ---------- | ---------------------------------------------------------------------------------- |
| **notify** | `notify` | _nvim-notify_ with `level=true`, `replace=true`, `merge=true` |
| **split** | `split` | horizontal split |
| **vsplit** | `split` | vertical split |
| **popup** | `popup` | simple popup |
| **mini** | `mini` | minimal view, by default bottom right, right-aligned |
| **cmdline** | `popup` | bottom line, similar to the classic cmdline |
| **cmdline_popup** | `popup` | fancy cmdline popup, with different styles according to the cmdline mode |
| **popupmenu** | `nui.menu` | special view with the options used to render the popupmenu when backend is **nui** |
| View | Backend | Description |
| ------------------ | ---------- | ---------------------------------------------------------------------------------- |
| **notify** | `notify` | _nvim-notify_ with `level=nil`, `replace=false`, `merge=false` |
| **split** | `split` | horizontal split |
| **vsplit** | `split` | vertical split |
| **popup** | `popup` | simple popup |
| **mini** | `mini` | minimal view, by default bottom right, right-aligned |
| **cmdline** | `popup` | bottom line, similar to the classic cmdline |
| **cmdline_popup** | `popup` | fancy cmdline popup, with different styles according to the cmdline mode |
| **cmdline_output** | `split` | split used by `config.presets.cmdline_output_to_split` |
| **messages** | `split` | split used for `:messages` |
| **confirm** | `popup` | popup used for `confirm` events |
| **hover** | `popup` | popup used for lsp signature help and hover |
| **popupmenu** | `nui.menu` | special view with the options used to render the popupmenu when backend is **nui** |

Please refer to [noice.config.views](https://github.com/folke/noice.nvim/blob/main/lua/noice/config/views.lua)
to see the options.
Expand Down Expand Up @@ -411,12 +445,12 @@ String or can also be a table like:

### Notify Options

| Option | Type | Default | Description |
| ----------- | ---------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| **title** | `string` | `nil` | title to be used for the notification. Uses `Message.title` if available. |
| **replace** | `boolean` | `true` | when true, messages routing to the same notify instance will replace existing messages instead of pushing a new notification every time |
| **merge** | `boolean` | `true` | Merge messages into one Notification or create separate notifications |
| **level** | `number\|string` | `"info"` | notification level. Uses `Message.level` if available. |
| Option | Type | Default | Description |
| ----------- | ---------------- | ---------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| **title** | `string` | `"Notification"` | title to be used for the notification. Uses `Message.title` if available. |
| **replace** | `boolean` | `false` | when true, messages routing to the same notify instance will replace existing messages instead of pushing a new notification every time |
| **merge** | `boolean` | `false` | Merge messages into one Notification or create separate notifications |
| **level** | `number\|string` | `nil` | notification level. Uses `Message.level` if available. |

### Virtual Text Options

Expand All @@ -437,6 +471,10 @@ For a list of the defaults, please refer to [config.format](https://github.com/f
- **date**: formatted date with optional date format string
- **message**: message content itself with optional `hl_group` to override message highlights
- **confirm**: only useful for `confirm` messages. Will format the choices as buttons.
- **cmdline**: will render the cmdline in the message that generated the message.
- **progress**: progress bar used by lsp progress
- **spinner**: spinners used by lsp progress
- **data**: render any custom data from `Message.opts`. Useful in combination with the opts passed to `vim.notify`

Formatters are used in `format` definitions. **Noice** includes the following built-in formats:

Expand All @@ -451,13 +489,16 @@ Formatters are used in `format` definitions. **Noice** includes the following bu
"{level} ",
"{date} ",
"{event}",
{ "{kind}", before = { ".", hl_group = "Comment" } },
{ "{kind}", before = { ".", hl_group = "NoiceFormatKind" } },
" ",
"{title} ",
"{cmdline} ",
"{message}",
},
telescope = ..., -- formatter used to display telescope results
telescope_preview = ..., -- formatter used to preview telescope results
lsp_progress = ..., -- formatter used by lsp progress
lsp_progress_done = ..., -- formatter used by lsp progress
}
```

Expand Down
2 changes: 1 addition & 1 deletion lua/noice/config/cmdline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ function M.setup()
kind = kind,
icon_hl_group = "Noice" .. hl_group_icon,
view = Config.options.cmdline.view,
lang = format.lang or format.ft,
opts = {
---@diagnostic disable-next-line: undefined-field
lang = format.lang or format.ft,
border = {
text = {
top = format.title or (" " .. kind_cc .. " "),
Expand Down
3 changes: 3 additions & 0 deletions lua/noice/config/format.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ M.builtin = {
{ "{kind}", before = { ".", hl_group = "NoiceFormatKind" } },
" ",
"{title} ",
"{cmdline} ",
"{message}",
},
telescope = {
Expand Down Expand Up @@ -55,6 +56,8 @@ M.defaults = {
debug = {
enabled = true,
},
---@class NoiceFormatOptions.cmdline
cmdline = {},
---@class NoiceFormatOptions.level
level = {
hl_group = {
Expand Down
1 change: 1 addition & 0 deletions lua/noice/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function M.defaults()
long_message_to_split = false, -- long messages will be sent to a split
inc_rename = false, -- enables an input dialog for inc-rename.nvim
lsp_doc_border = false, -- add a border to hover docs and signature help
cmdline_output_to_split = false, -- send the output of a command you executed in the cmdline to a split
},
throttle = 1000 / 30, -- how frequently does Noice need to check for ui updates? This has no effect when in blocking mode.
---@type NoiceConfigViews
Expand Down
11 changes: 9 additions & 2 deletions lua/noice/config/preset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ M.presets = {
routes = {
{
filter = { event = "msg_show", min_height = 20 },
view = "split",
opts = { enter = true },
view = "cmdline_output",
},
},
},
Expand All @@ -121,6 +120,14 @@ M.presets = {
},
},
},
cmdline_output_to_split = {
routes = {
{
view = "cmdline_output",
filter = { cmdline = "^:" },
},
},
},
}

return M
4 changes: 4 additions & 0 deletions lua/noice/config/views.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ M.defaults = {
wrap = true,
},
},
cmdline_output = {
format = "details",
view = "split",
},
messages = {
view = "split",
enter = true,
Expand Down
37 changes: 25 additions & 12 deletions lua/noice/message/filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ local M = {}
---@alias NoiceFilterFun fun(message: NoiceMessage, ...): boolean

---@class NoiceFilter
---@field any? NoiceFilter[]
---@field blocking? boolean
---@field cleared? boolean
---@field cmdline? string|boolean
---@field error? boolean
---@field event? NoiceEvent|NoiceEvent[]
---@field find? string
---@field has? boolean
---@field kind? NoiceKind|NoiceKind[]
---@field message? NoiceMessage|NoiceMessage[]
---@field any? NoiceFilter[]
---@field not? NoiceFilter
---@field min_height? integer
---@field max_height? integer
---@field min_width? integer
---@field max_length? integer
---@field max_width? integer
---@field message? NoiceMessage|NoiceMessage[]
---@field min_height? integer
---@field min_length? integer
---@field max_length? integer
---@field find? string
---@field error? boolean
---@field has? boolean
---@field warning? boolean
---@field min_width? integer
---@field mode? string
---@field blocking? boolean
---@field cleared? boolean
---@field not? NoiceFilter
---@field warning? boolean

-----@type table<string, NoiceFilterFun>
M.filters = {
Expand Down Expand Up @@ -53,6 +54,18 @@ M.filters = {
kind = type(kind) == "table" and kind or { kind }
return vim.tbl_contains(kind, message.kind)
end,
cmdline = function(message, cmdline)
---@cast message NoiceMessage
---@cast cmdline string|boolean
if type(cmdline) == "boolean" then
return (message.cmdline ~= nil) == cmdline
end
if message.cmdline then
local str = message.cmdline.state.firstc .. message.cmdline:get()
return str:find(cmdline)
end
return false
end,
message = function(message, other)
---@cast message NoiceMessage
other = vim.tbl_islist(other) and other or { other }
Expand Down
1 change: 1 addition & 0 deletions lua/noice/message/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local _id = 0
---@field tick number
---@field level? NotifyLevel
---@field kind? NoiceKind
---@field cmdline? NoiceCmdline
---@field _debug? boolean
---@field opts table<string, any>
---@overload fun(event: NoiceEvent, kind?: NoiceKind, content?: NoiceContent|NoiceContent[]): NoiceMessage
Expand Down
2 changes: 1 addition & 1 deletion lua/noice/text/block.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local Object = require("nui.object")
---@alias NoiceContent string|NoiceChunk|NuiLine|NuiText|NoiceBlock

---@class NoiceBlock
---@field private _lines NuiLine[]
---@field _lines NuiLine[]
---@overload fun(content?: NoiceContent|NoiceContent[], highlight?: string|table): NoiceBlock
local Block = Object("Block")

Expand Down
10 changes: 9 additions & 1 deletion lua/noice/text/format/formatters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function M.debug(message, opts)
vim.tbl_filter(
---@param t string
function(t)
return t
return t ~= nil
end,
debug
),
Expand Down Expand Up @@ -154,6 +154,14 @@ function M.spinner(message, opts)
message:append(require("noice.util.spinners").spin(opts.name), opts.hl_group)
end

---@param message NoiceMessage
---@param _opts NoiceFormatOptions.cmdline
function M.cmdline(message, _opts)
if message.cmdline then
message.cmdline:format(message, true)
end
end

---@param message NoiceMessage
---@param input NoiceMessage
---@param opts NoiceFormatOptions.confirm
Expand Down
Loading

0 comments on commit b3ee385

Please sign in to comment.