David-Kunz / gen.nvim

Neovim plugin to generate text using LLMs with customizable prompts
The Unlicense
1.15k stars 87 forks source link

allow prompt function to cancel generate operation #69

Closed bartman closed 7 months ago

bartman commented 7 months ago

This PR adds the ability to set a prompt function that returns an empty string or a nil value, with the intent to have gen.nvim skip this action. It is expected that the prompt function will inform the user that the action was terminated.

For example the following custom prompt will only work if called with a selection, where gen.nvim will pass it the visual selection. If the selection is empty, the prompt will print "content is empty!" and return nil.

        local gen = require'gen'
        gen.prompts['Testcases_for_code'] = {
            prompt = function(opt)
                if opt.content == "" then
                    print("content is empty!")
                    return nil
                end
                if opt.filetype == 'cpp' or opt.filetype == 'c' then
                    return "Using C++20 and googletest generate testcases for the following code:"
                        .. opt.content .. "\n"
                else
                    return "Using $filetype generate testcases for the following code:"
                        .. opt.content .. "\n"
                end
            end
        }
David-Kunz commented 7 months ago

Hi @bartman ,

Thank you for this contribution, I think this is a reasonable suggestion! ❤️

Best regards, David