Skip to content

Commit

Permalink
feat: add pre_hook to inline actions
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Feb 24, 2024
1 parent 9ecb304 commit 70ea7fb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
24 changes: 23 additions & 1 deletion RECIPES.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,29 @@ require("codecompanion").setup({

If you run `:CodeCompanionActions`, you should see that the two boilerplate prompts are now nested in their own `vim.ui.select` component.

Whilst these examples were useful at demonstrating the functionality of the _Action Palette_, they're not leveraging GenAI to add any real value to your workflow (this boilerplate could be snippets after all)...So let's step things up in the next section.
### Leveraging pre-hooks

To make this example complete, we can leverage a pre-hook to create a new buffer and set the filetype to be html:

```lua
{
name = "HTML boilerplate",
strategy = "inline",
description = "Create some HTML boilerplate",
pre_hook = function()
local bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_option(bufnr, "filetype", "html")
vim.api.nvim_set_current_buf(bufnr)
return bufnr
end
}
```

For the inline strategy, the plugin will detect a number being returned and assume that is the buffer number you wish any code to be streamed into.

### Conclusion

Whilst these examples were useful at demonstrating the functionality of the _Action Palette_, they're not making the most of the GenAI models to add any real value to your workflow (this boilerplate could be snippets after all). So let's step things up in the next section.

## Recipe #2: Using context in your prompts

Expand Down
1 change: 1 addition & 0 deletions lua/codecompanion/strategy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function Strategy:inline()
context = self.context,
client = self.client,
opts = self.selected.opts,
pre_hook = self.selected.pre_hook,
prompts = self.selected.prompts,
})
:start()
Expand Down
11 changes: 11 additions & 0 deletions lua/codecompanion/strategy/inline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,24 @@ local Inline = {}
---@field context table
---@field client CodeCompanion.Client
---@field opts table
---@field pre_hook fun():number -- Assuming pre_hook returns a number for example
---@field prompts table

---@param opts CodeCompanion.InlineArgs
---@return CodeCompanion.Inline
function Inline.new(opts)
log:trace("Initiating Inline")

if type(opts.pre_hook) == "function" then
local bufnr = opts.pre_hook()

if type(bufnr) == "number" then
opts.context.bufnr = bufnr
opts.context.start_line = 1
opts.context.start_col = 1
end
end

return setmetatable({
settings = config.options.ai_settings.inline,
context = opts.context,
Expand Down

0 comments on commit 70ea7fb

Please sign in to comment.