Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita88874e

Browse files
authored
feat(diff): apply all code blocks for a file at once when showing diff (#1409)
Refactored diff preview logic to process all code blocks for a filein one pass, improving consistency and correctness. Updated diff utilityfunctions to accept lines instead of buffer numbers, and adjusted contextlengths for more accurate region detection.Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent19a4d29 commita88874e

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

‎lua/CopilotChat/config/mappings.lua‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,10 @@ return {
172172

173173
localpath=block.header.filename
174174
localbufnr=prepare_diff_buffer(path,source)
175-
localnew_lines=diff.apply_diff(block,bufnr)
175+
locallines=vim.api.nvim_buf_get_lines(bufnr,0,-1,false)
176+
localnew_lines=diff.apply_diff(block,lines)
176177
vim.api.nvim_buf_set_lines(bufnr,0,-1,false,new_lines)
177-
localfirst,last=diff.get_diff_region(block,bufnr)
178+
localfirst,last=diff.get_diff_region(block,lines)
178179
iffirstandlastthen
179180
select.set(bufnr,source.winnr,first,last)
180181
select.highlight(bufnr)
@@ -192,7 +193,8 @@ return {
192193

193194
localpath=block.header.filename
194195
localbufnr=prepare_diff_buffer(path,source)
195-
localfirst,last=diff.get_diff_region(block,bufnr)
196+
locallines=vim.api.nvim_buf_get_lines(bufnr,0,-1,false)
197+
localfirst,last=diff.get_diff_region(block,lines)
196198
iffirstandlastandbufnrthen
197199
select.set(bufnr,source.winnr,first,last)
198200
select.highlight(bufnr)
@@ -223,7 +225,25 @@ return {
223225

224226
localpath=block.header.filename
225227
localbufnr=prepare_diff_buffer(path,source)
226-
localnew_lines=diff.apply_diff(block,bufnr)
228+
229+
-- Collect all blocks for the same filename
230+
localmessage=copilot.chat:get_message(constants.ROLE.ASSISTANT,true)
231+
localblocks= {}
232+
ifmessageandmessage.sectionandmessage.section.blocksthen
233+
for_,binipairs(message.section.blocks)do
234+
ifb.header.filename==paththen
235+
table.insert(blocks,b)
236+
end
237+
end
238+
else
239+
blocks= {block }
240+
end
241+
242+
-- Apply all diffs for the filename
243+
localnew_lines=vim.api.nvim_buf_get_lines(bufnr,0,-1,false)
244+
fori=#blocks,1,-1do
245+
new_lines=diff.apply_diff(blocks[i],new_lines)
246+
end
227247

228248
localopts= {
229249
filetype=vim.bo[bufnr].filetype,

‎lua/CopilotChat/utils/diff.lua‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ function M.apply_unified_diff(diff_text, original_content)
120120
new_content=patched
121121
applied=appliedorok
122122
end
123-
localoriginal_lines=vim.split(original_content,'\n')
124-
localnew_lines=vim.split(new_content,'\n')
123+
localoriginal_lines=vim.split(original_content,'\n', {trimempty=true })
124+
localnew_lines=vim.split(new_content,'\n', {trimempty=true })
125125
localfirst,last
126126
localmax_len=math.max(#original_lines,#new_lines)
127127
fori=1,max_lendo
@@ -137,10 +137,9 @@ end
137137

138138
--- Get diff from block content and buffer lines
139139
---@paramblockCopilotChat.ui.chat.Block Block containing diff info
140-
---@parambufnrinteger Buffer number
140+
---@paramlinestable table of lines
141141
---@returnstring diff,string content
142-
functionM.get_diff(block,bufnr)
143-
locallines=vim.api.nvim_buf_get_lines(bufnr,0,-1,false)
142+
functionM.get_diff(block,lines)
144143
localcontent=table.concat(lines,'\n')
145144
ifblock.header.filetype=='diff'then
146145
returnblock.content,content
@@ -161,18 +160,18 @@ function M.get_diff(block, bufnr)
161160
vim.diff(
162161
table.concat(original_lines,'\n'),
163162
table.concat(patched_lines,'\n'),
164-
{algorithm='myers',ctxlen=20,interhunkctxlen=50,ignore_whitespace_change=true }
163+
{algorithm='myers',ctxlen=10,interhunkctxlen=10,ignore_whitespace_change=true }
165164
)
166165
),
167166
content
168167
end
169168

170169
--- Apply a diff (unified or indices) to buffer lines
171170
---@paramblockCopilotChat.ui.chat.Block Block containing diff info
172-
---@parambufnrinteger Buffer number
171+
---@paramlinestable table of lines
173172
---@returntable new_lines
174-
functionM.apply_diff(block,bufnr)
175-
localdiff,content=M.get_diff(block,bufnr)
173+
functionM.apply_diff(block,lines)
174+
localdiff,content=M.get_diff(block,lines)
176175
localnew_lines,applied,_,_=M.apply_unified_diff(diff,content)
177176
ifnotappliedthen
178177
log.debug('Diff for'..block.header.filename..' failed to apply cleanly for:\n'..diff)
@@ -183,10 +182,10 @@ end
183182

184183
--- Get changed region for diff (unified or indices)
185184
---@paramblockCopilotChat.ui.chat.Block Block containing diff info
186-
---@parambufnrinteger Buffer number
185+
---@paramlinestable table of lines
187186
---@returnnumber?first,number?last
188-
functionM.get_diff_region(block,bufnr)
189-
localdiff,content=M.get_diff(block,bufnr)
187+
functionM.get_diff_region(block,lines)
188+
localdiff,content=M.get_diff(block,lines)
190189
local_,_,first,last=M.apply_unified_diff(diff,content)
191190
returnfirst,last
192191
end

‎tests/diff_spec.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ describe('CopilotChat.utils.diff', function()
179179
localoriginal_content=table.concat(original,'\n')
180180
localresult,applied=diff.apply_unified_diff(diff_text,original_content)
181181
assert.is_true(applied)
182-
assert.are.same({''},result)
182+
assert.are.same({},result)
183183
end)
184184

185185
it('applies unified diff with all lines added to empty file',function()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp