From 54b310779fd5c2597a21a0c146080a8adfa608b7 Mon Sep 17 00:00:00 2001 From: Oli Morris Date: Wed, 9 Oct 2024 20:04:23 +0100 Subject: [PATCH] refactor: clean up config and lua types --- .luarc.json | 13 ++-- README.md | 9 ++- doc/codecompanion.txt | 11 ++-- lua/cmp_codecompanion/slash_commands.lua | 2 +- lua/codecompanion/actions/init.lua | 2 +- .../actions/providers/default.lua | 2 +- .../actions/providers/mini_pick.lua | 1 - .../actions/providers/telescope.lua | 2 +- lua/codecompanion/adapters.lua | 5 +- lua/codecompanion/adapters/anthropic.lua | 2 +- lua/codecompanion/adapters/copilot.lua | 5 +- lua/codecompanion/adapters/gemini.lua | 2 +- lua/codecompanion/adapters/ollama.lua | 4 +- lua/codecompanion/adapters/openai.lua | 2 +- lua/codecompanion/commands.lua | 2 +- lua/codecompanion/config.lua | 21 ++++++- lua/codecompanion/helpers/diff/default.lua | 2 +- lua/codecompanion/helpers/diff/mini_diff.lua | 2 - .../helpers/slash_commands/buffer.lua | 61 ++++++++++++------- .../helpers/slash_commands/fetch.lua | 21 +++---- .../helpers/slash_commands/file.lua | 29 +++++---- .../helpers/slash_commands/help.lua | 27 ++++---- .../helpers/slash_commands/now.lua | 19 +++--- .../helpers/slash_commands/symbols.lua | 21 +++---- .../helpers/slash_commands/terminal.lua | 19 +++--- lua/codecompanion/helpers/tools/editor.lua | 2 +- lua/codecompanion/http.lua | 2 +- lua/codecompanion/init.lua | 48 ++++++++------- lua/codecompanion/keymaps.lua | 4 +- lua/codecompanion/strategies.lua | 2 +- lua/codecompanion/strategies/chat.lua | 2 +- lua/codecompanion/strategies/chat/tools.lua | 2 +- .../strategies/chat/variables.lua | 2 +- lua/codecompanion/strategies/inline.lua | 2 +- lua/codecompanion/types.lua | 6 ++ lua/codecompanion/workflow.lua | 2 +- .../strategies/chat/tools_spec.lua | 2 +- .../strategies/chat/variables_spec.lua | 2 +- plugin/codecompanion.lua | 14 ----- 39 files changed, 206 insertions(+), 172 deletions(-) create mode 100644 lua/codecompanion/types.lua delete mode 100644 plugin/codecompanion.lua diff --git a/.luarc.json b/.luarc.json index 1e1765c6..ec302153 100644 --- a/.luarc.json +++ b/.luarc.json @@ -1,5 +1,10 @@ { - "diagnostics.globals": [ - "vim" - ] -} \ No newline at end of file + "runtime": { + "version": "LuaJIT", + "pathStrict": true + }, + "diagnostics.globals": ["vim"], + "type": { + "checkTableShape": true + } +} diff --git a/README.md b/README.md index b229907e..93a94875 100644 --- a/README.md +++ b/README.md @@ -203,17 +203,20 @@ Run `:CodeCompanionActions` to open the action palette, which gives you access t **List of commands** -Below is a list of the plugin's commands: +The plugin has three core commands: - `CodeCompanion` - Open the inline assistant +- `CodeCompanionChat` - Open a chat buffer +- `CodeCompanionActions` - Open the _Action Palette_ + +However there are multiple options available: + - `CodeCompanion ` - Prompt the inline assistant - `CodeCompanion /` - Use the [prompt library](#clipboard-prompt-library) with the inline assistant e.g. `/commit` -- `CodeCompanionChat` - Open a chat buffer - `CodeCompanionChat ` - Send a prompt to the LLM via a chat buffer - `CodeCompanionChat ` - Open a chat buffer with a specific adapter - `CodeCompanionChat Toggle` - Toggle a chat buffer - `CodeCompanionChat Add` - Add visually selected chat to the current chat buffer -- `CodeCompanionActions` - Open the _Action Palette_ **Suggested workflow** diff --git a/doc/codecompanion.txt b/doc/codecompanion.txt index ca4bc9cc..9cc1fb36 100644 --- a/doc/codecompanion.txt +++ b/doc/codecompanion.txt @@ -1,4 +1,4 @@ -*codecompanion.txt* For NVIM v0.10.0 Last change: 2024 October 08 +*codecompanion.txt* For NVIM v0.10.0 Last change: 2024 October 09 ============================================================================== Table of Contents *codecompanion-table-of-contents* @@ -177,17 +177,20 @@ codecompanion`. mode_. **List of commands** -Below is a list of the plugin’s commands: +The plugin has three core commands: - `CodeCompanion` - Open the inline assistant +- `CodeCompanionChat` - Open a chat buffer +- `CodeCompanionActions` - Open the _Action Palette_ + +However there are multiple options available: + - `CodeCompanion ` - Prompt the inline assistant - `CodeCompanion /` - Use the |codecompanion-prompt-library| with the inline assistant e.g. `/commit` -- `CodeCompanionChat` - Open a chat buffer - `CodeCompanionChat ` - Send a prompt to the LLM via a chat buffer - `CodeCompanionChat ` - Open a chat buffer with a specific adapter - `CodeCompanionChat Toggle` - Toggle a chat buffer - `CodeCompanionChat Add` - Add visually selected chat to the current chat buffer -- `CodeCompanionActions` - Open the _Action Palette_ **Suggested workflow** diff --git a/lua/cmp_codecompanion/slash_commands.lua b/lua/cmp_codecompanion/slash_commands.lua index b0a65f86..2657aaba 100644 --- a/lua/cmp_codecompanion/slash_commands.lua +++ b/lua/cmp_codecompanion/slash_commands.lua @@ -1,5 +1,5 @@ -local config = require("codecompanion").config local SlashCommands = require("codecompanion.strategies.chat.slash_commands") +local config = require("codecompanion.config") local strategy = require("codecompanion.strategies") local source = {} diff --git a/lua/codecompanion/actions/init.lua b/lua/codecompanion/actions/init.lua index eba7ac51..52e64a6a 100644 --- a/lua/codecompanion/actions/init.lua +++ b/lua/codecompanion/actions/init.lua @@ -1,5 +1,5 @@ -local config = require("codecompanion").config local Strategy = require("codecompanion.strategies") +local config = require("codecompanion.config") local prompt_library = require("codecompanion.actions.prompt_library") local static_actions = require("codecompanion.actions.static") diff --git a/lua/codecompanion/actions/providers/default.lua b/lua/codecompanion/actions/providers/default.lua index 7504d601..50ccb505 100644 --- a/lua/codecompanion/actions/providers/default.lua +++ b/lua/codecompanion/actions/providers/default.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local ui = require("codecompanion.utils.ui") diff --git a/lua/codecompanion/actions/providers/mini_pick.lua b/lua/codecompanion/actions/providers/mini_pick.lua index 47cba384..2f0ff819 100644 --- a/lua/codecompanion/actions/providers/mini_pick.lua +++ b/lua/codecompanion/actions/providers/mini_pick.lua @@ -1,5 +1,4 @@ local MiniPick = require("mini.pick") -local config = require("codecompanion").config local log = require("codecompanion.utils.log") ---@class CodeCompanion.Actions.Providers.MiniPick diff --git a/lua/codecompanion/actions/providers/telescope.lua b/lua/codecompanion/actions/providers/telescope.lua index 4a8f7a1a..00d111fb 100644 --- a/lua/codecompanion/actions/providers/telescope.lua +++ b/lua/codecompanion/actions/providers/telescope.lua @@ -5,7 +5,7 @@ local action_state = require("telescope.actions.state") local telescope_actions = require("telescope.actions") local is_registered = false -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/adapters.lua b/lua/codecompanion/adapters.lua index 9eb84a0a..b01ac896 100644 --- a/lua/codecompanion/adapters.lua +++ b/lua/codecompanion/adapters.lua @@ -1,6 +1,4 @@ -local config = require("codecompanion").config - -local dep = require("codecompanion.utils.deprecate") +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") ---Check if a variable starts with "cmd:" @@ -270,7 +268,6 @@ end ---@param adapter? CodeCompanion.Adapter|string|function ---@return CodeCompanion.Adapter function Adapter.resolve(adapter) - config = require("codecompanion").config adapter = adapter or config.adapters[config.strategies.chat.adapter] if type(adapter) == "table" then diff --git a/lua/codecompanion/adapters/anthropic.lua b/lua/codecompanion/adapters/anthropic.lua index 2c3b3c91..7b725e3f 100644 --- a/lua/codecompanion/adapters/anthropic.lua +++ b/lua/codecompanion/adapters/anthropic.lua @@ -5,7 +5,7 @@ local utils = require("codecompanion.utils.messages") local input_tokens = 0 local output_tokens = 0 ----@class CodeCompanion.AdapterArgs +---@class Anthropic.Adapter: CodeCompanion.Adapter return { name = "anthropic", roles = { diff --git a/lua/codecompanion/adapters/copilot.lua b/lua/codecompanion/adapters/copilot.lua index 2667ccce..5efb8523 100644 --- a/lua/codecompanion/adapters/copilot.lua +++ b/lua/codecompanion/adapters/copilot.lua @@ -1,5 +1,6 @@ local curl = require("plenary.curl") +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local openai = require("codecompanion.adapters.openai") local util = require("codecompanion.utils.util") @@ -87,6 +88,8 @@ local function authorize_token() Authorization = "Bearer " .. _oauth_token, ["Accept"] = "application/json", }, + insecure = config.adapters.opts.allow_insecure, + proxy = config.adapters.opts.proxy, on_error = function(err) log:error("Copilot Adapter: Token request error %s", err) end, @@ -96,7 +99,7 @@ local function authorize_token() return _github_token end ----@class CodeCompanion.AdapterArgs +---@class Copilot.Adapter: CodeCompanion.Adapter return { name = "copilot", roles = { diff --git a/lua/codecompanion/adapters/gemini.lua b/lua/codecompanion/adapters/gemini.lua index 77ac55e5..32ae5410 100644 --- a/lua/codecompanion/adapters/gemini.lua +++ b/lua/codecompanion/adapters/gemini.lua @@ -4,7 +4,7 @@ local log = require("codecompanion.utils.log") local utils = require("codecompanion.utils.messages") ----@class CodeCompanion.AdapterArgs +---@class Gemini.Adapter: CodeCompanion.Adapter return { name = "gemini", roles = { diff --git a/lua/codecompanion/adapters/ollama.lua b/lua/codecompanion/adapters/ollama.lua index 841c3c01..b3a8d8c6 100644 --- a/lua/codecompanion/adapters/ollama.lua +++ b/lua/codecompanion/adapters/ollama.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local utils = require("codecompanion.utils.messages") @@ -56,7 +56,7 @@ local function get_models(self, opts) return models end ----@class CodeCompanion.AdapterArgs +---@class Gemini.Adapter: CodeCompanion.Adapter return { name = "ollama", roles = { diff --git a/lua/codecompanion/adapters/openai.lua b/lua/codecompanion/adapters/openai.lua index 5836ce3b..1a8d62ee 100644 --- a/lua/codecompanion/adapters/openai.lua +++ b/lua/codecompanion/adapters/openai.lua @@ -1,6 +1,6 @@ local log = require("codecompanion.utils.log") ----@class CodeCompanion.AdapterArgs +---@class OpenAI.Adapter: CodeCompanion.Adapter return { name = "openai", roles = { diff --git a/lua/codecompanion/commands.lua b/lua/codecompanion/commands.lua index d9a71d6a..889b6512 100644 --- a/lua/codecompanion/commands.lua +++ b/lua/codecompanion/commands.lua @@ -2,7 +2,7 @@ local context = require("codecompanion.utils.context") local log = require("codecompanion.utils.log") local codecompanion = require("codecompanion") -local config = codecompanion.config +local config = require("codecompanion.config") local prompt_library = vim .iter(config.prompt_library) diff --git a/lua/codecompanion/config.lua b/lua/codecompanion/config.lua index 16d3323b..38658dd1 100644 --- a/lua/codecompanion/config.lua +++ b/lua/codecompanion/config.lua @@ -1,6 +1,6 @@ local fmt = string.format -return { +local defaults = { adapters = { -- LLMs ------------------------------------------------------------------- anthropic = "anthropic", @@ -768,3 +768,22 @@ When given a task: 4. You can only give one reply for each conversation turn.]], }, } + +local M = { + config = vim.deepcopy(defaults), +} + +---@param args? table +M.setup = function(args) + args = args or {} + M.config = vim.tbl_deep_extend("force", vim.deepcopy(defaults), args) +end + +return setmetatable(M, { + __index = function(_, key) + if key == "setup" then + return M.setup + end + return rawget(M.config, key) + end, +}) diff --git a/lua/codecompanion/helpers/diff/default.lua b/lua/codecompanion/helpers/diff/default.lua index 8a5e7bcb..e237975d 100644 --- a/lua/codecompanion/helpers/diff/default.lua +++ b/lua/codecompanion/helpers/diff/default.lua @@ -1,7 +1,7 @@ -- Taken from the awesome: -- https://github.com/S1M0N38/dante.nvim -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local util = require("codecompanion.utils.util") diff --git a/lua/codecompanion/helpers/diff/mini_diff.lua b/lua/codecompanion/helpers/diff/mini_diff.lua index 55747020..2d0dc006 100644 --- a/lua/codecompanion/helpers/diff/mini_diff.lua +++ b/lua/codecompanion/helpers/diff/mini_diff.lua @@ -1,8 +1,6 @@ ---Utilising the awesome: ---https://github.com/echasnovski/mini.diff -local config = require("codecompanion").config - local log = require("codecompanion.utils.log") local util = require("codecompanion.utils.util") diff --git a/lua/codecompanion/helpers/slash_commands/buffer.lua b/lua/codecompanion/helpers/slash_commands/buffer.lua index c770d9b0..de5101d2 100644 --- a/lua/codecompanion/helpers/slash_commands/buffer.lua +++ b/lua/codecompanion/helpers/slash_commands/buffer.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local buf = require("codecompanion.utils.buffers") local file_utils = require("codecompanion.utils.files") @@ -13,7 +13,7 @@ CONSTANTS = { } ---Output from the slash command in the chat buffer ----@param SlashCommand CodeCompanion.SlashCommandBuffer +---@param SlashCommand CodeCompanion.SlashCommand ---@param selected table The selected item from the provider { name = string, bufnr = number, path = string } ---@return nil local function output(SlashCommand, selected) @@ -34,22 +34,35 @@ local function output(SlashCommand, selected) end local Chat = SlashCommand.Chat - Chat:append_to_buf({ - content = "[!" - .. CONSTANTS.NAME - .. ": `" - .. selected[CONSTANTS.DISPLAY] - .. "` / Buf No.: " - .. selected.bufnr - .. "]\n", - }) - Chat:append_to_buf({ content = content }) - Chat:fold_code() + if config.opts.visible then + Chat:append_to_buf({ + content = "[!" + .. CONSTANTS.NAME + .. ": `" + .. selected[CONSTANTS.DISPLAY] + .. "` / Buf No.: " + .. selected.bufnr + .. "]\n", + }) + Chat:append_to_buf({ content = content }) + Chat:fold_code() + else + Chat:add_message({ + role = "user", + content = string.format( + [[Here is the data from buffer number %d: + +%s]], + selected.bufnr, + content + ), + }, { visible = false }) + end end local Providers = { ---The default provider - ---@param SlashCommand CodeCompanion.SlashCommandBuffer + ---@param SlashCommand CodeCompanion.SlashCommand ---@param list table { filetype = string, bufnr = number, name = string, path = string, relative_path = string } ---@return nil default = function(SlashCommand, list) @@ -68,7 +81,7 @@ local Providers = { end, ---The Telescope provider - ---@param SlashCommand CodeCompanion.SlashCommandBuffer + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil telescope = function(SlashCommand) local ok, telescope = pcall(require, "telescope.builtin") @@ -125,7 +138,7 @@ local Providers = { end, ---The fzf-lua provider - ---@param SlashCommand CodeCompanion.SlashCommandBuffer + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil fzf_lua = function(SlashCommand) local ok, fzf_lua = pcall(require, "fzf-lua") @@ -151,26 +164,28 @@ local Providers = { end, } ----@class CodeCompanion.SlashCommandBuffer -local SlashCommandBuffer = {} +---@class CodeCompanion.SlashCommand.Buffer: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Buffer +---@field execute fun(self: CodeCompanion.SlashCommand.Buffer) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandBuffer +---@class Buffer.SlashCommand ---@field Chat CodeCompanion.Chat The chat buffer ---@field config table The config of the slash command ---@field context table The context of the chat buffer from the completion menu -function SlashCommandBuffer.new(args) +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandBuffer }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandBuffer:execute() +function SlashCommand:execute() -- Use the `default` provider if no provider is set if not self.config.opts or (self.config.opts and self.config.opts.provider == "default") then local buffers = {} @@ -208,4 +223,4 @@ function SlashCommandBuffer:execute() end end -return SlashCommandBuffer +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/fetch.lua b/lua/codecompanion/helpers/slash_commands/fetch.lua index 571e7226..abd64c7f 100644 --- a/lua/codecompanion/helpers/slash_commands/fetch.lua +++ b/lua/codecompanion/helpers/slash_commands/fetch.lua @@ -9,26 +9,25 @@ CONSTANTS = { NAME = "Fetch", } ----@class CodeCompanion.SlashCommandFetch -local SlashCommandFetch = {} - ----@class CodeCompanion.SlashCommandFetch ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandFetch.new(args) +---@class CodeCompanion.SlashCommand.Fetch: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Fetch +---@field execute fun(self: CodeCompanion.SlashCommand.Fetch) +local SlashCommand = {} + +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandFetch }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandFetch:execute() +function SlashCommand:execute() local ok, adapter = pcall(require, "codecompanion.adapters.non_llm." .. self.config.opts.adapter) if not ok then ok, adapter = pcall(loadfile, self.config.opts.provider) @@ -93,4 +92,4 @@ function SlashCommandFetch:execute() end) end -return SlashCommandFetch +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/file.lua b/lua/codecompanion/helpers/slash_commands/file.lua index 58c1b090..0ce3b895 100644 --- a/lua/codecompanion/helpers/slash_commands/file.lua +++ b/lua/codecompanion/helpers/slash_commands/file.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local file_utils = require("codecompanion.utils.files") local log = require("codecompanion.utils.log") @@ -9,7 +9,7 @@ CONSTANTS = { } ---Output from the slash command in the chat buffer ----@param SlashCommand CodeCompanion.SlashCommandFile +---@param SlashCommand CodeCompanion.SlashCommand ---@param selected table The selected item from the provider { relative_path = string, path = string } ---@return nil local function output(SlashCommand, selected) @@ -33,7 +33,7 @@ end local Providers = { ---The Telescope provider - ---@param SlashCommand CodeCompanion.SlashCommandFile + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil telescope = function(SlashCommand) local ok, telescope = pcall(require, "telescope.builtin") @@ -62,7 +62,7 @@ local Providers = { end, ---The mini.pick provider - ---@param SlashCommand CodeCompanion.SlashCommandFile + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil mini_pick = function(SlashCommand) local ok, mini_pick = pcall(require, "mini.pick") @@ -85,7 +85,7 @@ local Providers = { end, ---The fzf-lua provider - ---@param SlashCommand CodeCompanion.SlashCommandFile + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil fzf_lua = function(SlashCommand) local ok, fzf_lua = pcall(require, "fzf-lua") @@ -109,26 +109,25 @@ local Providers = { end, } ----@class CodeCompanion.SlashCommandFile -local SlashCommandFile = {} +---@class CodeCompanion.SlashCommand.File: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.File +---@field execute fun(self: CodeCompanion.SlashCommand.File) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandFile ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandFile.new(args) +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandFile }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandFile:execute() +function SlashCommand:execute() if self.config.opts and self.config.opts.provider then local provider = Providers[self.config.opts.provider] if not provider then @@ -140,4 +139,4 @@ function SlashCommandFile:execute() end end -return SlashCommandFile +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/help.lua b/lua/codecompanion/helpers/slash_commands/help.lua index 95934de4..99f9fee5 100644 --- a/lua/codecompanion/helpers/slash_commands/help.lua +++ b/lua/codecompanion/helpers/slash_commands/help.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local file_utils = require("codecompanion.utils.files") local log = require("codecompanion.utils.log") @@ -62,7 +62,7 @@ local function trim_content(content, tag) end ---Output from the slash command in the chat buffer ----@param SlashCommand CodeCompanion.SlashCommandHelp +---@param SlashCommand CodeCompanion.SlashCommand ---@param selected table The selected item from the provider { relative_path = string, path = string } ---@return nil local function output(SlashCommand, selected) @@ -92,7 +92,7 @@ end local Providers = { ---The Telescope provider - ---@param SlashCommand CodeCompanion.SlashCommandHelp + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil telescope = function(SlashCommand) local ok, telescope = pcall(require, "telescope.builtin") @@ -120,7 +120,7 @@ local Providers = { }) end, - ---@param SlashCommand CodeCompanion.SlashCommandHelp + ---@param SlashCommand CodeCompanion.SlashCommand ---@return nil mini_pick = function(SlashCommand) local ok, mini_pick = pcall(require, "mini.pick") @@ -144,26 +144,25 @@ local Providers = { ---TODO: The fzf-lua provider } ----@class CodeCompanion.SlashCommandHelp -local SlashCommandHelp = {} +---@class CodeCompanion.SlashCommand.Help: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Help +---@field execute fun(self: CodeCompanion.SlashCommand.Help) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandHelp ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandHelp.new(args) +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandHelp }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandHelp:execute() +function SlashCommand:execute() if self.config.opts and self.config.opts.provider then local provider = Providers[self.config.opts.provider] if not provider then @@ -175,4 +174,4 @@ function SlashCommandHelp:execute() end end -return SlashCommandHelp +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/now.lua b/lua/codecompanion/helpers/slash_commands/now.lua index 56daac1e..40b0a818 100644 --- a/lua/codecompanion/helpers/slash_commands/now.lua +++ b/lua/codecompanion/helpers/slash_commands/now.lua @@ -1,25 +1,24 @@ ----@class CodeCompanion.SlashCommandNow -local SlashCommandNow = {} +---@class CodeCompanion.SlashCommand.Now: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Now +---@field execute fun(self: CodeCompanion.SlashCommand.Now) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandNow ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandNow.new(args) +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandNow }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandNow:execute() +function SlashCommand:execute() local Chat = self.Chat Chat:append_to_buf({ content = os.date("%a, %d %b %Y %H:%M:%S %z") }) end -return SlashCommandNow +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/symbols.lua b/lua/codecompanion/helpers/slash_commands/symbols.lua index fbcc0041..43a31732 100644 --- a/lua/codecompanion/helpers/slash_commands/symbols.lua +++ b/lua/codecompanion/helpers/slash_commands/symbols.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") @@ -76,26 +76,25 @@ local Queries = { ]], } ----@class CodeCompanion.SlashCommandSymbols -local SlashCommandSymbols = {} +---@class CodeCompanion.SlashCommand.Symbols: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Symbols +---@field execute fun(self: CodeCompanion.SlashCommand.Symbols) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandSymbols ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandSymbols.new(args) +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandSymbols }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandSymbols:execute() +function SlashCommand:execute() if not config.opts.send_code and (self.config.opts and self.config.opts.contains_code) then return log:warn("Sending of code has been disabled") end @@ -176,4 +175,4 @@ function SlashCommandSymbols:execute() Chat:fold_code() end -return SlashCommandSymbols +return SlashCommand diff --git a/lua/codecompanion/helpers/slash_commands/terminal.lua b/lua/codecompanion/helpers/slash_commands/terminal.lua index b9b1bce8..08954677 100644 --- a/lua/codecompanion/helpers/slash_commands/terminal.lua +++ b/lua/codecompanion/helpers/slash_commands/terminal.lua @@ -2,26 +2,25 @@ CONSTANTS = { NAME = "Terminal Output", } ----@class CodeCompanion.SlashCommandTerminal -local SlashCommandTerminal = {} +---@class CodeCompanion.SlashCommand.Terminal: CodeCompanion.SlashCommand +---@field new fun(args: CodeCompanion.SlashCommand): CodeCompanion.SlashCommand.Terminal +---@field execute fun(self: CodeCompanion.SlashCommand.Terminal) +local SlashCommand = {} ----@class CodeCompanion.SlashCommandTerminal ----@field Chat CodeCompanion.Chat The chat buffer ----@field config table The config of the slash command ----@field context table The context of the chat buffer from the completion menu -function SlashCommandTerminal.new(args) +---@param args CodeCompanion.SlashCommand +function SlashCommand.new(args) local self = setmetatable({ Chat = args.Chat, config = args.config, context = args.context, - }, { __index = SlashCommandTerminal }) + }, { __index = SlashCommand }) return self end ---Execute the slash command ---@return nil -function SlashCommandTerminal:execute() +function SlashCommand:execute() local terminal_buf = _G.codecompanion_last_terminal if not terminal_buf then return @@ -45,4 +44,4 @@ Output: Chat:fold_code() end -return SlashCommandTerminal +return SlashCommand diff --git a/lua/codecompanion/helpers/tools/editor.lua b/lua/codecompanion/helpers/tools/editor.lua index cd430fd0..d6d2a3f5 100644 --- a/lua/codecompanion/helpers/tools/editor.lua +++ b/lua/codecompanion/helpers/tools/editor.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local keymaps = require("codecompanion.utils.keymaps") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/http.lua b/lua/codecompanion/http.lua index 99f3b250..3fab265a 100644 --- a/lua/codecompanion/http.lua +++ b/lua/codecompanion/http.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local curl = require("plenary.curl") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/init.lua b/lua/codecompanion/init.lua index e7d78b7a..db357891 100644 --- a/lua/codecompanion/init.lua +++ b/lua/codecompanion/init.lua @@ -1,17 +1,13 @@ +local config = require("codecompanion.config") local context_utils = require("codecompanion.utils.context") local log = require("codecompanion.utils.log") -local util = require("codecompanion.utils.util") local api = vim.api ---@class CodeCompanion ----@field chat fun(args?: table): nil ----@field config table ----@field buf_get_chat fun(bufnr: integer): CodeCompanion.Chat|table +---@field last_chat fun(): CodeCompanion.Chat|nil local M = {} -M.config = require("codecompanion.config") - ---Prompt the LLM from within the current buffer ---@param args table ---@return nil|CodeCompanion.Inline @@ -46,7 +42,7 @@ end M.prompt = function(name, args) local context = context_utils.get(api.nvim_get_current_buf(), args) local prompt = vim - .iter(M.config.prompt_library) + .iter(config.prompt_library) :filter(function(_, v) return v.opts.short_name and (v.opts.short_name:lower() == name:lower()) or false end) @@ -98,7 +94,7 @@ M.add = function(args) if not chat then return log:warn("No chat buffer found") end - if not M.config.opts.send_code then + if not config.opts.send_code then return log:warn("Sending of code to an LLM has been disabled") end @@ -129,7 +125,7 @@ M.chat = function(args) local prompt = args.fargs[1]:lower() -- Check if the adapter is available - adapter = M.config.adapters[prompt] + adapter = config.adapters[prompt] if not adapter then if prompt == "add" then @@ -201,13 +197,23 @@ M.actions = function(args) end ---Setup the plugin ----@param opts nil|table +---@param opts? table ---@return nil M.setup = function(opts) - M.config = vim.tbl_deep_extend("force", M.config, opts or {}) + if vim.fn.has("nvim-0.10.0") == 0 then + return vim.api.nvim_err_writeln("CodeCompanion.nvim requires Neovim 0.10.0+") + end + + config.setup(opts) if opts and opts.adapters then - require("codecompanion.utils.adapters").extend(M.config.adapters, opts.adapters) + require("codecompanion.utils.adapters").extend(config.adapters, opts.adapters) + end + + -- Create the user commands + local cmds = require("codecompanion.commands") + for _, cmd in ipairs(cmds) do + vim.api.nvim_create_user_command(cmd.cmd, cmd.callback, cmd.opts) end -- Set the highlight groups @@ -225,21 +231,21 @@ M.setup = function(opts) pattern = "codecompanion", group = group, callback = vim.schedule_wrap(function() - for name, var in pairs(M.config.strategies.chat.variables) do + for name, var in pairs(config.strategies.chat.variables) do vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. '"') if var.opts and var.opts.has_params then vim.cmd.syntax('match CodeCompanionChatVariable "#' .. name .. ':\\d\\+-\\?\\d\\+"') end end - for name, _ in pairs(M.config.strategies.agent.tools) do + for name, _ in pairs(config.strategies.agent.tools) do vim.cmd.syntax('match CodeCompanionChatTool "@' .. name .. '"') end end), }) -- TODO: Move to chat buffer - M.config.INFO_NS = api.nvim_create_namespace("CodeCompanion-info") - M.config.ERROR_NS = api.nvim_create_namespace("CodeCompanion-error") + config.INFO_NS = api.nvim_create_namespace("CodeCompanion-info") + config.ERROR_NS = api.nvim_create_namespace("CodeCompanion-error") local diagnostic_config = { underline = false, @@ -249,8 +255,8 @@ M.setup = function(opts) }, signs = false, } - vim.diagnostic.config(diagnostic_config, M.config.INFO_NS) - vim.diagnostic.config(diagnostic_config, M.config.ERROR_NS) + vim.diagnostic.config(diagnostic_config, config.INFO_NS) + vim.diagnostic.config(diagnostic_config, config.ERROR_NS) log.set_root(log.new({ handlers = { @@ -261,7 +267,7 @@ M.setup = function(opts) { type = "file", filename = "codecompanion.log", - level = vim.log.levels[M.config.opts.log_level], + level = vim.log.levels[config.opts.log_level], }, }, })) @@ -269,10 +275,10 @@ M.setup = function(opts) -- Setup cmp local has_cmp, cmp = pcall(require, "cmp") if has_cmp then - cmp.register_source("codecompanion_tools", require("cmp_codecompanion.tools").new(M.config)) + cmp.register_source("codecompanion_tools", require("cmp_codecompanion.tools").new(config)) cmp.register_source("codecompanion_variables", require("cmp_codecompanion.variables").new()) cmp.register_source("codecompanion_slash_commands", require("cmp_codecompanion.slash_commands").new()) - cmp.register_source("codecompanion_models", require("cmp_codecompanion.models").new(M.config)) + cmp.register_source("codecompanion_models", require("cmp_codecompanion.models").new(config)) cmp.setup.filetype("codecompanion", { enabled = true, sources = { diff --git a/lua/codecompanion/keymaps.lua b/lua/codecompanion/keymaps.lua index 4bc6c8c0..6ebed587 100644 --- a/lua/codecompanion/keymaps.lua +++ b/lua/codecompanion/keymaps.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local ts = require("codecompanion.utils.treesitter") local ui = require("codecompanion.utils.ui") @@ -289,7 +289,7 @@ M.change_adapter = { } end - local adapters = require("codecompanion").config.adapters + local adapters = vim.deepcopy(config.adapters) local current_adapter = chat.adapter.name local current_model = vim.deepcopy(chat.adapter.schema.model.default) diff --git a/lua/codecompanion/strategies.lua b/lua/codecompanion/strategies.lua index 51557584..29ea1489 100644 --- a/lua/codecompanion/strategies.lua +++ b/lua/codecompanion/strategies.lua @@ -1,5 +1,5 @@ local adapters = require("codecompanion.adapters") -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/strategies/chat.lua b/lua/codecompanion/strategies/chat.lua index fb2b8cd4..834741ee 100644 --- a/lua/codecompanion/strategies/chat.lua +++ b/lua/codecompanion/strategies/chat.lua @@ -1,6 +1,6 @@ local adapters = require("codecompanion.adapters") local client = require("codecompanion.http") -local config = require("codecompanion").config +local config = require("codecompanion.config") local keymaps = require("codecompanion.utils.keymaps") local schema = require("codecompanion.schema") diff --git a/lua/codecompanion/strategies/chat/tools.lua b/lua/codecompanion/strategies/chat/tools.lua index a48c9b54..f1881bf9 100644 --- a/lua/codecompanion/strategies/chat/tools.lua +++ b/lua/codecompanion/strategies/chat/tools.lua @@ -1,5 +1,5 @@ local Job = require("plenary.job") -local config = require("codecompanion").config +local config = require("codecompanion.config") local TreeHandler = require("codecompanion.utils.xml.xmlhandler.tree") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/strategies/chat/variables.lua b/lua/codecompanion/strategies/chat/variables.lua index 2750706d..5823e911 100644 --- a/lua/codecompanion/strategies/chat/variables.lua +++ b/lua/codecompanion/strategies/chat/variables.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local CONSTANTS = { diff --git a/lua/codecompanion/strategies/inline.lua b/lua/codecompanion/strategies/inline.lua index 285242bd..f466e391 100644 --- a/lua/codecompanion/strategies/inline.lua +++ b/lua/codecompanion/strategies/inline.lua @@ -1,6 +1,6 @@ local adapters = require("codecompanion.adapters") local client = require("codecompanion.http") -local config = require("codecompanion").config +local config = require("codecompanion.config") local keymaps = require("codecompanion.utils.keymaps") local log = require("codecompanion.utils.log") diff --git a/lua/codecompanion/types.lua b/lua/codecompanion/types.lua new file mode 100644 index 00000000..1511ab17 --- /dev/null +++ b/lua/codecompanion/types.lua @@ -0,0 +1,6 @@ +---@meta CodeCompanion + +---@class (exact) CodeCompanion.SlashCommand +---@field Chat CodeCompanion.Chat The chat buffer +---@field config table The config of the slash command +---@field context table The context of the chat buffer from the completion menu diff --git a/lua/codecompanion/workflow.lua b/lua/codecompanion/workflow.lua index 4036543f..9f1d83c2 100644 --- a/lua/codecompanion/workflow.lua +++ b/lua/codecompanion/workflow.lua @@ -1,4 +1,4 @@ -local config = require("codecompanion").config +local config = require("codecompanion.config") local log = require("codecompanion.utils.log") local api = vim.api diff --git a/lua/spec/codecompanion/strategies/chat/tools_spec.lua b/lua/spec/codecompanion/strategies/chat/tools_spec.lua index e6138337..8ebc6c64 100644 --- a/lua/spec/codecompanion/strategies/chat/tools_spec.lua +++ b/lua/spec/codecompanion/strategies/chat/tools_spec.lua @@ -2,7 +2,7 @@ local Chat = require("codecompanion.strategies.chat") local Tools = require("codecompanion.strategies.chat.tools") local codecompanion = require("codecompanion") -local config = require("codecompanion").config +local config = require("codecompanion.config") -- Mock dependencies config.strategies = { diff --git a/lua/spec/codecompanion/strategies/chat/variables_spec.lua b/lua/spec/codecompanion/strategies/chat/variables_spec.lua index 78b1a19a..94f2ce6b 100644 --- a/lua/spec/codecompanion/strategies/chat/variables_spec.lua +++ b/lua/spec/codecompanion/strategies/chat/variables_spec.lua @@ -2,7 +2,7 @@ local Chat = require("codecompanion.strategies.chat") local Variables = require("codecompanion.strategies.chat.variables") local codecompanion = require("codecompanion") -local config = require("codecompanion").config +local config = require("codecompanion.config") -- mock dependencies config.strategies = { diff --git a/plugin/codecompanion.lua b/plugin/codecompanion.lua deleted file mode 100644 index 59b58f5e..00000000 --- a/plugin/codecompanion.lua +++ /dev/null @@ -1,14 +0,0 @@ -if vim.fn.has("nvim-0.10.0") == 0 then - return vim.api.nvim_err_writeln("CodeCompanion.nvim requires Neovim 0.10.0+") -end - -if vim.g.loaded_codecompanion then - return -end - -local cmds = require("codecompanion.commands") -for _, cmd in ipairs(cmds) do - vim.api.nvim_create_user_command(cmd.cmd, cmd.callback, cmd.opts) -end - -vim.g.loaded_codecompanion = true