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

replace = true not replacing #50

Open
Amzd opened this issue Dec 9, 2023 · 9 comments
Open

replace = true not replacing #50

Amzd opened this issue Dec 9, 2023 · 9 comments

Comments

@Amzd
Copy link
Contributor

Amzd commented Dec 9, 2023

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
Copy link
Owner

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
Copy link
Contributor Author

Amzd commented Dec 10, 2023

Thanks for the suggestion @David-Kunz

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

@Amzd
Copy link
Contributor Author

Amzd commented Dec 10, 2023

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

@David-Kunz
Copy link
Owner

And in normal visual mode?

@Amzd
Copy link
Contributor Author

Amzd commented Mar 29, 2024

In normal visual mode it does not work

@life00
Copy link

life00 commented Apr 2, 2024

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
Copy link

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
Copy link

repomaa commented Jul 1, 2024

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

@odyshev
Copy link

odyshev commented Jul 31, 2024

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.

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

6 participants