Skip to content

Commit 34b59f4

Browse files
committed
feat(amazonq): add button to view diff
1 parent 1e462f9 commit 34b59f4

File tree

22 files changed

+347
-47
lines changed

22 files changed

+347
-47
lines changed

package-lock.json

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"generateNonCodeFiles": "npm run generateNonCodeFiles -w packages/ --if-present"
4040
},
4141
"devDependencies": {
42-
"@aws-toolkits/telemetry": "^1.0.258",
42+
"@aws-toolkits/telemetry": "^1.0.259",
4343
"@playwright/browser-chromium": "^1.43.1",
4444
"@types/vscode": "^1.68.0",
4545
"@types/vscode-webview": "^1.57.1",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Feature",
3+
"description": "Add View Diff button to allow users to open native diff view in the editor."
4+
}

packages/amazonq/src/extension.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import {
2020
import { makeEndpointsProvider, registerGenericCommands } from 'aws-core-vscode'
2121
import { CommonAuthWebview } from 'aws-core-vscode/login'
2222
import {
23+
amazonQDiffScheme,
2324
DefaultAWSClientBuilder,
2425
DefaultAwsContext,
2526
ExtContext,
2627
RegionProvider,
2728
Settings,
29+
VirtualFileSystem,
30+
VirtualMemoryFile,
2831
activateLogger,
2932
activateTelemetry,
3033
env,
@@ -133,6 +136,14 @@ export async function activateAmazonQCommon(context: vscode.ExtensionContext, is
133136
// Handle Amazon Q Extension un-installation.
134137
setupUninstallHandler(VSCODE_EXTENSION_ID.amazonq, context)
135138

139+
const vfs = new VirtualFileSystem()
140+
141+
// Register an empty file that's used when a to open a diff
142+
vfs.registerProvider(
143+
vscode.Uri.from({ scheme: amazonQDiffScheme, path: 'empty' }),
144+
new VirtualMemoryFile(new Uint8Array())
145+
)
146+
136147
// Hide the Amazon Q tree in toolkit explorer
137148
await setContext('aws.toolkit.amazonq.dismissed', true)
138149

packages/core/src/amazonq/commons/controllers/contentController.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
*/
55

66
import * as vscode from 'vscode'
7-
import { Position, TextEditor, window } from 'vscode'
7+
import fs from 'fs-extra'
8+
import path from 'path'
9+
import { commands, Position, TextEditor, Uri, window, workspace } from 'vscode'
810
import { getLogger } from '../../../shared/logger'
11+
import { amazonQDiffScheme, getNonexistentFilename, tempDirPath } from '../../../shared'
912

1013
export class EditorContentController {
1114
/* *
@@ -52,4 +55,78 @@ export class EditorContentController {
5255
)
5356
}
5457
}
58+
59+
/**
60+
* Displays a diff view comparing proposed changes with the existing file.
61+
*
62+
* How is diff generated:
63+
* 1. Creates a temporary file as a clone of the original file.
64+
* 2. Applies the proposed changes to the temporary file within the selected range.
65+
* 3. Opens a diff view comparing original file to the temporary file.
66+
*
67+
* This approach ensures that the diff view only shows the changes proposed by Amazon Q,
68+
* isolating them from any other modifications in the original file.
69+
*
70+
* @param message the message from Amazon Q chat
71+
*/
72+
public async viewDiff(message: any) {
73+
const { filePath } = message?.context?.activeFileContext || {}
74+
const selection = message?.context?.focusAreaContext?.selectionInsideExtendedCodeBlock as vscode.Selection
75+
76+
if (filePath && message?.code?.trim().length > 0 && selection) {
77+
const id = Date.now()
78+
79+
const originalFileUri = Uri.file(filePath)
80+
const fileNameWithExtension = path.basename(originalFileUri.path)
81+
82+
const fileName = path.parse(fileNameWithExtension).name
83+
const fileExtension = path.extname(fileNameWithExtension)
84+
85+
// Create a new file in the temp directory
86+
const tempFile = await getNonexistentFilename(tempDirPath, `${fileName}_proposed-${id}`, fileExtension, 99)
87+
const tempFilePath = path.join(tempDirPath, tempFile)
88+
89+
// Create a new URI for the temp file
90+
const diffScheme = amazonQDiffScheme
91+
const tempFileUri = Uri.parse(`${diffScheme}:${tempFilePath}`)
92+
93+
// Write the initial code to the temp file
94+
fs.writeFileSync(tempFilePath, fs.readFileSync(originalFileUri.fsPath, 'utf-8'))
95+
const doc = await workspace.openTextDocument(tempFileUri.path)
96+
97+
// Apply the edit to the temp file
98+
const edit = new vscode.WorkspaceEdit()
99+
edit.replace(doc.uri, selection, message.code)
100+
101+
const successfulEdit = await workspace.applyEdit(edit)
102+
if (successfulEdit) {
103+
await doc.save()
104+
fs.writeFileSync(tempFilePath, doc.getText())
105+
}
106+
class ContentProvider {
107+
provideTextDocumentContent(_uri: Uri) {
108+
return fs.readFileSync(tempFilePath, 'utf-8')
109+
}
110+
}
111+
const contentProvider = new ContentProvider()
112+
const disposable = workspace.registerTextDocumentContentProvider(diffScheme, contentProvider)
113+
114+
await commands.executeCommand(
115+
'vscode.diff',
116+
originalFileUri,
117+
tempFileUri,
118+
`${fileName}${fileExtension} (Generated by Amazon Q)`
119+
)
120+
121+
vscode.window.onDidChangeVisibleTextEditors(() => {
122+
if (
123+
!vscode.window.visibleTextEditors.some(
124+
(editor) => editor.document.uri.toString() === tempFileUri.toString()
125+
)
126+
) {
127+
disposable.dispose()
128+
}
129+
})
130+
}
131+
}
55132
}

packages/core/src/amazonq/webview/ui/apps/cwChatConnector.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface ChatPayload {
1919
export interface ConnectorProps {
2020
sendMessageToExtension: (message: ExtensionMessage) => void
2121
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
22-
onChatAnswerReceived?: (tabID: string, message: ChatItem) => void
22+
onChatAnswerReceived?: (tabID: string, message: ChatItem, messageData: any) => void
2323
onCWCContextCommandMessage: (message: ChatItem, command?: string) => string | undefined
2424
onError: (tabID: string, message: string, title: string) => void
2525
onWarning: (tabID: string, message: string, title: string) => void
@@ -282,7 +282,7 @@ export class Connector {
282282
content: messageData.relatedSuggestions,
283283
}
284284
}
285-
this.onChatAnswerReceived(messageData.tabID, answer)
285+
this.onChatAnswerReceived(messageData.tabID, answer, messageData)
286286

287287
// Exit the function if we received an answer from AI
288288
if (
@@ -310,7 +310,7 @@ export class Connector {
310310
: undefined,
311311
traceId: messageData.traceId,
312312
}
313-
this.onChatAnswerReceived(messageData.tabID, answer)
313+
this.onChatAnswerReceived(messageData.tabID, answer, messageData)
314314

315315
return
316316
}
@@ -321,13 +321,17 @@ export class Connector {
321321
return
322322
}
323323

324-
this.onChatAnswerReceived(messageData.tabID, {
325-
type: ChatItemType.ANSWER,
326-
messageId: messageData.triggerID,
327-
body: messageData.message,
328-
followUp: this.followUpGenerator.generateAuthFollowUps('cwc', messageData.authType),
329-
canBeVoted: false,
330-
})
324+
this.onChatAnswerReceived(
325+
messageData.tabID,
326+
{
327+
type: ChatItemType.ANSWER,
328+
messageId: messageData.triggerID,
329+
body: messageData.message,
330+
followUp: this.followUpGenerator.generateAuthFollowUps('cwc', messageData.authType),
331+
canBeVoted: false,
332+
},
333+
messageData
334+
)
331335

332336
return
333337
}

packages/core/src/amazonq/webview/ui/apps/featureDevChatConnector.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface ConnectorProps {
1919
sendMessageToExtension: (message: ExtensionMessage) => void
2020
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
2121
onAsyncEventProgress: (tabID: string, inProgress: boolean, message: string) => void
22-
onChatAnswerReceived?: (tabID: string, message: ChatItem) => void
22+
onChatAnswerReceived?: (tabID: string, message: ChatItem, messageData: any) => void
2323
sendFeedback?: (tabId: string, feedbackPayload: FeedbackPayload) => void | undefined
2424
onError: (tabID: string, message: string, title: string) => void
2525
onWarning: (tabID: string, message: string, title: string) => void
@@ -153,7 +153,7 @@ export class Connector {
153153
}
154154
: undefined,
155155
}
156-
this.onChatAnswerReceived(messageData.tabID, answer)
156+
this.onChatAnswerReceived(messageData.tabID, answer, messageData)
157157
}
158158
}
159159

@@ -176,7 +176,7 @@ export class Connector {
176176
},
177177
body: '',
178178
}
179-
this.onChatAnswerReceived(messageData.tabID, answer)
179+
this.onChatAnswerReceived(messageData.tabID, answer, messageData)
180180
}
181181
}
182182

