Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] add update_in_insert and lazy_update_in_insert options #142

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ require("lspconfig").clangd.setup {

>NOTE: You can set `vim.b.navic_lazy_update_context = true` for specific buffers, where you want the the updates to not occur on every `CursorMoved` event. It should help if you are facing performance issues in large files. Read the docs for example usage of this variable. Alternatively, you can pass `lazy_update_context=true` to the `setup` function to turn off context updates on the `CursorMoved` event completely for all buffers. It's useful when you just want context updates to happen only on `CursorHold` events and not on `CursorMoved`.

>NOTE: You can set `vim.b.navic_lazy_update_in_insert = false` for specific buffers, where you don't want for context updates to occur on `CursorMovedI` events. Just as with `vim.b.navic_lazy_update_context = true`, this should help mitigate performance issues in large files, for Insert mode.

>NOTE: You can set `vim.b.navic_update_in_insert = false` to completely disable context updates for specific buffers, for `CursorMovedI` and `CursorHoldI` events.

## 🪄 Customise

Use the `setup` function to modify default parameters.
Expand All @@ -80,6 +84,7 @@ Use the `setup` function to modify default parameters.
* `depth_limit_indicator` : Icon to indicate that `depth_limit` was hit and the shown context is truncated.
* `format_text` : A function to customize the text displayed in each segment.
* `lazy_update_context` : If true, turns off context updates for the "CursorMoved" event.
* `update_in_insert` : If true, turns on context updates for the "CursorMovedI" and "CursorHoldI" events.
* `safe_output` : Sanitize the output for use in statusline and winbar.
* `click` : Single click to goto element, double click to open nvim-navbuddy on the clicked element.
* `lsp` :
Expand Down Expand Up @@ -126,6 +131,8 @@ navic.setup {
depth_limit_indicator = "..",
safe_output = true,
lazy_update_context = false,
update_in_insert = false,
lazy_update_in_insert = false,
click = false,
format_text = function(text)
return text
Expand Down
28 changes: 28 additions & 0 deletions doc/navic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,23 @@ you are facing performance issues on large files. Example usage
end,
})
<

*vim.b.navic_lazy_update_in_insert*
Set it to true to update context only on CursorHoldI event, if using `update_in_insert`. Could be usefull if
you are facing performance issues on large files. Example usage
>lua
vim.api.nvim_create_autocmd("BufEnter", {
callback = function()
if vim.api.nvim_buf_line_count(0) > 10000 then
vim.b.navic_lazy_update_in_insert = true
end
end,
})
<

*vim.b.navic_update_in_insert*
Set it to false to disable updating context on CursorHoldI and CursorMovedI events.

=============================================================================
Customisation *navic-customise*

Expand Down Expand Up @@ -243,6 +260,15 @@ Use |navic.setup| to override any of the default options
behavior with more flexibility, like disabling context updates
depending on file size, total lines etc.

update_in_insert: boolean
If true, turns on context updates for "CursorHoldI" and "CursorMovedI" events,
to see changes in context while in Insert mode.

lazy_update_in_insert: boolean
If true, turns off context updates for the "CursorMovedI" event,
with updates happening only on the "CursorHoldI" event.
The same benefits as with the `lazy_update_context` option apply, just for Insert mode.

lsp :
auto_attach: boolean
Enable to have nvim-navic automatically attach to every LSP for
Expand Down Expand Up @@ -296,6 +322,8 @@ Defaults >lua
depth_limit_indicator = "..",
safe_output = true,
lazy_update_context = false,
update_in_insert = false,
lazy_update_in_insert = false,
click = false
}
<
Expand Down
37 changes: 37 additions & 0 deletions lua/nvim-navic/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local lib = require("nvim-navic.lib")
---@field depth_limit number | nil
---@field depth_limit_indicator string | nil
---@field lazy_update_context boolean | nil
---@field update_in_insert boolean | nil
---@field lazy_update_in_insert boolean | nil
---@field safe_output boolean | nil
---@field click boolean | nil
---@field lsp LspOptions | nil
Expand Down Expand Up @@ -56,6 +58,8 @@ local config = {
depth_limit_indicator = "..",
safe_output = true,
lazy_update_context = false,
update_in_insert = false,
lazy_update_in_insert = false,
click = false,
lsp = {
auto_attach = false,
Expand Down Expand Up @@ -147,6 +151,12 @@ function M.setup(opts)
if opts.lazy_update_context then
config.lazy_update_context = opts.lazy_update_context
end
if opts.update_in_insert then
config.update_in_insert = opts.update_in_insert
end
if opts.lazy_update_in_insert then
config.lazy_update_in_insert = opts.lazy_update_in_insert
end
if opts.click then
config.click = opts.click
end
Expand Down Expand Up @@ -385,6 +395,33 @@ function M.attach(client, bufnr)
buffer = bufnr,
})
end
if config.update_in_insert then
vim.api.nvim_create_autocmd("CursorHoldI", {
callback = function()
if type(vim.b.navic_update_in_insert) == "nil"
or vim.b.navic_update_in_insert ~= false
then
lib.update_context(bufnr)
end
end,
group = navic_augroup,
buffer = bufnr,
})
if config.lazy_update_context ~= true then
vim.api.nvim_create_autocmd("CursorMovedI", {
callback = function()
if vim.b.navic_lazy_update_in_insert ~= true
and (type(vim.b.navic_update_in_insert) == "nil"
or vim.b.navic_update_in_insert ~= false)
then
lib.update_context(bufnr)
end
end,
group = navic_augroup,
buffer = bufnr,
})
end
end
vim.api.nvim_create_autocmd("BufDelete", {
callback = function()
lib.clear_buffer_data(bufnr)
Expand Down