sourcegraph / sg.nvim

Experimental Sourcegraph + Cody plugin for Neovim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accept output of commans.do_task()

undg opened this issue · comments

commented

I like to accept do_task() in same way as :CodyTask with <CR> or :CodyTaskAccept

Example:

local function generate_commit_message()
    local git_diff = vim.fn.system('git diff --cached')

    vim.api.nvim_command('G commit')

    commands.do_task(
        vim.api.nvim_get_current_buf(),
        0,
        1,
        'Suggest an informative commit message by summarizing code changes from the shared command output. The commit message should provide meaningful context for future readers.'
        .. git_diff
    )
end

It's do pick first line in commit buffer (I got there custom prefix from git hook), however it doesn't want to replace it with generated text.

Is there any way around it?

Edit

Seems like using standard nvim api via nvim_command solves that problem, and probably will be more robust solution.

Example:

local function shorten_type_error()
    vim.diagnostic.goto_prev()
    local error = vim.diagnostic.get_next().message
    vim.diagnostic.goto_next()

    cody_commands.ask({
        'Explain this diagnostic in shortest possible way. Remove all unnecesary noise, make it short and sweet.'
        .. error,
    })
end
commented

After reading this part I wonder how I can get access to M.tasks and M.active_task_index

vim.api.nvim_create_user_command("CodyTask", function(command)
local bufnr = vim.api.nvim_get_current_buf()
local task = cody_commands.do_task(bufnr, command.line1 - 1, command.line2, command.args)
table.insert(M.tasks, task)
M.active_task_index = #M.tasks
end, { range = 2, nargs = 1 })

Can I require anything from /after/plugin/cody.lua 🤔

@undg have you solved your issue?
I am facing the same issue.

commented

Yeah! It's a bit hack'y and perhaps overkill, but I've followed approach that I've seen in tests.

All you need to do is run nvim_command:

vim.api.nvim_command(
    'CodyTask '
    ..
    ' - Prompt for AI model'
)

Here you have few examples of how I've solved it:
https://github.com/undg/.dot/tree/master/vim/.config/nvim/lua/custom/sg/

And here I'm using those functions in keymaps and commands:
https://github.com/undg/.dot/blob/master/vim/.config/nvim/lua/plugins/sg.lua#L17-L36
https://github.com/undg/.dot/blob/master/vim/.config/nvim/lua/plugins/sg.lua#L58-L69