Skip to content

Commit 75ab0bd

Browse files
committed
fix: patch aws#2133 and handle more variants of FIM suggestions
1 parent a908195 commit 75ab0bd

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

server/aws-lsp-codewhisperer/src/language-server/inline-completion/utils/diffUtils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ describe('categorizeUnifieddiffV2v2 should return correct type (addOnly, edit, d
126126
{
127127
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
128128
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
129+
@@ -6,7 +6,11 @@
130+
- return a * b;
131+
+ return a * b * c;
132+
}`,
133+
},
134+
{
135+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
136+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
137+
@@ -6,7 +6,11 @@
138+
- return a * b;
139+
+ return a * b *
140+
+ c * d;
141+
}`,
142+
},
143+
{
144+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
145+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
129146
@@ -6,7 +6,11 @@
130147
131148
// write a function to subtract 2 numbers
@@ -300,6 +317,15 @@ describe('categorizeUnifieddiffV2v2 should return correct type (addOnly, edit, d
300317
const languageId = getLanguageIdFromUri(uri)
301318
textDocument = TextDocument.create(uri, languageId, 0, content)`,
302319
},
320+
{
321+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
322+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
323+
@@ -6,7 +6,11 @@
324+
- return a * b;
325+
+ return a * b * c;
326+
+ }
327+
}`,
328+
},
303329
]
304330

305331
for (let i = 0; i < cases.length; i++) {

server/aws-lsp-codewhisperer/src/language-server/inline-completion/utils/diffUtils.ts

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,24 @@ export function readUdiff(unifiedDiff: string): UnifiedDiff {
275275
const firstMinusIndex = relevantLines.findIndex(s => s.startsWith('-'))
276276
const firstPlusIndex = relevantLines.findIndex(s => s.startsWith('+'))
277277

278+
// TODO: Comment these out as they are used for a different version of addonly type determination logic in case the current implementation doesn't work.
279+
// Could remove later if we are sure current imple works.
280+
/**
281+
* Concatenate all contiguous added lines (i.e., unbroken sequence of "+"s).
282+
* Exclude all newlines when concatenating, so we get a single line representing the new text
283+
*/
284+
// let singleLine = ''
285+
// let prev: number | undefined
286+
// for (const idx of plusIndexes) {
287+
// if (!prev || idx === prev + 1) {
288+
// const removedPlus = relevantLines[idx].substring(1)
289+
// const removedStartNewline = trimStartNewline(removedPlus)
290+
// singleLine += removedStartNewline
291+
// } else {
292+
// break
293+
// }
294+
// }
295+
278296
return {
279297
linesWithoutHeaders: relevantLines,
280298
firstMinusIndex: firstMinusIndex,
@@ -291,14 +309,17 @@ export function categorizeUnifieddiff(unifiedDiff: string): 'addOnly' | 'deleteO
291309
const firstPlusIndex = d.firstPlusIndex
292310
const diffWithoutHeaders = d.linesWithoutHeaders
293311

312+
// Shouldn't be the case but if there is no - nor +, assume it's an edit
294313
if (firstMinusIndex === -1 && firstPlusIndex === -1) {
295314
return 'edit'
296315
}
297316

317+
// Naive case, only +
298318
if (firstMinusIndex === -1 && firstPlusIndex !== -1) {
299319
return 'addOnly'
300320
}
301321

322+
// Naive case, only -
302323
if (firstMinusIndex !== -1 && firstPlusIndex === -1) {
303324
return 'deleteOnly'
304325
}
@@ -321,12 +342,47 @@ export function categorizeUnifieddiff(unifiedDiff: string): 'addOnly' | 'deleteO
321342

322343
// If last '-' line is followed by '+' block, it could be addonly
323344
if (plusIndexes[0] === minusIndexes[minusIndexes.length - 1] + 1) {
345+
/**
346+
-------------------------------
347+
- return
348+
+ return a - b;
349+
-------------------------------
350+
commonPrefix = "return "
351+
minusLinesDelta = ""
352+
353+
--------------------------------
354+
-\t\t\t
355+
+\treturn a - b;
356+
--------------------------------
357+
commonPrefix = "\t"
358+
minusLinesDelta = "\t\t"
359+
360+
*
361+
*
362+
*
363+
*/
324364
const minusLine = diffWithoutHeaders[minusIndexes[minusIndexes.length - 1]].substring(1)
325365
const pluscode = extractAdditions(unifiedDiff)
326366

327367
// If minusLine subtract the longest common substring of minusLine and plugcode and it's empty string, it's addonly
328368
const commonPrefix = longestCommonPrefix(minusLine, pluscode)
329-
if (minusLine.substring(commonPrefix.length).trim().length === 0) {
369+
const minusLinesDelta = minusLine.substring(commonPrefix.length)
370+
if (minusLinesDelta.trim().length === 0) {
371+
return 'addOnly'
372+
}
373+
374+
/**
375+
-------------------------------
376+
- return a * b;
377+
+ return a * b * c;
378+
-------------------------------
379+
commonPrefix = "return a * b"
380+
minusLinesDelta = ";"
381+
pluscodeDelta = " * c;"
382+
*
383+
*/
384+
const pluscodeDelta = pluscode.substring(commonPrefix.length)
385+
if (pluscodeDelta.endsWith(minusLinesDelta)) {
330386
return 'addOnly'
331387
}
332388
}
@@ -400,3 +456,23 @@ export function longestCommonPrefix(str1: string, str2: string): string {
400456

401457
return prefix
402458
}
459+
460+
// TODO: They are used for a different version of addonly type determination logic in case the current implementation doesn't work.
461+
// Could remove later if we are sure current impl works.
462+
// function trimStartNewline(str: string): string {
463+
// return str.replace(/^[\n\r]+/, '')
464+
// }
465+
466+
// function hasOneContiguousInsert(original: string, changed: string) {
467+
// const delta = changed.length - original.length
468+
// if (delta <= 0) {
469+
// // Changed string must be longer
470+
// return false
471+
// }
472+
473+
// let p, s
474+
// for (p = 0; original[p] === changed[p] && p < original.length; ++p);
475+
// for (s = original.length - 1; original[s] === changed[s + delta] && s >= 0; --s);
476+
477+
// return p === s + 1
478+
// }

0 commit comments

Comments
 (0)