Skip to content

Commit 2f32ab3

Browse files
committed
POC: On Demand Fix Working POC
1 parent af8f3f1 commit 2f32ab3

File tree

4 files changed

+55
-78
lines changed

4 files changed

+55
-78
lines changed

packages/core/src/codewhisperer/commands/basicCommands.ts

Lines changed: 34 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,9 @@ TODO:
352352
export const generateSecurityFix = Commands.declare(
353353
'aws.amazonq.generateSecurityFix',
354354
() => async (issue: CodeScanIssue, filePath: string, source: Component) => {
355-
/* Working */
356355
return vscode.window.withProgress(
357356
{
358357
location: vscode.ProgressLocation.Notification,
359-
// title: CodeWhispererConstants.runningSecurityScan,
360358
title: 'Generating security fix...',
361359
cancellable: false,
362360
},
@@ -366,107 +364,65 @@ export const generateSecurityFix = Commands.declare(
366364
try {
367365
const originalDoc = await vscode.workspace.openTextDocument(uri)
368366
const originalCode = originalDoc.getText()
367+
const lines = originalCode.split('\n')
369368

370-
const originalTempUri = vscode.Uri.parse(`${uri.fsPath}`)
371-
const changedTempUri = vscode.Uri.parse(`untitled:changed_${uri.fsPath}`)
372-
373-
await vscode.workspace.openTextDocument(changedTempUri).then(async (doc) => {
374-
const edit = new vscode.WorkspaceEdit()
375-
edit.insert(changedTempUri, new vscode.Position(0, 0), originalCode.toUpperCase())
376-
const success = await vscode.workspace.applyEdit(edit)
377-
})
378-
await vscode.commands.executeCommand(
379-
'vscode.diff',
380-
originalTempUri,
381-
changedTempUri,
382-
`Diff: ${uri.fsPath}`
383-
)
384-
} catch (error) {
385-
vscode.window.showErrorMessage(`Failed to show diff: ${error.message}`)
386-
}
387-
}
388-
)
389-
// void vscode.window.showInformationMessage(`Generating security fix...`)
390-
391-
/*
392-
return vscode.window.withProgress(
393-
{
394-
location: vscode.ProgressLocation.Notification,
395-
title: 'Generating security fix...',
396-
cancellable: false,
397-
},
398-
async () => {
399-
const uri = vscode.Uri.file(filePath)
369+
// Convert only the lines from startLine to endLine to uppercase
370+
for (let i = issue.startLine - 1; i < issue.endLine; i++) {
371+
if (i >= 0 && i < lines.length) {
372+
lines[i] = lines[i].toUpperCase()
373+
}
374+
}
400375

401-
try {
402-
const originalDoc = await vscode.workspace.openTextDocument(uri)
403-
const originalCode = originalDoc.getText()
376+
const modifiedCode = lines.join('\n')
404377

405378
const originalTempUri = vscode.Uri.parse(`${uri.fsPath}`)
406379
const changedTempUri = vscode.Uri.parse(`untitled:changed_${uri.fsPath}`)
407380

408381
await vscode.workspace.openTextDocument(changedTempUri).then(async (doc) => {
409382
const edit = new vscode.WorkspaceEdit()
410-
edit.insert(changedTempUri, new vscode.Position(0, 0), originalCode.toUpperCase())
411-
const success = await vscode.workspace.applyEdit(edit)
383+
edit.insert(changedTempUri, new vscode.Position(0, 0), modifiedCode)
384+
await vscode.workspace.applyEdit(edit)
412385
})
413386

414-
const diffResult = await vscode.commands.executeCommand(
387+
await vscode.commands.executeCommand(
415388
'vscode.diff',
416389
originalTempUri,
417390
changedTempUri,
418391
`Diff: ${uri.fsPath}`
419392
)
420393

421-
// Prompt user to accept or ignore the changes
422-
const acceptChanges = 'Accept Changes'
423-
const ignoreChanges = 'Ignore Changes'
424-
425-
const userChoice = await vscode.window.showInformationMessage(
394+
// Show accept/reject buttons
395+
const choice = await vscode.window.showInformationMessage(
426396
'Do you want to accept the changes?',
427-
{ modal: true },
428-
acceptChanges,
429-
ignoreChanges
397+
{ modal: false },
398+
'Accept',
399+
'Reject'
430400
)
431401

432-
if (userChoice === acceptChanges) {
433-
// Apply changes to the original document
402+
if (choice === 'Accept') {
403+
// Save changes to original file
434404
const edit = new vscode.WorkspaceEdit()
435-
edit.replace(
436-
uri,
437-
new vscode.Range(
438-
new vscode.Position(0, 0),
439-
originalDoc.lineAt(originalDoc.lineCount - 1).range.end
440-
),
441-
originalCode.toUpperCase()
442-
)
443-
const success = await vscode.workspace.applyEdit(edit)
444-
if (success) {
445-
await originalDoc.save()
446-
vscode.window.showInformationMessage('Changes applied successfully!')
447-
} else {
448-
vscode.window.showErrorMessage('Failed to apply changes.')
449-
}
450-
} else {
451-
vscode.window.showInformationMessage('Changes ignored.')
405+
edit.replace(uri, new vscode.Range(0, 0, originalDoc.lineCount, 0), modifiedCode)
406+
await vscode.workspace.applyEdit(edit)
407+
await originalDoc.save()
452408
}
409+
// Close the diff view
453410
await vscode.commands.executeCommand('workbench.action.closeActiveEditor')
454-
const changedDoc = await vscode.workspace.openTextDocument(changedTempUri)
455-
const discardEdit = new vscode.WorkspaceEdit()
456-
discardEdit.replace(
457-
changedTempUri,
458-
new vscode.Range(
459-
new vscode.Position(0, 0),
460-
changedDoc.lineAt(changedDoc.lineCount - 1).range.end
461-
),
462-
''
463-
)
464-
await vscode.workspace.applyEdit(discardEdit)
411+
// Delete the temporary file
412+
try {
413+
await vscode.workspace.fs.delete(changedTempUri)
414+
} catch (error) {
415+
// console.error(`Failed to delete temporary file: ${error}`)
416+
}
417+
// Close and delete the diff view
418+
// await vscode.commands.executeCommand('workbench.action.closeAllEditors')
465419
} catch (error) {
466-
vscode.window.showErrorMessage(`Failed to show diff: ${error.message}`)
420+
void vscode.window.showErrorMessage(`Failed to show diff: ${error}`)
467421
}
468422
}
469-
)*/
423+
)
424+
425+
// void vscode.window.showInformationMessage(`Generating security fix...`)
470426
}
471427
)
472428

