Skip to content

Commit

Permalink
feat(log): log $/progress handler invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
j-hui committed Feb 4, 2024
1 parent f03a2d6 commit 8d4cd3b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 26 deletions.
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Available options are shown below:
-- Options related to Neovim's built-in LSP client
lsp = {
progress_ringbuf_size = 0, -- Configure the nvim's LSP progress ring buffer size
log_handler = false, -- Log `$/progress` handler invocations (for debugging)
},
},

Expand Down Expand Up @@ -276,7 +277,6 @@ source code. You are encouraged to hack around with that.
:put = execute('lua print(dofile([[lua/fidget/commands.lua]]).make_panvimdocs())')
-->


<!-- {{{ Generated from fidget.commands.lua -->

## Commands
Expand Down Expand Up @@ -306,11 +306,10 @@ Clear active notifications

Positional arguments:

- **`{group_key}`**: _`(any)`_ group to clear
- **`{group_key}`**: _`(any)`_ group to clear

</details>


#### `:Fidget clear_history`

Clear notifications history
Expand All @@ -320,19 +319,18 @@ Clear notifications history

Flags:

- **`--before {seconds}`**: _`(number)`_ filter history for items updated at least this long ago
- **`--group_key {group_key}`**: _`(any)`_ clear history by group key
- **`--include_active {true|false}`**: _`(boolean)`_ whether to clear items that have not been removed (default: true)
- **`--include_removed {true|false}`**: _`(boolean)`_ whether to clear items that have have been removed (default: true)
- **`--since {seconds}`**: _`(number)`_ filter history for items updated at most this long ago
- **`--before {seconds}`**: _`(number)`_ filter history for items updated at least this long ago
- **`--group_key {group_key}`**: _`(any)`_ clear history by group key
- **`--include_active {true|false}`**: _`(boolean)`_ whether to clear items that have not been removed (default: true)
- **`--include_removed {true|false}`**: _`(boolean)`_ whether to clear items that have have been removed (default: true)
- **`--since {seconds}`**: _`(number)`_ filter history for items updated at most this long ago

Positional arguments:

- **`{group_key}`**: _`(any)`_ clear history by group key
- **`{group_key}`**: _`(any)`_ clear history by group key

</details>


#### `:Fidget history`

Show notifications history
Expand All @@ -342,19 +340,18 @@ Show notifications history

Flags:

- **`--before {seconds}`**: _`(number)`_ filter history for items updated at least this long ago
- **`--group_key {group_key}`**: _`(any)`_ filter history by group key
- **`--include_active {true|false}`**: _`(boolean)`_ whether to clear items that have not been removed (default: `true`)
- **`--include_removed {true|false}`**: _`(boolean)`_ whether to clear items that have have been removed (default: `true`)
- **`--since {seconds}`**: _`(number)`_ filter history for items updated at most this long ago
- **`--before {seconds}`**: _`(number)`_ filter history for items updated at least this long ago
- **`--group_key {group_key}`**: _`(any)`_ filter history by group key
- **`--include_active {true|false}`**: _`(boolean)`_ whether to clear items that have not been removed (default: `true`)
- **`--include_removed {true|false}`**: _`(boolean)`_ whether to clear items that have have been removed (default: `true`)
- **`--since {seconds}`**: _`(number)`_ filter history for items updated at most this long ago

Positional arguments:

- **`{group_key}`**: _`(any)`_ filter history by group key
- **`{group_key}`**: _`(any)`_ filter history by group key

</details>


#### `:Fidget lsp_suppress`

Suppress LSP progress notifications
Expand All @@ -364,11 +361,10 @@ Suppress LSP progress notifications

Positional arguments:

- **`{suppress}`**: _`(boolean)`_ whether to suppress (omitting this argument toggles suppression)
- **`{suppress}`**: _`(boolean)`_ whether to suppress (omitting this argument toggles suppression)

</details>


#### `:Fidget suppress`

Suppress notification window
Expand All @@ -378,11 +374,10 @@ Suppress notification window

Positional arguments:

- **`{suppress}`**: _`(boolean)`_ whether to suppress (omitting this argument toggles suppression)
- **`{suppress}`**: _`(boolean)`_ whether to suppress (omitting this argument toggles suppression)

</details>


<!-- panvimdoc-ignore-end -->

<!-- panvimdoc-include-comment
Expand Down Expand Up @@ -446,7 +441,6 @@ Positional arguments:

<!-- Generated from fidget.commands.lua }}} -->


