Skip to content

napisani/context-nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

context-nvim

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:

  1. Manual Contexts: These are contexts that you manually define and manage. You can add, remove, and edit these contexts with context-nvim subcommands.
  2. History Contexts: These are contexts that are automatically generated by context-nvim based on buffers you have opened recently.

Features

  • 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

Demo

demo

Installation

Using lazy.nvim:

{
    "napisani/context-nvim",
    config = function()
        require("context_nvim").setup({ })
    end,
},

Using it

Configuration

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'
        },
    }
})

Subcommands

add_current_file

Adds the current file to the manual context.

:ContextNvim add_current_file

add_current

Adds the current visual selection or buffer to the manual context.

:ContextNvim add_current

add_qflist

Adds all items from the quickfix list to the manual context.

:ContextNvim add_qflist

clear_history

Clears the history context.

:ContextNvim clear_history

clear_manual

Clears the manual context.

:ContextNvim clear_manual

add_dir

Opens a Telescope picker to select and add a directory to the manual context.

:ContextNvim add_dir

add_file

Opens a Telescope picker to select and add a file to the manual context.

:ContextNvim add_file

find_context_history

Opens a Telescope picker to view and manage the history context.

:ContextNvim find_context_history

find_context_manual

Opens a Telescope picker to view and manage the manual context.

:ContextNvim find_context_manual

add_line_lsp_daig

Adds context for LSP line diagnostics to the manual context.

:ContextNvim add_line_lsp_daig

paste_manual

Pastes all manual contexts at the current cursor position.

:ContextNvim paste_manual

paste_history

Pastes all history contexts at the current cursor position.

:ContextNvim paste_history

insert_prompt

Brings up a telescope picker, when an entry is selected. The prompt will be inserted into the buffer.

:ContextNvim insert_prompt 

Blink Configuration

To use context-nvim with blink.nvim, you need to:

  1. Enable it in the context-nvim config:
require("context_nvim").setup({
  ...
  blink = {
    manual_context_keyword = "@manual_context",
    history_keyword = "@history_context",
    prompt_keyword = "@prompt",
  }
  ...
})
  1. 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",
      },
    },
  }
}

Example workflow (with cmp)

  1. Open a file in neovim
  2. 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
  3. Open gp.nvim or another AI prompt plugin that supports chatting with an AI model
  4. start to type @manual_context (or @history_context)
  5. Accept the cmp suggestion to insert the manual context into the chat
  6. start typing the @prompt and select a prompt you want to insert
  7. submit the AI prompt
  8. Profit?

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published