|
| 1 | +-- This file configures the CodeCompanion.nvim plugin for Neovim. |
| 2 | +-- It includes dependencies, options, and extensions to enhance the development experience. |
| 3 | +-- https://codecompanion.olimorris.dev/installation.html |
| 4 | + |
| 5 | +return { |
| 6 | + 'olimorris/codecompanion.nvim', |
| 7 | + lazy = false, |
| 8 | + dependencies = { |
| 9 | + { 'nvim-lua/plenary.nvim', branch = 'master' }, |
| 10 | + 'nvim-treesitter/nvim-treesitter', |
| 11 | + 'ravitemer/mcphub.nvim', |
| 12 | + { |
| 13 | + 'saghen/blink.cmp', |
| 14 | + ---@module 'blink.cmp' |
| 15 | + ---@type blink.cmp.Config |
| 16 | + opts = { |
| 17 | + sources = { |
| 18 | + default = { 'codecompanion' }, |
| 19 | + providers = { |
| 20 | + codecompanion = { |
| 21 | + name = 'CodeCompanion', |
| 22 | + module = 'codecompanion.providers.completion.blink', |
| 23 | + enabled = true, |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + opts_extend = { |
| 29 | + 'sources.default', |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + opts = { |
| 34 | + -- opts = { |
| 35 | + -- log_level = 'DEBUG', |
| 36 | + -- }, |
| 37 | + display = { |
| 38 | + action_palette = { |
| 39 | + provider = 'snacks', |
| 40 | + }, |
| 41 | + chat = { |
| 42 | + -- Enable to inspect the chat settings, but when enabled is impossible to change provider |
| 43 | + show_settings = false, |
| 44 | + }, |
| 45 | + }, |
| 46 | + adapters = { |
| 47 | + opts = { |
| 48 | + show_defaults = true, |
| 49 | + show_model_choices = true, |
| 50 | + }, |
| 51 | + copilot_claude = function() |
| 52 | + return require('codecompanion.adapters').extend('copilot', { |
| 53 | + name = 'copilot_claude', |
| 54 | + schema = { |
| 55 | + model = { |
| 56 | + default = 'claude-3.7-sonnet', |
| 57 | + }, |
| 58 | + }, |
| 59 | + }) |
| 60 | + end, |
| 61 | + }, |
| 62 | + strategies = { |
| 63 | + chat = { |
| 64 | + adapter = 'copilot_claude', |
| 65 | + keymaps = { |
| 66 | + send = { |
| 67 | + callback = function(chat) |
| 68 | + vim.cmd 'stopinsert' |
| 69 | + chat:add_buf_message { role = 'llm', content = '' } |
| 70 | + chat:submit() |
| 71 | + end, |
| 72 | + index = 1, |
| 73 | + description = 'Send', |
| 74 | + }, |
| 75 | + }, |
| 76 | + slash_commands = { |
| 77 | + -- ['git_diff'] = { |
| 78 | + -- description = 'Show git diff', |
| 79 | + -- ---@param chat CodeCompanion.Chat |
| 80 | + -- callback = function(chat) |
| 81 | + -- local handle = io.popen 'git diff --no-ext-diff' |
| 82 | + -- if handle ~= nil then |
| 83 | + -- local result = handle:read '*a' |
| 84 | + -- handle:close() |
| 85 | + -- if result ~= '' then |
| 86 | + -- chat:add_reference({ role = 'user', content = result }, 'git', '<git_diff>') |
| 87 | + -- else |
| 88 | + -- return vim.notify('No changes in git diff', vim.log.levels.INFO, { title = 'CodeCompanion' }) |
| 89 | + -- end |
| 90 | + -- else |
| 91 | + -- return vim.notify('Could not retrieve git diff', vim.log.levels.ERROR, { title = 'CodeCompanion' }) |
| 92 | + -- end |
| 93 | + -- end, |
| 94 | + -- opts = { |
| 95 | + -- contains_code = true, |
| 96 | + -- }, |
| 97 | + -- }, |
| 98 | + ['git_diff'] = { |
| 99 | + description = 'Show git diff', |
| 100 | + ---@param chat CodeCompanion.Chat |
| 101 | + callback = function(chat) |
| 102 | + local snacks = require 'snacks' |
| 103 | + |
| 104 | + snacks.picker.pick { |
| 105 | + source = 'custom_git_diff_source', |
| 106 | + title = 'Git diff sources', |
| 107 | + items = { |
| 108 | + { text = 'Working Tree' }, |
| 109 | + { text = 'Staged' }, |
| 110 | + }, |
| 111 | + format = function(item, picker) |
| 112 | + local ret = { |
| 113 | + { item.text, item.text_hl }, |
| 114 | + } ---@type snacks.picker.Highlight[] |
| 115 | + return ret |
| 116 | + end, |
| 117 | + single_select = true, |
| 118 | + layout = { |
| 119 | + fullscreen = false, |
| 120 | + hidden = { 'preview' }, |
| 121 | + }, |
| 122 | + confirm = function(picker, item) |
| 123 | + picker:close() |
| 124 | + local cmd = item.text == 'Working Tree' and 'git diff --no-ext-diff' or 'git diff --no-ext-diff --staged' |
| 125 | + |
| 126 | + local handle = io.popen(cmd) |
| 127 | + if handle ~= nil then |
| 128 | + local result = handle:read '*a' |
| 129 | + handle:close() |
| 130 | + if result ~= '' then |
| 131 | + chat:add_reference({ role = 'user', content = result }, 'git', '<git_diff>') |
| 132 | + else |
| 133 | + vim.notify('No changes in git diff', vim.log.levels.INFO, { title = 'CodeCompanion' }) |
| 134 | + end |
| 135 | + else |
| 136 | + vim.notify('Could not retrieve git diff', vim.log.levels.ERROR, { title = 'CodeCompanion' }) |
| 137 | + end |
| 138 | + end, |
| 139 | + } |
| 140 | + end, |
| 141 | + opts = { |
| 142 | + contains_code = true, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + inline = { adapter = 'copilot_claude' }, |
| 148 | + }, |
| 149 | + -- |
| 150 | + extensions = { |
| 151 | + mcphub = { |
| 152 | + callback = 'mcphub.extensions.codecompanion', |
| 153 | + opts = { |
| 154 | + make_vars = true, |
| 155 | + make_slash_commands = true, |
| 156 | + show_result_in_chat = true, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + prompt_library = { |
| 161 | + ['Review git diff'] = { |
| 162 | + strategy = 'chat', |
| 163 | + description = 'Review git diff', |
| 164 | + opts = { |
| 165 | + index = 12, |
| 166 | + is_default = true, |
| 167 | + is_slash_cmd = true, |
| 168 | + short_name = 'git_review', |
| 169 | + auto_submit = true, |
| 170 | + }, |
| 171 | + prompts = { |
| 172 | + { |
| 173 | + role = 'user', |
| 174 | + content = function() |
| 175 | + return string.format( |
| 176 | + [[You are an expert Senior Software Engineer tasked to review some code. |
| 177 | +Follow these steps: |
| 178 | +1. Identify the programming language |
| 179 | +2. Look at the git diff and contextualize it |
| 180 | +3. Read carefully the changes and try to understand the final goal, if not clear, ask to the user. |
| 181 | +4. Identify potential issues, improvements, and best practices |
| 182 | +5. Consider any security risks |
| 183 | +6. Consider alternative approaches and optimizations to reach the same result |
| 184 | +7. Provide a summary of your review and suggestions for improvement |
| 185 | +
|
| 186 | +**git diff** |
| 187 | +
|
| 188 | +```diff |
| 189 | +%s |
| 190 | +```]], |
| 191 | + vim.fn.system 'git diff --no-ext-diff' |
| 192 | + ) |
| 193 | + end, |
| 194 | + opts = { |
| 195 | + contains_code = true, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + }, |
| 202 | + config = function(_, opts) |
| 203 | + local spinner = require 'custom.functions.code-companion-spinner' |
| 204 | + spinner:init() |
| 205 | + |
| 206 | + -- Setup the entire opts table |
| 207 | + require('codecompanion').setup(opts) |
| 208 | + end, |
| 209 | +} |
0 commit comments