David-Kunz / gen.nvim

Neovim plugin to generate text using LLMs with customizable prompts
The Unlicense
992 stars 64 forks source link

replace = true not replacing #50

Open Amzd opened 7 months ago

Amzd commented 7 months ago

I created a custom prompt with replace = true but it just adds the result at the spot of the cursor without removing the selected part.

require('gen').prompts['Fix_Err'] = {
    replace = true,
    extract = "```$filetype\n(.-)```",
}

vim.keymap.set({'v', "n"}, '<leader>sf', function()
    local lsp = vim.lsp
    local cursor = vim.fn.getcurpos()
    local line_number = cursor[2] - 1
    local diagnostics = vim.diagnostic.get(0, { lnum = line_number })

    local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
    local filecontents = table.concat(lines, "\n")
    local line = tostring(line_number)
    local error = diagnostics[1].message

    require('gen').prompts['Fix_Err'].prompt = "This is my code: \n\n```$filetype\n" .. filecontents .. "```\n\n"
        .. "Tell me the replacement for line " .. line 
        .. " that fixes the error \"" .. error 
        .. "\" in format: ```$filetype\n...\n``` without any other text."
    vim.api.nvim_command('Gen Fix_Err')
end)

It usually gets the code that would fix the error but it does not remove the selected part.

Am I doing something obviously wrong? I am new to vim.

David-Kunz commented 7 months ago

Hi @Amzd ,

It seems the visual selection is lost. Can you try with the following?

require('gen').prompts['Fix_Err'] = {
    replace = true,
    extract = "```$filetype\n(.-)```",
}

vim.keymap.set({'v', "n"}, '<leader>sf', function()
    local lsp = vim.lsp
    local cursor = vim.fn.getcurpos()
    local line_number = cursor[2] - 1
    local diagnostics = vim.diagnostic.get(0, { lnum = line_number })

    local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
    local filecontents = table.concat(lines, "\n")
    local line = tostring(line_number)
    local error = diagnostics[1].message

    require('gen').prompts['Fix_Err'].prompt = "This is my code: \n\n```$filetype\n" .. filecontents .. "```\n\n"
        .. "Tell me the replacement for line " .. line 
        .. " that fixes the error \"" .. error 
        .. "\" in format: ```$filetype\n...\n``` without any other text."
-    vim.api.nvim_command('Gen Fix_Err')
+    vim.cmd("'<,'>Gen Fix_Err")
end)
Amzd commented 7 months ago

Thanks for the suggestion @David-Kunz

When I do that I get an error in the cmd function Mark not set

Amzd commented 7 months ago

it works when the mode says -- (insert) VISUAL -- but I think I can only get into that with the mouse

David-Kunz commented 4 months ago

And in normal visual mode?

Amzd commented 3 months ago

In normal visual mode it does not work

life00 commented 3 months ago

I also had issues with visual selection related commands. I use lazy and here is an example of selection keymap:

{
  "David-Kunz/gen.nvim",
  opts = {
    model = "mistral:7b", -- The default model to use.
  },
  keys = {
    {
      "<leader>cg",
      "<cmd>Gen<cr>",
      mode = "n",
      noremap = true,
      silent = true,
      desc = "LLM tools",
    },
    {
      "<leader>cg",
      ":'<,'>Gen<cr>",
      mode = "v",
      noremap = true,
      silent = true,
      desc = "LLM tools",
    },
  },
},
sukantamaikap commented 2 months ago

I'm facing something similar. In visual mode, replacement does not happen. I get the the below error:

^I/Users/sm/.local/share/nvim/lazy/gen.nvim/lua/gen/init.lua:325: in function </Users/sm/.local/share/nvim/lazy/gen.nvim/lua/gen/init.lua:317>
repomaa commented 2 weeks ago

dito. Here's the full error from :messages

^I...ir/pack/myNeovimPackages/start/gen.nvim/lua/gen/init.lua:331: in function <...ir/pack/myNeovimPackages/start/gen.nvim/lua/gen/init.lua:323> f
unction: builtin#18 ...ir/pack/myNeovimPackages/start/gen.nvim/lua/gen/init.lua:331: Invalid 'buffer': Expected Lua number                        
stack traceback:                                                                                                                                  
^I[C]: in function 'nvim_buf_delete'                                                                                                              
^I...ir/pack/myNeovimPackages/start/gen.nvim/lua/gen/init.lua:331: in function <...ir/pack/myNeovimPackages/start/gen.nvim/lua/gen/init.lua:323>  
"app/javascript/src/containers/settings/purchases/budgets/index.tsx" 190L, 6518B written