Skip to content

Commit fb5826e

Browse files
authored
Merge branch 'main' into version-3
2 parents 63e7009 + 9784f0d commit fb5826e

File tree

5 files changed

+306
-42
lines changed

5 files changed

+306
-42
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [2.7.0](https://github.com/olimorris/codecompanion.nvim/compare/v2.6.6...v2.7.0) (2024-09-13)
4+
5+
6+
### Features
7+
8+
* add mini.diff as option for inline diffs ([#210](https://github.com/olimorris/codecompanion.nvim/issues/210)) ([a33d4ae](https://github.com/olimorris/codecompanion.nvim/commit/a33d4aefbf3d4cc21c23dcbcfc27b61fb6b26245))
9+
310
## [2.6.6](https://github.com/olimorris/codecompanion.nvim/compare/v2.6.5...v2.6.6) (2024-09-11)
411

512

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ New features are always announced <a href="https://github.com/olimorris/codecomp
2020
Thank you to the following people:
2121

2222
<p align="center">
23-
<!-- coffee --><a href="https://github.com/bassamsdata"><img src="https://github.com/bassamsdata.png" width="60px" alt="Bassam Data" /></a><a href="https://github.com/ivo-toby"><img src="https://github.com/ivo-toby.png" width="60px" alt="Ivo Toby" /></a><a href="https://github.com/KTSCode"><img src="https://github.com/KTSCode.png" width="60px" alt="KTS Code" /></a><!-- coffee --><!-- sponsors --><a href="https://github.com/mtmr0x"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;5414299?u&#x3D;b60f401c665a5aecd45bf4a5c79f7fced0e85b6d&amp;v&#x3D;4" width="60px" alt="Matheus Marsiglio" /></a><a href="https://github.com/unicell"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;35352?u&#x3D;1de708f9084ea3ea710294a38694414af4c6ed53&amp;v&#x3D;4" width="60px" alt="Qiu Yu" /></a><a href="https://github.com/zhming0"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;1054703?u&#x3D;b173a2c1afc61fa25d9343704659630406e3dea7&amp;v&#x3D;4" width="60px" alt="Zhiming Guo" /></a><!-- sponsors -->
23+
<!-- coffee --><a href="https://x.com/luxus"><img src="https://pbs.twimg.com/profile_images/744754093495844864/GwnEJygG_400x400.jpg" width="60px" alt="Luxus" /></a><a href="https://github.com/bassamsdata"><img src="https://github.com/bassamsdata.png" width="60px" alt="Bassam Data" /></a><a href="https://github.com/ivo-toby"><img src="https://github.com/ivo-toby.png" width="60px" alt="Ivo Toby" /></a><a href="https://github.com/KTSCode"><img src="https://github.com/KTSCode.png" width="60px" alt="KTS Code" /></a><!-- coffee --><!-- sponsors --><a href="https://github.com/mtmr0x"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;5414299?u&#x3D;b60f401c665a5aecd45bf4a5c79f7fced0e85b6d&amp;v&#x3D;4" width="60px" alt="Matheus Marsiglio" /></a><a href="https://github.com/unicell"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;35352?u&#x3D;1de708f9084ea3ea710294a38694414af4c6ed53&amp;v&#x3D;4" width="60px" alt="Qiu Yu" /></a><a href="https://github.com/zhming0"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;1054703?u&#x3D;b173a2c1afc61fa25d9343704659630406e3dea7&amp;v&#x3D;4" width="60px" alt="Zhiming Guo" /></a><a href="https://github.com/koskeller"><img src="https:&#x2F;&#x2F;avatars.githubusercontent.com&#x2F;u&#x2F;30801340?u&#x3D;b89bc8fe35246370a30bbe59719c3b32459f4129&amp;v&#x3D;4" width="60px" alt="Konstantin Keller" /></a><!-- sponsors -->
2424
</p>
2525

2626
<!-- panvimdoc-ignore-end -->
@@ -865,6 +865,8 @@ Use Markdown formatting and include the programming language name at the start o
865865
layout = "vertical", -- vertical|horizontal|buffer
866866
diff = {
867867
enabled = true,
868+
-- mini_diff is using inline diff in the same buffer but requires the plugin to be installed: https://github.com/echasnovski/mini.diff
869+
diff_method = "default", -- default|mini_diff
868870
close_chat_at = 240, -- Close an open chat buffer if the total columns of your display are less than...
869871
layout = "vertical", -- vertical|horizontal
870872
opts = { "internal", "filler", "closeoff", "algorithm:patience", "followwrap", "linematch:120" },
@@ -1331,6 +1333,27 @@ require('legendary').setup({
13311333
})
13321334
```
13331335

1336+
**Mini.Diff**
1337+
1338+
if you're using [mini.diff](https://github.com/echasnovski/mini.diff) you can put an icon in the statusline to indicate which diff is used currently, git or llm changes:
1339+
1340+
```lua
1341+
local function getDiffSource()
1342+
local buf_id, diff_source, diffIcon
1343+
buf_id = vim.api.nvim_get_current_buf()
1344+
diff_source = vim.b[buf_id].diffCompGit
1345+
if not diff_source then
1346+
return ""
1347+
end
1348+
if diff_source == "git" then
1349+
diffIcon = "󰊤 "
1350+
elseif diff_source == "llm" then
1351+
diffIcon = ""
1352+
end
1353+
return string.format("%%#StatusLineLSP#%s", diffIcon)
1354+
end
1355+
```
1356+
13341357
## :toolbox: Troubleshooting
13351358

13361359
Before raising an [issue](https://github.com/olimorris/codecompanion.nvim/issues), there are a number of steps you can take to troubleshoot a problem:

lua/codecompanion/config.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ Use Markdown formatting and include the programming language name at the start o
613613
layout = "vertical", -- vertical|horizontal|buffer
614614
diff = {
615615
enabled = true,
616+
diff_method = "default", -- default|mini_diff
616617
close_chat_at = 240, -- Close an open chat buffer if the total columns of your display are less than...
617618
layout = "vertical", -- vertical|horizontal
618619
opts = { "internal", "filler", "closeoff", "algorithm:patience", "followwrap", "linematch:120" },

lua/codecompanion/strategies/inline.lua

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local config = require("codecompanion").config
44

55
local keymaps = require("codecompanion.utils.keymaps")
66
local log = require("codecompanion.utils.log")
7+
local mini_diff = require("codecompanion.strategies.inline.mini_diff")
78
local msg_utils = require("codecompanion.utils.messages")
89
local ui = require("codecompanion.utils.ui")
910
local util = require("codecompanion.utils.util")
@@ -173,6 +174,13 @@ end
173174
---Start the classification of the user's prompt
174175
---@param opts? table
175176
function Inline:start(opts)
177+
-- NOTE: we need to add this here to intiate the mini.diff early to be
178+
-- working properly
179+
if config.display.inline.diff.diff_method == "mini_diff" then
180+
log:trace("CodeCompanion: Using mini diff for inline display")
181+
require("codecompanion.strategies.inline.mini_diff").setup()
182+
end
183+
176184
log:trace("Starting Inline with opts: %s", opts)
177185

178186
if opts and opts[1] then
@@ -562,61 +570,72 @@ function Inline:start_diff()
562570
if config.display.inline.diff.enabled == false then
563571
return
564572
end
573+
if config.display.inline.diff.diff_method == "mini_diff" then
574+
return -- no need to do anything here, since it's handled in mini_diff.lua
575+
else
576+
-- Taken from the awesome:
577+
-- https://github.com/S1M0N38/dante.nvim
565578

566-
-- Taken from the awesome:
567-
-- https://github.com/S1M0N38/dante.nvim
568-
569-
-- Get current window properties
570-
local wrap = vim.wo.wrap
571-
local linebreak = vim.wo.linebreak
572-
local breakindent = vim.wo.breakindent
573-
vim.cmd("set diffopt=" .. table.concat(config.display.inline.diff.opts, ","))
579+
-- Get current window properties
580+
local wrap = vim.wo.wrap
581+
local linebreak = vim.wo.linebreak
582+
local breakindent = vim.wo.breakindent
583+
vim.cmd("set diffopt=" .. table.concat(config.display.inline.diff.opts, ","))
574584

575-
-- Close the chat buffer
576-
local last_chat = require("codecompanion").last_chat()
577-
if last_chat and last_chat:is_visible() and config.display.inline.diff.close_chat_at > vim.o.columns then
578-
last_chat:hide()
579-
end
585+
-- Close the chat buffer
586+
local last_chat = require("codecompanion").last_chat()
587+
if last_chat and last_chat:is_visible() and config.display.inline.diff.close_chat_at > vim.o.columns then
588+
last_chat:hide()
589+
end
580590

581-
-- Create the diff buffer
582-
if config.display.inline.diff.layout == "vertical" then
583-
vim.cmd("vsplit")
584-
else
585-
vim.cmd("split")
591+
-- Create the diff buffer
592+
if config.display.inline.diff.layout == "vertical" then
593+
vim.cmd("vsplit")
594+
else
595+
vim.cmd("split")
596+
end
597+
self.diff.winnr = api.nvim_get_current_win()
598+
self.diff.bufnr = api.nvim_create_buf(false, true)
599+
api.nvim_win_set_buf(self.diff.winnr, self.diff.bufnr)
600+
api.nvim_set_option_value("filetype", self.context.filetype, { buf = self.diff.bufnr })
601+
api.nvim_set_option_value("wrap", wrap, { win = self.diff.winnr })
602+
api.nvim_set_option_value("linebreak", linebreak, { win = self.diff.winnr })
603+
api.nvim_set_option_value("breakindent", breakindent, { win = self.diff.winnr })
604+
605+
-- Set the diff buffer to the contents, prior to any modifications
606+
api.nvim_buf_set_lines(self.diff.bufnr, 0, 0, true, self.diff.lines)
607+
api.nvim_win_set_cursor(self.diff.winnr, { self.context.cursor_pos[1], self.context.cursor_pos[2] })
608+
609+
-- Begin diffing
610+
api.nvim_set_current_win(self.diff.winnr)
611+
vim.cmd("diffthis")
612+
api.nvim_set_current_win(self.context.winnr)
613+
vim.cmd("diffthis")
586614
end
587-
self.diff.winnr = api.nvim_get_current_win()
588-
self.diff.bufnr = api.nvim_create_buf(false, true)
589-
api.nvim_win_set_buf(self.diff.winnr, self.diff.bufnr)
590-
api.nvim_set_option_value("filetype", self.context.filetype, { buf = self.diff.bufnr })
591-
api.nvim_set_option_value("wrap", wrap, { win = self.diff.winnr })
592-
api.nvim_set_option_value("linebreak", linebreak, { win = self.diff.winnr })
593-
api.nvim_set_option_value("breakindent", breakindent, { win = self.diff.winnr })
594-
595-
-- Set the diff buffer to the contents, prior to any modifications
596-
api.nvim_buf_set_lines(self.diff.bufnr, 0, 0, true, self.diff.lines)
597-
api.nvim_win_set_cursor(self.diff.winnr, { self.context.cursor_pos[1], self.context.cursor_pos[2] })
598-
599-
-- Begin diffing
600-
api.nvim_set_current_win(self.diff.winnr)
601-
vim.cmd("diffthis")
602-
api.nvim_set_current_win(self.context.winnr)
603-
vim.cmd("diffthis")
604615
end
605616

606617
---Accept the changes in the diff
607618
---@return nil
608619
function Inline:accept()
609-
api.nvim_win_close(self.diff.winnr, false)
610-
self.diff = {}
620+
if config.display.inline.diff.diff_method == "mini_diff" then
621+
mini_diff.accept(self.context.bufnr)
622+
else
623+
api.nvim_win_close(self.diff.winnr, false)
624+
self.diff = {}
625+
end
611626
end
612627

613628
---Reject the changes in the diff
614629
---@return nil
615630
function Inline:reject()
616-
vim.cmd("diffoff")
617-
api.nvim_win_close(self.diff.winnr, false)
618-
api.nvim_buf_set_lines(self.context.bufnr, 0, -1, true, self.diff.lines)
619-
self.diff = {}
631+
if config.display.inline.diff.diff_method == "mini_diff" then
632+
mini_diff.reject(self.context.bufnr)
633+
else
634+
vim.cmd("diffoff")
635+
api.nvim_win_close(self.diff.winnr, false)
636+
api.nvim_buf_set_lines(self.context.bufnr, 0, -1, true, self.diff.lines)
637+
self.diff = {}
638+
end
620639
end
621640

622641
return Inline

0 commit comments

Comments
 (0)