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 · 1 comment
Open

Add before & after cursor as context #120

felixkreuk opened this issue Sep 10, 2024 · 1 comment

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

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

2 participants