Robitx / gp.nvim

Gp.nvim (GPT prompt) Neovim AI plugin: ChatGPT sessions & Instructable text/code operations & Speech to text [OpenAI, Ollama, Anthropic, ..]
MIT License
897 stars 77 forks source link

Rewrite to diff: feature request #191

Open fneu opened 3 months ago

fneu commented 3 months ago

The way that claude.vim implements rewrite functionality is pretty nice: The edited file contents are written to a new split and a diff is automatically opened. It is basically a mix of :GpRewrite and :GpVnew.

It's possible to mirror this functionality with git and, e.g., fugitives :Gvdiff, but in that case I have to stage my changes beforehand. I could probably even automate this, by opening a split and copying the file contents there, before running :GpRewrite, and then diffing.

I'd still like to propose adding this directly to gp.nvim.

fneu commented 3 months ago

this works for me as a workaround for now:

    vim.keymap.set({ "x" }, "<space>cd", ":GpDiff ", { remap = true, desc = "[C]opilot rewrite to [D]iff" })

    function _G.gp_diff(args, line1, line2)
        local contents = vim.api.nvim_buf_get_lines(vim.api.nvim_get_current_buf(), 0, -1, false)

        vim.cmd("vnew")
        local scratch_buf = vim.api.nvim_get_current_buf()
        vim.bo[scratch_buf].buftype = "nofile"
        vim.bo[scratch_buf].bufhidden = "wipe"

        vim.api.nvim_buf_set_lines(scratch_buf, 0, -1, false, contents)

        vim.cmd(line1 .. "," .. line2 .. "GpRewrite " .. args)

        vim.defer_fn(function()
            vim.cmd("diffthis")
            vim.cmd("wincmd p")
            vim.cmd("diffthis")
        end, 1000)
    end

    vim.cmd("command! -range -nargs=+ GpDiff lua gp_diff(<q-args>, <line1>, <line2>)")