This is a plugin for neovim that helps you contextual code used for AI prompts.
context-nvim
allows you to manage two types of contexts:
- Manual Contexts: These are contexts that you manually define and manage. You can add, remove, and edit these contexts with
context-nvim
subcommands. - History Contexts: These are contexts that are automatically generated by
context-nvim
based on buffers you have opened recently.
- Telescope picker for viewing / deleting manual contexts
- Telescope picker for viewing / deleting history contexts
- subcommands for managing manual contexts
nvim-cmp
source for referencing history-based and manual contexts in your AI prompts
Using lazy.nvim:
{
"napisani/context-nvim",
config = function()
require("context_nvim").setup({ })
end,
},
require("context_nvim").setup({
enable_history = true, -- whether to enable history context by tracking opened files/buffers
history_length = 10, -- how many history items to track
history_for_files_only = true, -- only use history for files, any non-file buffers will be ignored
history_pattern = "*", -- history pattern to match files/buffers that will be tracked
root_dir = ".", -- root directory of the project, used for finding files and constructing paths
cmp = {
enable = true, -- whether to enable the nvim-cmp source for referencing contexts
register_cmp_avante = true, -- whether to include the cmp source for avante input buffers.
-- They need to be registered using an autocmd, so this is a separate config option
manual_context_keyword = "@manual_context", -- keyword to use for manual context
history_keyword = "@history_context", -- keyword to use for history context
prompt_keyword = "@prompt", -- keyword to use for prompt context
},
blink = {
manual_context_keyword = "@manual_context", -- keyword to use for manual context
history_keyword = "@history_context", -- keyword to use for history context
prompt_keyword = "@prompt", -- keyword to use for prompt context
},
telescope = {
enable = true, -- whether to enable the telescope picker
},
logger = {
level = "error", -- log level for the plenary logger
},
lsp = {
ignore_sources = {}, -- lsp sources to ignore when adding line diagnostics to the manual context
},
prompts = {
{
name = 'unit tests', -- the name of the prompt (required)
prompt = 'Generate a suite of unit tests using Jest, respond with only code.', -- the prompt text (required)
cmp = 'jest' -- an alternate name for the cmp completion source (optional) defaults to 'name'
},
}
})
Adds the current file to the manual context.
:ContextNvim add_current_file
Adds the current visual selection or buffer to the manual context.
:ContextNvim add_current
Adds all items from the quickfix list to the manual context.
:ContextNvim add_qflist
Clears the history context.
:ContextNvim clear_history
Clears the manual context.
:ContextNvim clear_manual
Opens a Telescope picker to select and add a directory to the manual context.
:ContextNvim add_dir
Opens a Telescope picker to select and add a file to the manual context.
:ContextNvim add_file
Opens a Telescope picker to view and manage the history context.
:ContextNvim find_context_history
Opens a Telescope picker to view and manage the manual context.
:ContextNvim find_context_manual
Adds context for LSP line diagnostics to the manual context.
:ContextNvim add_line_lsp_daig
Pastes all manual contexts at the current cursor position.
:ContextNvim paste_manual
Pastes all history contexts at the current cursor position.
:ContextNvim paste_history
Brings up a telescope picker, when an entry is selected. The prompt will be inserted into the buffer.
:ContextNvim insert_prompt
To use context-nvim with blink.nvim, you need to:
- Enable it in the context-nvim config:
require("context_nvim").setup({
...
blink = {
manual_context_keyword = "@manual_context",
history_keyword = "@history_context",
prompt_keyword = "@prompt",
}
...
})
- Configure blink.nvim to use the context-nvim source:
{
sources = {
default = { ..., "context_nvim" }, -- to add the context_nvim source to all filetypes
per_filetype = { codecompanion = { "context_nvim" } }, -- to add the context_nvim source to a specific filetype
providers = {
context_nvim = {
enabled = true,
name = "context_nvim",
module = "context_nvim.blink_source",
},
},
}
}
- Open a file in neovim
- Perform one or many of the following actions:
- Add the current file to the manual context
- Add the current visual selection to the manual context
- Add the current buffer to the manual context
- Add the quickfix list to the manual context
- Add a directory to the manual context
- Add a file to the manual context
- Add line diagnostics to the manual context
- Open
gp.nvim
or another AI prompt plugin that supports chatting with an AI model - start to type
@manual_context
(or@history_context
) - Accept the cmp suggestion to insert the manual context into the chat
- start typing the
@prompt
and select a prompt you want to insert - submit the AI prompt
- Profit?