From 70ea7fb86ae458839ef20323d03d4fc54c071ce1 Mon Sep 17 00:00:00 2001 From: Oli Morris Date: Sat, 24 Feb 2024 12:46:27 +0000 Subject: [PATCH] feat: add `pre_hook` to inline actions --- RECIPES.md | 24 +++++++++++++++++++++++- lua/codecompanion/strategy.lua | 1 + lua/codecompanion/strategy/inline.lua | 11 +++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/RECIPES.md b/RECIPES.md index 50da5197..e0384cf9 100644 --- a/RECIPES.md +++ b/RECIPES.md @@ -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 diff --git a/lua/codecompanion/strategy.lua b/lua/codecompanion/strategy.lua index e91debab..6bf09c31 100644 --- a/lua/codecompanion/strategy.lua +++ b/lua/codecompanion/strategy.lua @@ -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() diff --git a/lua/codecompanion/strategy/inline.lua b/lua/codecompanion/strategy/inline.lua index 8938e48f..6529fb5f 100644 --- a/lua/codecompanion/strategy/inline.lua +++ b/lua/codecompanion/strategy/inline.lua @@ -143,6 +143,7 @@ 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 @@ -150,6 +151,16 @@ local 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,