packages/core/src/codewhisperer/service/securityIssueCodeActionProvider.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ export class SecurityIssueCodeActionProvider extends SecurityIssueProvider imple
4444
arguments: args,
4545
}
4646
codeActions.push(fixIssue)
47+
} else {
48+
const generatefixIssue = new vscode.CodeAction(
49+
`Amazon Q: Code Fix "${issue.title}"`,
50+
vscode.CodeActionKind.QuickFix
51+
)
52+
const args: [CodeScanIssue, string, Component] = [issue, group.filePath, 'quickfix']
53+
generatefixIssue.command = {
54+
title: 'Generate Fix with Amazon Q',
55+
command: 'aws.amazonq.generateSecurityFix',
56+
arguments: args,
57+
}
58+
codeActions.push(generatefixIssue)
4759
}
4860
const openIssue = new vscode.CodeAction(
4961
`Amazon Q: View details for "${issue.title}"`,

packages/core/src/codewhisperer/views/securityIssue/securityIssueWebview.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export class SecurityIssueWebview extends VueWebview {
3636
void vscode.commands.executeCommand('aws.amazonq.applySecurityFix', ...args)
3737
}
3838

39+
public generateSecurityFix() {
40+
const args: [CodeScanIssue | undefined, string | undefined, Component] = [this.issue, this.filePath, 'webview']
41+
void vscode.commands.executeCommand('aws.amazonq.generateSecurityFix', ...args)
42+
}
43+
3944
public explainWithQ() {
4045
const args = [this.issue]
4146
void this.navigateToFile()?.then(() => {

packages/core/src/codewhisperer/views/securityIssue/vue/root.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<div class="container button-container" style="justify-content: space-between">
66
<h1>{{ title }} <img class="severity" :src="severityImage" :alt="severity" /></h1>
77
<input v-if="isFixAvailable" class="mt-4 ml-16" type="submit" @click="applyFix" value="Fix" />
8+
<input v-else class="mt-4 ml-16" type="submit" @click="generateSecurityFix" value="Code Fix" />
89
</div>
910

1011
<div class="mt-16">
@@ -166,6 +167,9 @@ export default defineComponent({
166167
applyFix() {
167168
client.applyFix()
168169
},
170+
generateSecurityFix() {
171+
client.generateSecurityFix()
172+
},
169173
explainWithQ() {
170174
client.explainWithQ()
171175
},

0 commit comments

Comments
 (0)