Skip to content

Instantly share code, notes, and snippets.

@datsfilipe
Forked from VictorTaelin/holefill.vimrc
Last active February 3, 2024 14:57
Show Gist options
  • Save datsfilipe/d1b511fcfb7e22e244424efd65025358 to your computer and use it in GitHub Desktop.
Save datsfilipe/d1b511fcfb7e22e244424efd65025358 to your computer and use it in GitHub Desktop.
" Calls GPT-4 to fill holes in the current file,
" omitting collapsed folds to save prompt space
local M = {}
local function save_visible_lines(dest)
local visible_lines = {}
local lnum = 1
while lnum <= vim.fn.line('$') do
if vim.fn.foldclosed(lnum) == -1 then
table.insert(visible_lines, vim.fn.getline(lnum))
lnum = lnum + 1
else
table.insert(visible_lines, vim.fn.getline(vim.fn.foldclosed(lnum)))
table.insert(visible_lines, "...")
table.insert(visible_lines, vim.fn.getline(vim.fn.foldclosedend(lnum)))
lnum = vim.fn.foldclosedend(lnum) + 1
end
end
vim.fn.writefile(visible_lines, dest)
end
function M.execute()
local tmp_file = ''
local holefill_exists = vim.fn.executable('holefill') > 0
if not holefill_exists then
print('Error: holefill executable not found. Please install it.')
return
end
if (vim.fn.bufname('%') == '') then
tmp_file = vim.fn.tempname()
vim.cmd('w ' .. tmp_file)
else
vim.cmd('w')
tmp_file = vim.fn.expand('%:p')
end
save_visible_lines('.fill.tmp')
vim.fn.system('NODE_NO_WARNINGS=1 holefill ' .. tmp_file .. ' .fill.tmp')
vim.fn.system('del .fill.tmp')
vim.cmd('edit!')
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment