gen.nvim icon indicating copy to clipboard operation
gen.nvim copied to clipboard

replace = true not replacing

Open Amzd opened this issue 2 years ago • 9 comments

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.

Amzd avatar Dec 09 '23 14:12 Amzd

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)

David-Kunz avatar Dec 10 '23 10:12 David-Kunz

Thanks for the suggestion @David-Kunz

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

Amzd avatar Dec 10 '23 11:12 Amzd

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

Amzd avatar Dec 10 '23 12:12 Amzd

And in normal visual mode?

David-Kunz avatar Mar 22 '24 11:03 David-Kunz

In normal visual mode it does not work

Amzd avatar Mar 29 '24 22:03 Amzd

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",
    },
  },
},

life00 avatar Apr 02 '24 09:04 life00

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>

sukantamaikap avatar May 08 '24 11:05 sukantamaikap

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

repomaa avatar Jul 01 '24 10:07 repomaa

Hey folks! Try this. If you select a "line" using "V", replace works like a charm. But if you select a "text-line", f.e. using "^v$", replace throws error.

odyshev avatar Jul 31 '24 20:07 odyshev