Skip to content

Instantly share code, notes, and snippets.

@eduardoarandah
Last active February 19, 2025 17:35
Show Gist options
  • Save eduardoarandah/20d8527e966ead9b8fd274016c720a8b to your computer and use it in GitHub Desktop.
Save eduardoarandah/20d8527e966ead9b8fd274016c720a8b to your computer and use it in GitHub Desktop.
create context for the llm in neovim
-----------------------------
-- Create context for the LLM
-- Long context prompting tips
-- https://docs.anthropic.com/en/docs/build-with-claude/prompt-engineering/long-context-tips#example-quote-extraction
-----------------------------
-- Dump context with documents tags
vim.api.nvim_create_user_command("ContextDump", function()
local content = vim.fn.getreg("z")
vim.fn.setreg("+", "<documents>\n" .. content .. "</documents>")
vim.fn.setreg("z", "")
vim.notify("Context dumped to clipboard", vim.log.levels.INFO)
end, { bang = true })
-- Add buffer to context
local function add_buffer_to_context(bufnr)
-- Get buffer name
local bufname = vim.api.nvim_buf_get_name(bufnr)
if bufname == "" then
return false
end
local header = string.format("<document>\n<source>%s</source>\n<document_content>\n", bufname)
local footer = "\n</document_content>\n</document>\n"
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local content = table.concat(lines, "\n")
vim.fn.setreg("z", vim.fn.getreg("z") .. header .. content .. footer)
return true
end
-- Add current file to context
vim.api.nvim_create_user_command("ContextAddBuffer", function()
if add_buffer_to_context(0) then
vim.notify("Added file to context", vim.log.levels.INFO)
end
end, { bang = true })
-- Add all open buffers to context
vim.api.nvim_create_user_command("ContextAddOpenBuffers", function()
local buffers = vim.api.nvim_list_bufs()
local added_count = 0
for _, bufnr in ipairs(buffers) do
if vim.api.nvim_buf_is_loaded(bufnr) then
if add_buffer_to_context(bufnr) then
added_count = added_count + 1
end
end
end
vim.notify(string.format("Added %d buffers to context", added_count), vim.log.levels.INFO)
end, { bang = true })
-- Add current selection to context
vim.api.nvim_create_user_command("ContextAddSelection", function(opts)
local start_line = vim.fn.line("'<") - 1 -- nvim_buf_get_lines es 0-based
local end_line = vim.fn.line("'>")
local header = string.format(
"<document>\n<source>%s</source>\n<line_start>%d</line_start>\n<line_end>%d</line_end>\n<document_content>\n",
vim.fn.expand("%"),
start_line + 1, -- volvemos a 1-based para el XML
end_line
)
local footer = "\n</document_content>\n</document>\n"
local lines = vim.api.nvim_buf_get_lines(0, start_line, end_line, false)
local content = table.concat(lines, "\n")
vim.fn.setreg("z", vim.fn.getreg("z") .. header .. content .. footer)
vim.notify("Added selection to context", vim.log.levels.INFO)
end, { range = true, bang = true })
-- Copy function to context
vim.api.nvim_create_user_command("ContextAddFunction", function()
local current_pos = vim.fn.getpos(".")
vim.cmd('normal "Zyas') -- usar treesitter para obtener el scope
local start_line = current_pos[2]
local end_line = start_line + #vim.fn.split(vim.fn.getreg("Z"), "\n") - 1
local header = string.format("<document>\n<source>%s</source>\n<line_start>%d</line_start>\n<line_end>%d</line_end>\n<document_content>\n", vim.fn.expand("%"), start_line, end_line)
local footer = "\n</document_content>\n</document>\n"
local content = vim.fn.getreg("Z")
vim.fn.setreg("z", vim.fn.getreg("z") .. header .. content .. footer)
vim.notify("Added function to context", vim.log.levels.INFO)
end, { bang = true })
-- Add directory structure to context
vim.api.nvim_create_user_command("ContextAddDirectoryStructure", function()
if vim.fn.executable("tree") ~= 1 then
vim.notify("'tree' command not found. Please install it first.", vim.log.levels.ERROR)
return
end
local header = string.format("<document>\n<source>directory_structure</source>\n<document_content>\n")
local footer = "\n</document_content>\n</document>\n"
-- -I para ignorar .git explícitamente, además de usar --gitignore
local cmd = 'tree -L 3 -a --gitignore --noreport --dirsfirst -I ".git"'
local lines = vim.fn.systemlist(cmd)
local content = table.concat(lines, "\n")
vim.fn.setreg("z", vim.fn.getreg("z") .. header .. content .. footer)
vim.notify("Added directory structure to context", vim.log.levels.INFO)
end, { bang = true })
-----------
-- Mappings
-----------
-- ContextAddOpenBuffers
-- ContextAddFunction
-- ContextAddDirectoryStructure
vim.keymap.set("v", "<F9>", ":<C-u>ContextAddSelection<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<F9>", ":<C-u>ContextAddBuffer<CR>", { noremap = true, silent = true })
vim.keymap.set("n", "<F10>", ":ContextDump<CR>", { noremap = true, silent = true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment