Skip to content

Commit

Permalink
refactor: clean up config and lua types
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Oct 9, 2024
1 parent c5c741d commit 54b3107
Show file tree
Hide file tree
Showing 39 changed files with 206 additions and 172 deletions.
13 changes: 9 additions & 4 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"diagnostics.globals": [
"vim"
]
}
"runtime": {
"version": "LuaJIT",
"pathStrict": true
},
"diagnostics.globals": ["vim"],
"type": {
"checkTableShape": true
}
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <your prompt>` - Prompt the inline assistant
- `CodeCompanion /<prompt library>` - Use the [prompt library](#clipboard-prompt-library) with the inline assistant e.g. `/commit`
- `CodeCompanionChat` - Open a chat buffer
- `CodeCompanionChat <prompt>` - Send a prompt to the LLM via a chat buffer
- `CodeCompanionChat <adapter>` - 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**

Expand Down
11 changes: 7 additions & 4 deletions doc/codecompanion.txt
Original file line number Diff line number Diff line change
@@ -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*
Expand Down Expand Up @@ -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 <your prompt>` - Prompt the inline assistant
- `CodeCompanion /<prompt library>` - Use the |codecompanion-prompt-library| with the inline assistant e.g. `/commit`
- `CodeCompanionChat` - Open a chat buffer
- `CodeCompanionChat <prompt>` - Send a prompt to the LLM via a chat buffer
- `CodeCompanionChat <adapter>` - 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**

Expand Down
2 changes: 1 addition & 1 deletion lua/cmp_codecompanion/slash_commands.lua
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/actions/init.lua
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/actions/providers/default.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 0 additions & 1 deletion lua/codecompanion/actions/providers/mini_pick.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/actions/providers/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
5 changes: 1 addition & 4 deletions lua/codecompanion/adapters.lua
Original file line number Diff line number Diff line change
@@ -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:"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/adapters/anthropic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
5 changes: 4 additions & 1 deletion lua/codecompanion/adapters/copilot.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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,
Expand All @@ -96,7 +99,7 @@ local function authorize_token()
return _github_token
end

---@class CodeCompanion.AdapterArgs
---@class Copilot.Adapter: CodeCompanion.Adapter
return {
name = "copilot",
roles = {
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/adapters/gemini.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions lua/codecompanion/adapters/ollama.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/adapters/openai.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local log = require("codecompanion.utils.log")

---@class CodeCompanion.AdapterArgs
---@class OpenAI.Adapter: CodeCompanion.Adapter
return {
name = "openai",
roles = {
Expand Down
2 changes: 1 addition & 1 deletion lua/codecompanion/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion lua/codecompanion/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local fmt = string.format

return {
local defaults = {
adapters = {
-- LLMs -------------------------------------------------------------------
anthropic = "anthropic",
Expand Down Expand Up @@ -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,
})
2 changes: 1 addition & 1 deletion lua/codecompanion/helpers/diff/default.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
2 changes: 0 additions & 2 deletions lua/codecompanion/helpers/diff/mini_diff.lua
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
61 changes: 38 additions & 23 deletions lua/codecompanion/helpers/slash_commands/buffer.lua
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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 = {}
Expand Down Expand Up @@ -208,4 +223,4 @@ function SlashCommandBuffer:execute()
end
end

return SlashCommandBuffer
return SlashCommand
Loading

0 comments on commit 54b3107

Please sign in to comment.