David-Kunz / gen.nvim

Neovim plugin to generate text using LLMs with customizable prompts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

replace = true not replacing

Amzd opened this issue · 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.

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)

Thanks for the suggestion @David-Kunz

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

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

And in normal visual mode?

In normal visual mode it does not work

commented

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

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>