Skip to content

Commit 5724562

Browse files
committed
chore: disable copilot-chat in favour of code-companion
Install `mcp-hub` to manager MCPs
1 parent 27f902c commit 5724562

File tree

4 files changed

+390
-53
lines changed

4 files changed

+390
-53
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
-- Function to show a spinner when processing LLM request
2+
-- https://github.com/olimorris/codecompanion.nvim/discussions/640#discussioncomment-12866279
3+
-- https://github.com/yuhua99/my_dotfiles/blob/lazyvim/nvim/lua/plugins/code-companion.lua
4+
5+
local M = {
6+
processing = false,
7+
spinner_index = 1,
8+
namespace_id = nil,
9+
timer = nil,
10+
spinner_symbols = {
11+
'',
12+
'',
13+
'',
14+
'',
15+
'',
16+
'',
17+
'',
18+
'',
19+
'',
20+
'',
21+
},
22+
filetype = 'codecompanion',
23+
}
24+
25+
function M:get_buf(filetype)
26+
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
27+
if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].filetype == filetype then
28+
return buf
29+
end
30+
end
31+
return nil
32+
end
33+
34+
function M:update_spinner()
35+
if not self.processing then
36+
self:stop_spinner()
37+
return
38+
end
39+
40+
self.spinner_index = (self.spinner_index % #self.spinner_symbols) + 1
41+
42+
local buf = self:get_buf(self.filetype)
43+
if buf == nil then
44+
return
45+
end
46+
47+
-- Clear previous virtual text
48+
vim.api.nvim_buf_clear_namespace(buf, self.namespace_id, 0, -1)
49+
50+
local last_line = vim.api.nvim_buf_line_count(buf) - 1
51+
vim.api.nvim_buf_set_extmark(buf, self.namespace_id, last_line, 0, {
52+
virt_lines = { { { self.spinner_symbols[self.spinner_index] .. ' Processing...', 'Comment' } } },
53+
virt_lines_above = true, -- false means below the line
54+
})
55+
end
56+
57+
function M:start_spinner()
58+
self.processing = true
59+
self.spinner_index = 0
60+
61+
if self.timer then
62+
self.timer:stop()
63+
self.timer:close()
64+
self.timer = nil
65+
end
66+
67+
self.timer = vim.loop.new_timer()
68+
self.timer:start(
69+
0,
70+
100,
71+
vim.schedule_wrap(function()
72+
self:update_spinner()
73+
end)
74+
)
75+
end
76+
77+
function M:stop_spinner()
78+
self.processing = false
79+
80+
if self.timer then
81+
self.timer:stop()
82+
self.timer:close()
83+
self.timer = nil
84+
end
85+
86+
local buf = self:get_buf(self.filetype)
87+
if buf == nil then
88+
return
89+
end
90+
91+
vim.api.nvim_buf_clear_namespace(buf, self.namespace_id, 0, -1)
92+
end
93+
94+
function M:init()
95+
-- Create namespace for virtual text
96+
self.namespace_id = vim.api.nvim_create_namespace 'CodeCompanionSpinner'
97+
98+
vim.api.nvim_create_augroup('CodeCompanionHooks', { clear = true })
99+
local group = vim.api.nvim_create_augroup('CodeCompanionHooks', {})
100+
101+
vim.api.nvim_create_autocmd({ 'User' }, {
102+
pattern = 'CodeCompanionRequest*',
103+
group = group,
104+
callback = function(request)
105+
if request.match == 'CodeCompanionRequestStarted' then
106+
self:start_spinner()
107+
elseif request.match == 'CodeCompanionRequestFinished' then
108+
self:stop_spinner()
109+
end
110+
end,
111+
})
112+
end
113+
114+
return M

lua/custom/plugins/code-companion.lua

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

Comments
 (0)