Skip to content

Commit 4f5a9da

Browse files
authored
fix: addonly EDITS should be handled as COMPLETIONS (#2133)
1 parent ed8c6dd commit 4f5a9da

File tree

4 files changed

+544
-21
lines changed

4 files changed

+544
-21
lines changed

server/aws-lsp-codewhisperer/src/language-server/inline-completion/handler/editCompletionHandler.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { AmazonQError, AmazonQServiceConnectionExpiredError } from '../../../sha
3838
import { DocumentChangedListener } from '../documentChangedListener'
3939
import { EMPTY_RESULT, EDIT_DEBOUNCE_INTERVAL_MS } from '../contants/constants'
4040
import { StreakTracker } from '../tracker/streakTracker'
41+
import { processEditSuggestion } from '../utils/diffUtils'
4142

4243
export class EditCompletionHandler {
4344
private readonly editsEnabled: boolean
@@ -396,6 +397,13 @@ export class EditCompletionHandler {
396397
textDocument?.uri || ''
397398
)
398399

400+
const processedSuggestion = processEditSuggestion(
401+
suggestion.content,
402+
session.startPosition,
403+
session.document
404+
)
405+
const isInlineEdit = processedSuggestion.type === SuggestionType.EDIT
406+
399407
if (isSimilarToRejected) {
400408
// Mark as rejected in the session
401409
session.setSuggestionState(suggestion.itemId, 'Reject')
@@ -405,14 +413,14 @@ export class EditCompletionHandler {
405413
// Return empty item that will be filtered out
406414
return {
407415
insertText: '',
408-
isInlineEdit: true,
416+
isInlineEdit: isInlineEdit,
409417
itemId: suggestion.itemId,
410418
}
411419
}
412420

413421
return {
414-
insertText: suggestion.content,
415-
isInlineEdit: true,
422+
insertText: processedSuggestion.suggestionContent,
423+
isInlineEdit: isInlineEdit,
416424
itemId: suggestion.itemId,
417425
}
418426
})

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

Lines changed: 311 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,315 @@
11
import * as assert from 'assert'
2-
import { getAddedAndDeletedLines, getCharacterDifferences, generateDiffContexts } from './diffUtils'
2+
import {
3+
categorizeUnifieddiff,
4+
extractAdditions,
5+
getAddedAndDeletedLines,
6+
getCharacterDifferences,
7+
generateDiffContexts,
8+
} from './diffUtils'
9+
10+
describe('extractAdditions', function () {
11+
interface Case {
12+
udiff: string
13+
expected: string
14+
}
15+
16+
const cases: Case[] = [
17+
{
18+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
19+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
20+
@@ -1,9 +1,10 @@
21+
public class MathUtil {
22+
// write a function to add 2 numbers
23+
public static int add(int a, int b) {
24+
25+
+ return a + b;
26+
}
27+
28+
// write a function to subtract 2 numbers
29+
public static int subtract(int a, int b) {
30+
return a - b;`,
31+
expected: ' return a + b;',
32+
},
33+
{
34+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
35+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
36+
@@ -1,9 +1,17 @@
37+
public class MathUtil {
38+
// write a function to add 2 numbers
39+
public static int add(int a, int b) {
40+
41+
+ if (a > Integer.MAX_VALUE - b){
42+
+ throw new IllegalArgumentException("Overflow!");
43+
+ }
44+
+ else if (a < Integer.MIN_VALUE - b){
45+
+ throw new IllegalArgumentException("Underflow");
46+
+ }
47+
+ else{
48+
+ return a + b;
49+
+ }
50+
}
51+
52+
// write a function to subtract 2 numbers
53+
public static int subtract(int a, int b) {
54+
return a - b;`,
55+
expected: ` if (a > Integer.MAX_VALUE - b){
56+
throw new IllegalArgumentException("Overflow!");
57+
}
58+
else if (a < Integer.MIN_VALUE - b){
59+
throw new IllegalArgumentException("Underflow");
60+
}
61+
else{
62+
return a + b;
63+
}`,
64+
},
65+
{
66+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
67+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
68+
@@ -6,7 +6,11 @@
69+
70+
// write a function to subtract 2 numbers
71+
public static int subtract(int a, int b) {
72+
return a - b;
73+
}
74+
-
75+
+
76+
+ // write a function to multiply 2 numbers
77+
+ public static int multiply(int a, int b) {
78+
+ return a * b;
79+
+ }
80+
}`,
81+
expected: `
82+
// write a function to multiply 2 numbers
83+
public static int multiply(int a, int b) {
84+
return a * b;
85+
}`,
86+
},
87+
{
88+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
89+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
90+
@@ -3,7 +3,9 @@
91+
public static int add(int a, int b) {
92+
return a + b;
93+
}
94+
95+
// write a function to subtract 2 numbers
96+
-
97+
+ public static int subtract(int a, int b) {
98+
+ return a - b;
99+
+ }
100+
}`,
101+
expected: ` public static int subtract(int a, int b) {
102+
return a - b;
103+
}`,
104+
},
105+
]
106+
107+
for (let i = 0; i < cases.length; i++) {
108+
it(`case ${i}`, function () {
109+
const c = cases[i]
110+
const udiff = c.udiff
111+
const expected = c.expected
112+
113+
const actual = extractAdditions(udiff)
114+
assert.strictEqual(actual, expected)
115+
})
116+
}
117+
})
118+
119+
describe('categorizeUnifieddiffV2v2 should return correct type (addOnly, edit, deleteOnly)', function () {
120+
interface Case {
121+
udiff: string
122+
}
123+
124+
describe('addOnly', function () {
125+
const addOnlyCases: Case[] = [
126+
{
127+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
128+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
129+
@@ -6,7 +6,11 @@
130+
131+
// write a function to subtract 2 numbers
132+
public static int subtract(int a, int b) {
133+
return a - b;
134+
}
135+
-
136+
+
137+
+ // write a function to multiply 2 numbers
138+
+ public static int multiply(int a, int b) {
139+
+ return a * b;
140+
+ }
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
146+
@@ -6,7 +6,11 @@
147+
148+
// write a function to subtract 2 numbers
149+
public static int subtract(int a, int b) {
150+
return a - b;
151+
}
152+
-
153+
+
154+
+
155+
+ // write a function to multiply 2 numbers
156+
+ public static int multiply(int a, int b) {
157+
+ return a * b;
158+
+ }
159+
}`,
160+
},
161+
{
162+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
163+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
164+
@@ -6,7 +6,11 @@
165+
166+
// write a function to subtract 2 numbers
167+
public static int subtract(int a, int b) {
168+
return a - b;
169+
}
170+
-
171+
+
172+
+ // write a function to multiply 2 numbers
173+
+ public static int multiply(int a, int b) {
174+
+ return a * b;
175+
+ }
176+
}`,
177+
},
178+
{
179+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
180+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/MathUtil.java
181+
@@ -1,9 +1,10 @@
182+
public class MathUtil {
183+
// write a function to add 2 numbers
184+
public static int add(int a, int b) {
185+
186+
+ return a + b;
187+
}
188+
189+
// write a function to subtract 2 numbers
190+
public static int subtract(int a, int b) {
191+
return a - b;`,
192+
},
193+
{
194+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
195+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
196+
@@ -3,7 +3,9 @@
197+
public static int add(int a, int b) {
198+
return a + b;
199+
}
200+
201+
// write a function to subtract 2 numbers
202+
-
203+
+ public static int subtract(int a, int b) {
204+
+ return a - b;
205+
+ }
206+
}`,
207+
},
208+
{
209+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
210+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator-2/src/main/hello/MathUtil.java
211+
@@ -4,8 +4,8 @@
212+
return a + b;
213+
}
214+
215+
// write a function to subtract 2 numbers
216+
public static int subtract(int a, int b) {
217+
- return
218+
+ return a - b;
219+
}
220+
}`,
221+
},
222+
{
223+
udiff: `--- file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/LRUCache.java
224+
+++ file:///Volumes/workplace/ide/sample_projects/Calculator/src/main/hello/LRUCache.java
225+
@@ -7,7 +7,11 @@
226+
private Map<Integer, Node> map;
227+
private DoubleLinkedList list;
228+
private int capacity;
229+
230+
// get
231+
- public LruCache
232+
+ public LruCache(int capacity) {
233+
+ this.capacity = capacity;
234+
+ map = new HashMap<>();
235+
+ list = new DoubleLinkedList();
236+
+ }
237+
}`,
238+
},
239+
]
240+
241+
for (let i = 0; i < addOnlyCases.length; i++) {
242+
it(`case ${i}`, function () {
243+
const actual = categorizeUnifieddiff(addOnlyCases[i].udiff)
244+
assert.strictEqual(actual, 'addOnly')
245+
})
246+
}
247+
})
248+
249+
describe('edit', function () {
250+
const cases: Case[] = [
251+
{
252+
udiff: `--- a/src/main/hello/MathUtil.java
253+
+++ b/src/main/hello/MathUtil.java
254+
@@ -1,11 +1,11 @@
255+
public class MathUtil {
256+
// write a function to add 2 numbers
257+
- public static int add(int a, int b) {
258+
+ public static double add(double a, double b) {
259+
return a + b;
260+
}
261+
262+
// write a function to subtract 2 numbers
263+
public static int subtract(int a, int b) {
264+
public static double subtract(double a, double b) {
265+
return a - b;
266+
}
267+
}`,
268+
},
269+
{
270+
udiff: `--- a/server/aws-lsp-codewhisperer/src/shared/codeWhispererService.ts
271+
+++ b/server/aws-lsp-codewhisperer/src/shared/codeWhispererService.ts
272+
@@ -502,11 +502,7 @@ export class CodeWhispererServiceToken extends CodeWhispererServiceBase {
273+
: undefined
274+
}
275+
276+
- private withProfileArn<T extends object>(request: T): T {
277+
- if (!this.profileArn) return request
278+
-
279+
- return { ...request, profileArn: this.profileArn }
280+
- }
281+
+ // ddddddddddddddddd
282+
283+
async generateSuggestions(request: BaseGenerateSuggestionsRequest): Promise<GenerateSuggestionsResponse> {
284+
// Cast is now safe because GenerateTokenSuggestionsRequest extends GenerateCompletionsRequest`,
285+
},
286+
{
287+
udiff: `--- file:///Users/atona/workplace/NEP/language-servers/server/aws-lsp-codewhisperer/src/language-server/inline-completion/utils/textDocumentUtils.ts
288+
+++ file:///Users/atona/workplace/NEP/language-servers/server/aws-lsp-codewhisperer/src/language-server/inline-completion/utils/textDocumentUtils.ts
289+
@@ -15,11 +15,11 @@
290+
return ''
291+
}
292+
}
293+
294+
export const getTextDocument = async (uri: string, workspace: any, logging: any): Promise<TextDocument | undefined> => {
295+
- let
296+
+ if (!textDocument) {
297+
if (!textDocument) {
298+
try {
299+
const content = await workspace.fs.readFile(URI.parse(uri).fsPath)
300+
const languageId = getLanguageIdFromUri(uri)
301+
textDocument = TextDocument.create(uri, languageId, 0, content)`,
302+
},
303+
]
304+
305+
for (let i = 0; i < cases.length; i++) {
306+
it(`case ${i}`, function () {
307+
const actual = categorizeUnifieddiff(cases[i].udiff)
308+
assert.strictEqual(actual, 'edit')
309+
})
310+
}
311+
})
312+
})
3313

4314
describe('diffUtils', () => {
5315
describe('getAddedAndDeletedLines', () => {

0 commit comments

Comments
 (0)