|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | import * as vscode from 'vscode' |
| 7 | +import fs from 'fs-extra' |
| 8 | +import path from 'path' |
7 | 9 | import { Position, TextEditor, window } from 'vscode' |
8 | 10 | import { getLogger } from '../../../shared/logger' |
| 11 | +import { AMAZON_Q_DIFF_SCHEME, amazonQTabSuffix } from '../../../shared/constants' |
| 12 | +import { disposeOnEditorClose } from '../../../shared/utilities/editorUtilities' |
| 13 | +import { |
| 14 | + applyChanges, |
| 15 | + createTempFileForDiff, |
| 16 | + getSelectionFromRange, |
| 17 | +} from '../../../shared/utilities/textDocumentUtilities' |
| 18 | +import { extractFileAndCodeSelectionFromMessage, getIndentedCode } from '../../../shared' |
9 | 19 |
|
| 20 | +class ContentProvider implements vscode.TextDocumentContentProvider { |
| 21 | + constructor(private uri: vscode.Uri) {} |
| 22 | + |
| 23 | + provideTextDocumentContent(_uri: vscode.Uri) { |
| 24 | + return fs.readFileSync(this.uri.fsPath, 'utf-8') |
| 25 | + } |
| 26 | +} |
10 | 27 | export class EditorContentController { |
11 | 28 | /* * |
12 | 29 | * Insert the Amazon Q chat written code to the cursor position |
@@ -52,4 +69,97 @@ export class EditorContentController { |
52 | 69 | ) |
53 | 70 | } |
54 | 71 | } |
| 72 | + |
| 73 | + /** |
| 74 | + * Accepts and applies a diff to a file, then closes the associated diff view tab. |
| 75 | + * |
| 76 | + * @param {any} message - The message containing diff information. |
| 77 | + * @returns {Promise<void>} A promise that resolves when the diff is applied and the tab is closed. |
| 78 | + * |
| 79 | + * @description |
| 80 | + * This method performs the following steps: |
| 81 | + * 1. Extracts file path and selection from the message. |
| 82 | + * 2. If valid file path, non-empty code, and selection are present: |
| 83 | + * a. Opens the document. |
| 84 | + * b. Gets the indented code to update. |
| 85 | + * c. Applies the changes to the document. |
| 86 | + * d. Attempts to close the diff view tab for the file. |
| 87 | + * |
| 88 | + * @throws {Error} If there's an issue opening the document or applying changes. |
| 89 | + */ |
| 90 | + public async acceptDiff(message: any) { |
| 91 | + const { filePath, selection } = extractFileAndCodeSelectionFromMessage(message) |
| 92 | + |
| 93 | + if (filePath && message?.code?.trim().length > 0 && selection) { |
| 94 | + try { |
| 95 | + const doc = await vscode.workspace.openTextDocument(filePath) |
| 96 | + |
| 97 | + const code = getIndentedCode(message, doc, selection) |
| 98 | + const range = getSelectionFromRange(doc, selection) |
| 99 | + try { |
| 100 | + await applyChanges(doc, range, code) |
| 101 | + |
| 102 | + // Sets the editor selection from the start of the given range, extending it by the number of lines in the code till the end of the last line |
| 103 | + const editor = await vscode.window.showTextDocument(doc) |
| 104 | + editor.selection = new vscode.Selection( |
| 105 | + range.start, |
| 106 | + new Position(range.start.line + code.split('\n').length, Number.MAX_SAFE_INTEGER) |
| 107 | + ) |
| 108 | + } catch (error) { |
| 109 | + getLogger().error(`Diff: Unable to apply changes in the document:`, (error as Error).message) |
| 110 | + } |
| 111 | + |
| 112 | + // If vscode.diff is open for the filePath then close it. |
| 113 | + vscode.window.tabGroups.all.flatMap(({ tabs }) => |
| 114 | + tabs.map((tab) => { |
| 115 | + if (tab.label === `${path.basename(filePath)} ${amazonQTabSuffix}`) { |
| 116 | + const tabClosed = vscode.window.tabGroups.close(tab) |
| 117 | + if (!tabClosed) { |
| 118 | + getLogger().error('Diff: Unable to close the diff view tab for %s', tab.label) |
| 119 | + } |
| 120 | + } |
| 121 | + }) |
| 122 | + ) |
| 123 | + } catch (error) { |
| 124 | + getLogger().error(`Diff: Unable to open the document:`, (error as Error).message) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Displays a diff view comparing proposed changes with the existing file. |
| 131 | + * |
| 132 | + * How is diff generated: |
| 133 | + * 1. Creates a temporary file as a clone of the original file. |
| 134 | + * 2. Applies the proposed changes to the temporary file within the selected range. |
| 135 | + * 3. Opens a diff view comparing original file to the temporary file. |
| 136 | + * |
| 137 | + * This approach ensures that the diff view only shows the changes proposed by Amazon Q, |
| 138 | + * isolating them from any other modifications in the original file. |
| 139 | + * |
| 140 | + * @param message the message from Amazon Q chat |
| 141 | + */ |
| 142 | + public async viewDiff(message: any) { |
| 143 | + const { filePath, selection } = extractFileAndCodeSelectionFromMessage(message) |
| 144 | + |
| 145 | + if (filePath && message?.code?.trim().length > 0 && selection) { |
| 146 | + const originalFileUri = vscode.Uri.file(filePath) |
| 147 | + const uri = await createTempFileForDiff(originalFileUri, message, selection) |
| 148 | + |
| 149 | + // Register content provider and show diff |
| 150 | + const contentProvider = new ContentProvider(uri) |
| 151 | + const disposable = vscode.workspace.registerTextDocumentContentProvider( |
| 152 | + AMAZON_Q_DIFF_SCHEME, |
| 153 | + contentProvider |
| 154 | + ) |
| 155 | + await vscode.commands.executeCommand( |
| 156 | + 'vscode.diff', |
| 157 | + originalFileUri, |
| 158 | + uri, |
| 159 | + `${path.basename(filePath)} ${amazonQTabSuffix}` |
| 160 | + ) |
| 161 | + |
| 162 | + disposeOnEditorClose(uri, disposable) |
| 163 | + } |
| 164 | + } |
55 | 165 | } |
0 commit comments