## Highlights

Rather than defining its own highlights, Fidget's default configuration uses
Expand Down
18 changes: 18 additions & 0 deletions doc/fidget-option.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ progress.lsp.progress_ringbuf_size *fidget.option.progress.lsp.progress_ringbuf
`0`


progress.lsp.log_handler *fidget.option.progress.lsp.log_handler*

Log `$/progress` handler invocations (for debugging)

Works by wrapping `vim.lsp.handlers["$/progress"]` with Fidget's logger.
Invocations are logged at the `vim.log.levels.INFO` level.

This option exists primarily for debugging; leaving this set to `true` is
not recommended.

Since it works by overriding Neovim's existing `$/progress` handler, you
should not use this function if you'd like Neovim to use your overriden
`$/progress` handler! It will conflict with the handler that Fidget tries
to install for logging purposes.
Default: ~
`false`


==============================================================================
Notification options *fidget.option.notification*

Expand Down
41 changes: 37 additions & 4 deletions lua/fidget/progress/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---@mod fidget.progress.lsp Neovim LSP shim layer
local M = {}
local logger = require("fidget.logger")
local M = {}
local logger = require("fidget.logger")

---@class ProgressMessage
---@field token Key Unique identifier used to accumulate updates
Expand All @@ -13,12 +13,16 @@ local logger = require("fidget.logger")

--- Autocmd ID for the LSPAttach event.
---@type number?
local lsp_attach_autocmd = nil
local lsp_attach_autocmd = nil

--- Built-in `$/progress` handler.
---@type function
local lsp_progress_handler = vim.lsp.handlers["$/progress"]

---@options progress.lsp [[
---@protected
--- Nvim LSP client options
M.options = {
M.options = {
--- Configure the nvim's LSP progress ring buffer size
---
--- Useful for avoiding progress message overflow when the LSP server blasts
Expand All @@ -29,6 +33,20 @@ M.options = {
---
---@type number
progress_ringbuf_size = 0,

--- Log `$/progress` handler invocations (for debugging)
---
--- Works by wrapping `vim.lsp.handlers["$/progress"]` with Fidget's logger.
--- Invocations are logged at the `vim.log.levels.INFO` level.
---
--- This option exists primarily for debugging; leaving this set to `true` is
--- not recommended.
---
--- Since it works by overriding Neovim's existing `$/progress` handler, you
--- should not use this function if you'd like Neovim to use your overriden
--- `$/progress` handler! It will conflict with the handler that Fidget tries
--- to install for logging purposes.
log_handler = false,
}
---@options ]]

Expand All @@ -37,6 +55,7 @@ require("fidget.options").declare(M, "progress.lsp", M.options, function()
vim.api.nvim_del_autocmd(lsp_attach_autocmd)
lsp_attach_autocmd = nil
end

if vim.ringbuf and M.options.progress_ringbuf_size > 0 then
logger.info("Setting LSP progress ringbuf size to", M.options.progress_ringbuf_size)
lsp_attach_autocmd = vim.api.nvim_create_autocmd("LspAttach", {
Expand All @@ -47,6 +66,20 @@ require("fidget.options").declare(M, "progress.lsp", M.options, function()
end
})
end

if M.options.log_handler then
vim.lsp.handlers["$/progress"] = function(x, msg, ctx)
local client = vim.lsp.get_client_by_id(ctx.client_id)
local tag = string.format("nvim $/progress handler for client %d (%s)", ctx.client_id, client.name)
logger.info(tag, "invoked:", msg)

local result = lsp_progress_handler(x, msg, ctx)

if result ~= nil then
logger.info(tag, "returned:", result)
end
end
end
end)

--- Consumes LSP progress messages from each client.progress ring buffer.
Expand Down

0 comments on commit 8d4cd3b

Please sign in to comment.