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

Add before & after cursor as context #120

Open
felixkreuk opened this issue Sep 10, 2024 · 3 comments
Open

Add before & after cursor as context #120

felixkreuk opened this issue Sep 10, 2024 · 3 comments

Comments

@felixkreuk
Copy link

felixkreuk commented Sep 10, 2024

First, thanks a lot for this nifty plugin! :)

The use case would be to generate a code snippet at a specific line.
In this scenario, it may be redundant to give the whole file as context, but a few lines before and after the current cursor line might help.

Edit: other helpful contexts can be passing the current function/class/scope and cursor location, etc.

Apologies if this is already implemented and I missed it. Thanks again!

@David-Kunz
Copy link
Owner

Hi @felixkreuk ,

Thank you for this feature suggestion. This is already possible as of today, for example to insert the lines before and after the cursor, you can do the following:

require('gen').prompts['Code_Completion'] = { 
  prompt = function()
    local buf = vim.api.nvim_get_current_buf()
    local row, col = unpack(vim.api.nvim_win_get_cursor(0))

    local before = vim.api.nvim_buf_get_text(0, 0, 0, row-1, col+1, {})
    local after = vim.api.nvim_buf_get_text(0, row-1, col+1, -1, -1, {})
    local prompt = '<|fim_prefix|>' .. table.concat(before, "\n") .. '<|fim_suffix|>' .. table.concat(after, "\n") .. '<|fim_middle|>only output the middle part, not the prefix/suffix, nothing else, just the missing code including the $filetype code fence ```$filetype\n<resulting code>\n``` for example ```$filetype\nconsole.log("hello")\n```'
    return prompt
  end,
  model = "qwen2.5-coder:7b-instruct",
  extract = "```$filetype\n(.-)```"
}
vim.keymap.set('i', '<c-]>', '<esc>:Gen Code_Completion<CR>')

Would that work for you?

Thanks and best regards,
David

@aep
Copy link

aep commented Nov 26, 2024

having prompt be a function is pretty smart!
your example doesnt work, i think buf is never used?

but i'm not sure how to write my own function, because i cant figure out how to debug lua code in nvim D:
for example, how would i even check that before contains what i want

@David-Kunz
Copy link
Owner

Thanks, @aep . The following works for me:

require('gen').prompts['Code_Completion'] = { 
  prompt = function()
    local row, col = unpack(vim.api.nvim_win_get_cursor(0))
    local before = vim.api.nvim_buf_get_text(0, 0, 0, row-1, col+1, {})
    local after = vim.api.nvim_buf_get_text(0, row-1, col+1, -1, -1, {})
    local prompt = '<|fim_prefix|>' .. table.concat(before, "\n") .. '<|fim_suffix|>' .. table.concat(after, "\n") .. '<|fim_middle|>only output the middle part, not the prefix/suffix, nothing else, just the missing code in between, do not repeate the provided text, include the $filetype code fence ```$filetype\n<resulting code>\n``` for example ```$filetype\nconsole.log("hello")\n```'
    return prompt
  end,
  model = "qwen2.5-coder:32b",
  extract = "```$filetype\n(.-)```"
}
vim.keymap.set('i', '<c-]>', '<esc>:Gen Code_Completion<CR>')

Note: You have to accept it with ctrl-enter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants