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