@@ -185,19 +185,27 @@ export class Connector {
185185
return
186186
}
187187

188-
this.onChatAnswerReceived(messageData.tabID, {
189-
type: ChatItemType.ANSWER,
190-
body: messageData.message,
191-
followUp: undefined,
192-
canBeVoted: false,
193-
})
194-
195-
this.onChatAnswerReceived(messageData.tabID, {
196-
type: ChatItemType.SYSTEM_PROMPT,
197-
body: undefined,
198-
followUp: this.followUpGenerator.generateAuthFollowUps('featuredev', messageData.authType),
199-
canBeVoted: false,
200-
})
188+
this.onChatAnswerReceived(
189+
messageData.tabID,
190+
{
191+
type: ChatItemType.ANSWER,
192+
body: messageData.message,
193+
followUp: undefined,
194+
canBeVoted: false,
195+
},
196+
messageData
197+
)
198+
199+
this.onChatAnswerReceived(
200+
messageData.tabID,
201+
{
202+
type: ChatItemType.SYSTEM_PROMPT,
203+
body: undefined,
204+
followUp: this.followUpGenerator.generateAuthFollowUps('featuredev', messageData.authType),
205+
canBeVoted: false,
206+
},
207+
messageData
208+
)
201209

202210
return
203211
}

packages/core/src/amazonq/webview/ui/apps/gumbyChatConnector.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export interface ConnectorProps {
1717
sendMessageToExtension: (message: ExtensionMessage) => void
1818
onMessageReceived?: (tabID: string, messageData: any, needToShowAPIDocsTab: boolean) => void
1919
onAsyncEventProgress: (tabID: string, inProgress: boolean, message: string, messageId: string) => void
20-
onChatAnswerReceived?: (tabID: string, message: ChatItem) => void
20+
onChatAnswerReceived?: (tabID: string, message: ChatItem, messageData: any) => void
2121
onChatAnswerUpdated?: (tabID: string, message: ChatItem) => void
2222
onQuickHandlerCommand: (tabID: string, command: string, eventId?: string) => void
2323
onError: (tabID: string, message: string, title: string) => void
@@ -90,7 +90,7 @@ export class Connector {
9090
canBeVoted: false,
9191
}
9292

93-
this.onChatAnswerReceived(tabID, answer)
93+
this.onChatAnswerReceived(tabID, answer, messageData)
9494

9595
return
9696
}
@@ -114,7 +114,7 @@ export class Connector {
114114
return
115115
}
116116

117-
this.onChatAnswerReceived(messageData.tabID, answer)
117+
this.onChatAnswerReceived(messageData.tabID, answer, messageData)
118118
}
119119
}
120120

@@ -143,10 +143,14 @@ export class Connector {
143143
return
144144
}
145145

146-
this.onChatAnswerReceived(messageData.tabID, {
147-
type: ChatItemType.SYSTEM_PROMPT,
148-
body: messageData.message,
149-
})
146+
this.onChatAnswerReceived(
147+
messageData.tabID,
148+
{
149+
type: ChatItemType.SYSTEM_PROMPT,
150+
body: messageData.message,
151+
},
152+
messageData
153+
)
150154
}
151155

152156
onCustomFormAction(

packages/core/src/amazonq/webview/ui/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MessageCommand =
1616
| 'open-diff'
1717
| 'code_was_copied_to_clipboard'
1818
| 'insert_code_at_cursor_position'
19+
| 'view_diff'
1920
| 'stop-response'
2021
| 'trigger-tabID-received'
2122
| 'clear'

0 commit comments

Comments
 (0)