@@ -352,11 +352,9 @@ TODO:
352352export 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
0 commit comments