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

Accept output of commans.do_task()

Open undg opened this issue 2 years ago • 3 comments

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

undg avatar Oct 24 '23 23:10 undg

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

https://github.com/sourcegraph/sg.nvim/blob/6c592e9e78e68cd2bf4385da1b2a633219a22aab/after/plugin/cody.lua#L66-L71

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

undg avatar Oct 25 '23 08:10 undg

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

zippeurfou avatar Feb 08 '24 01:02 zippeurfou

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

undg avatar Feb 16 '24 20:02 undg