Skip to content

Commit 4dac86b

Browse files
committed
FolowUp PR to reuse file-click function to who the diff view
1 parent 8392a4c commit 4dac86b

File tree

7 files changed

+72
-90
lines changed

7 files changed

+72
-90
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export class Connector extends BaseConnector {
305305

306306
onFileClick = (tabID: string, filePath: string, messageId?: string) => {
307307
this.sendMessageToExtension({
308-
command: messageId === '' ? 'file-click' : 'open-diff',
308+
command: 'file-click',
309309
tabID,
310310
messageId,
311311
filePath,

packages/core/src/codewhispererChat/app.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
AcceptDiff,
2929
QuickCommandGroupActionClick,
3030
FileClick,
31-
OpenDiff,
3231
} from './controllers/chat/model'
3332
import { EditorContextCommand, registerCommands } from './commands/registerCommands'
3433
import { ContextSelectedMessage, CustomFormActionMessage } from './view/connector/connector'
@@ -42,7 +41,6 @@ export function init(appContext: AmazonQAppInitContext) {
4241
processInsertCodeAtCursorPosition: new EventEmitter<InsertCodeAtCursorPosition>(),
4342
processAcceptDiff: new EventEmitter<AcceptDiff>(),
4443
processViewDiff: new EventEmitter<ViewDiff>(),
45-
processOpenDiff: new EventEmitter<OpenDiff>(),
4644
processCopyCodeToClipboard: new EventEmitter<CopyCodeToClipboard>(),
4745
processContextMenuCommand: new EventEmitter<EditorContextCommand>(),
4846
processTriggerTabIDReceived: new EventEmitter<TriggerTabIDReceived>(),
@@ -78,7 +76,6 @@ export function init(appContext: AmazonQAppInitContext) {
7876
),
7977
processAcceptDiff: new MessageListener<AcceptDiff>(cwChatControllerEventEmitters.processAcceptDiff),
8078
processViewDiff: new MessageListener<ViewDiff>(cwChatControllerEventEmitters.processViewDiff),
81-
processOpenDiff: new MessageListener<OpenDiff>(cwChatControllerEventEmitters.processOpenDiff),
8279
processCopyCodeToClipboard: new MessageListener<CopyCodeToClipboard>(
8380
cwChatControllerEventEmitters.processCopyCodeToClipboard
8481
),
@@ -140,7 +137,6 @@ export function init(appContext: AmazonQAppInitContext) {
140137
),
141138
processAcceptDiff: new MessagePublisher<AcceptDiff>(cwChatControllerEventEmitters.processAcceptDiff),
142139
processViewDiff: new MessagePublisher<ViewDiff>(cwChatControllerEventEmitters.processViewDiff),
143-
processOpenDiff: new MessagePublisher<OpenDiff>(cwChatControllerEventEmitters.processOpenDiff),
144140
processCopyCodeToClipboard: new MessagePublisher<CopyCodeToClipboard>(
145141
cwChatControllerEventEmitters.processCopyCodeToClipboard
146142
),

packages/core/src/codewhispererChat/clients/chat/v0/chat.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ export class ChatSession {
2121
* _readFiles = list of files read from the project to gather context before generating response.
2222
* _filePath = The path helps the system locate exactly where to make the necessary changes in the project structure
2323
* _tempFilePath = Used to show the code diff view in the editor including LLM changes.
24+
* _showDiffOnFileWrite = Controls whether to show diff view (true) or file context view (false) to the user
2425
*/
2526
private _readFiles: string[] = []
2627
private _filePath: string | undefined
2728
private _tempFilePath: string | undefined
2829
private _toolUse: ToolUse | undefined
30+
private _showDiffOnFileWrite: boolean = false
2931

3032
contexts: Map<string, { first: number; second: number }[]> = new Map()
3133
// TODO: doesn't handle the edge case when two files share the same relativePath string but from different root
@@ -56,7 +58,7 @@ export class ChatSession {
5658
public setSessionID(id?: string) {
5759
this.sessionId = id
5860
}
59-
public get listOfReadFiles(): string[] {
61+
public get readFiles(): string[] {
6062
return this._readFiles
6163
}
6264
public get filePath(): string | undefined {
@@ -65,13 +67,19 @@ export class ChatSession {
6567
public get tempFilePath(): string | undefined {
6668
return this._tempFilePath
6769
}
70+
public get showDiffOnFileWrite(): boolean {
71+
return this._showDiffOnFileWrite
72+
}
73+
public setShowDiffOnFileWrite(value: boolean) {
74+
this._showDiffOnFileWrite = value
75+
}
6876
public setFilePath(filePath: string | undefined) {
6977
this._filePath = filePath
7078
}
7179
public setTempFilePath(tempFilePath: string | undefined) {
7280
this._tempFilePath = tempFilePath
7381
}
74-
public pushToListOfReadFiles(filePath: string) {
82+
public addListOfReadFiles(filePath: string) {
7583
this._readFiles.push(filePath)
7684
}
7785
public clearListOfReadFiles() {

packages/core/src/codewhispererChat/controllers/chat/controller.ts

Lines changed: 59 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
DocumentReference,
3333
FileClick,
3434
RelevantTextDocumentAddition,
35-
OpenDiff,
3635
} from './model'
3736
import {
3837
AppToWebViewMessageDispatcher,
@@ -93,7 +92,6 @@ export interface ChatControllerMessagePublishers {
9392
readonly processInsertCodeAtCursorPosition: MessagePublisher<InsertCodeAtCursorPosition>
9493
readonly processAcceptDiff: MessagePublisher<AcceptDiff>
9594
readonly processViewDiff: MessagePublisher<ViewDiff>
96-
readonly processOpenDiff: MessagePublisher<OpenDiff>
9795
readonly processCopyCodeToClipboard: MessagePublisher<CopyCodeToClipboard>
9896
readonly processContextMenuCommand: MessagePublisher<EditorContextCommand>
9997
readonly processTriggerTabIDReceived: MessagePublisher<TriggerTabIDReceived>
@@ -119,7 +117,6 @@ export interface ChatControllerMessageListeners {
119117
readonly processInsertCodeAtCursorPosition: MessageListener<InsertCodeAtCursorPosition>
120118
readonly processAcceptDiff: MessageListener<AcceptDiff>
121119
readonly processViewDiff: MessageListener<ViewDiff>
122-
readonly processOpenDiff: MessageListener<OpenDiff>
123120
readonly processCopyCodeToClipboard: MessageListener<CopyCodeToClipboard>
124121
readonly processContextMenuCommand: MessageListener<EditorContextCommand>
125122
readonly processTriggerTabIDReceived: MessageListener<TriggerTabIDReceived>
@@ -218,10 +215,6 @@ export class ChatController {
218215
return this.processViewDiff(data)
219216
})
220217

221-
this.chatControllerMessageListeners.processOpenDiff.onMessage((data) => {
222-
return this.processOpenDiff(data)
223-
})
224-
225218
this.chatControllerMessageListeners.processCopyCodeToClipboard.onMessage((data) => {
226219
return this.processCopyCodeToClipboard(data)
227220
})
@@ -397,20 +390,6 @@ export class ChatController {
397390
})
398391
}
399392

400-
private async processOpenDiff(message: OpenDiff) {
401-
const session = this.sessionStorage.getSession(message.tabID)
402-
const filePath = session.filePath ?? message.filePath
403-
const fileExists = await fs.existsFile(filePath)
404-
// Check if fileExists=false, If yes, return instead of showing broken diff experience.
405-
if (!session.tempFilePath) {
406-
return
407-
}
408-
const leftUri = fileExists ? vscode.Uri.file(filePath) : vscode.Uri.from({ scheme: 'untitled' })
409-
const rightUri = vscode.Uri.file(session.tempFilePath ?? filePath)
410-
const fileName = path.basename(filePath)
411-
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, `${fileName} ${amazonQTabSuffix}`)
412-
}
413-
414393
private async processAcceptCodeDiff(message: CustomFormActionMessage) {
415394
const session = this.sessionStorage.getSession(message.tabID ?? '')
416395
const filePath = session.filePath ?? ''
@@ -643,55 +622,70 @@ export class ChatController {
643622
}
644623
private async processFileClickMessage(message: FileClick) {
645624
const session = this.sessionStorage.getSession(message.tabID)
646-
const lineRanges = session.contexts.get(message.filePath)
625+
// Check if user clicked on filePath in the contextList or filePath in the fileListTress and perform the functionality accordingly.
626+
if (session.showDiffOnFileWrite) {
627+
const filePath = session.filePath ?? message.filePath
628+
const fileExists = await fs.existsFile(filePath)
629+
// Check if fileExists=false, If yes, return instead of showing broken diff experience.
630+
if (!session.tempFilePath) {
631+
void vscode.window.showInformationMessage('Generated code changes have been reviewed and processed.')
632+
return
633+
}
634+
const leftUri = fileExists ? vscode.Uri.file(filePath) : vscode.Uri.from({ scheme: 'untitled' })
635+
const rightUri = vscode.Uri.file(session.tempFilePath ?? filePath)
636+
const fileName = path.basename(filePath)
637+
await vscode.commands.executeCommand('vscode.diff', leftUri, rightUri, `${fileName} ${amazonQTabSuffix}`)
638+
} else {
639+
const lineRanges = session.contexts.get(message.filePath)
647640

648-
if (!lineRanges) {
649-
return
650-
}
641+
if (!lineRanges) {
642+
return
643+
}
651644

652-
// Check if clicked file is in a different workspace root
653-
const projectRoot =
654-
session.relativePathToWorkspaceRoot.get(message.filePath) || workspace.workspaceFolders?.[0]?.uri.fsPath
655-
if (!projectRoot) {
656-
return
657-
}
658-
let absoluteFilePath = path.join(projectRoot, message.filePath)
645+
// Check if clicked file is in a different workspace root
646+
const projectRoot =
647+
session.relativePathToWorkspaceRoot.get(message.filePath) || workspace.workspaceFolders?.[0]?.uri.fsPath
648+
if (!projectRoot) {
649+
return
650+
}
651+
let absoluteFilePath = path.join(projectRoot, message.filePath)
659652

660-
// Handle clicking on a user prompt outside the workspace
661-
if (message.filePath.endsWith(promptFileExtension)) {
662-
try {
663-
await vscode.workspace.fs.stat(vscode.Uri.file(absoluteFilePath))
664-
} catch {
665-
absoluteFilePath = path.join(getUserPromptsDirectory(), message.filePath)
653+
// Handle clicking on a user prompt outside the workspace
654+
if (message.filePath.endsWith(promptFileExtension)) {
655+
try {
656+
await vscode.workspace.fs.stat(vscode.Uri.file(absoluteFilePath))
657+
} catch {
658+
absoluteFilePath = path.join(getUserPromptsDirectory(), message.filePath)
659+
}
666660
}
667-
}
668661

669-
try {
670-
// Open the file in VSCode
671-
const document = await workspace.openTextDocument(absoluteFilePath)
672-
const editor = await window.showTextDocument(document, ViewColumn.Active)
673-
674-
// Create multiple selections based on line ranges
675-
const selections: Selection[] = lineRanges
676-
.filter(({ first, second }) => first !== -1 && second !== -1)
677-
.map(({ first, second }) => {
678-
const startPosition = new Position(first - 1, 0) // Convert 1-based to 0-based
679-
const endPosition = new Position(second - 1, document.lineAt(second - 1).range.end.character)
680-
return new Selection(
681-
startPosition.line,
682-
startPosition.character,
683-
endPosition.line,
684-
endPosition.character
685-
)
686-
})
662+
try {
663+
// Open the file in VSCode
664+
const document = await workspace.openTextDocument(absoluteFilePath)
665+
const editor = await window.showTextDocument(document, ViewColumn.Active)
666+
667+
// Create multiple selections based on line ranges
668+
const selections: Selection[] = lineRanges
669+
.filter(({ first, second }) => first !== -1 && second !== -1)
670+
.map(({ first, second }) => {
671+
const startPosition = new Position(first - 1, 0) // Convert 1-based to 0-based
672+
const endPosition = new Position(second - 1, document.lineAt(second - 1).range.end.character)
673+
return new Selection(
674+
startPosition.line,
675+
startPosition.character,
676+
endPosition.line,
677+
endPosition.character
678+
)
679+
})
687680

688-
// Apply multiple selections to the editor
689-
if (selections.length > 0) {
690-
editor.selection = selections[0] // Set the first selection as active
691-
editor.selections = selections // Apply multiple selections
692-
editor.revealRange(selections[0], vscode.TextEditorRevealType.InCenter)
693-
}
694-
} catch (error) {}
681+
// Apply multiple selections to the editor
682+
if (selections.length > 0) {
683+
editor.selection = selections[0] // Set the first selection as active
684+
editor.selections = selections // Apply multiple selections
685+
editor.revealRange(selections[0], vscode.TextEditorRevealType.InCenter)
686+
}
687+
} catch (error) {}
688+
}
695689
}
696690

697691
private processException(e: any, tabID: string) {
@@ -991,6 +985,7 @@ export class ChatController {
991985
private async processPromptMessageAsNewThread(message: PromptMessage) {
992986
const session = this.sessionStorage.getSession(message.tabID)
993987
session.clearListOfReadFiles()
988+
session.setShowDiffOnFileWrite(false)
994989
this.editorContextExtractor
995990
.extractContextForTrigger('ChatMessage')
996991
.then(async (context) => {

packages/core/src/codewhispererChat/controllers/chat/messenger/messenger.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ export class Messenger {
213213
const message = this.getToolUseMessage(toolUse)
214214
// const isConfirmationRequired = this.getIsConfirmationRequired(toolUse)
215215

216+
// TODO: If toolUse is fs_write then session.setShowDiffOnFileWrite(true)
217+
216218
this.dispatcher.sendChatMessage(
217219
new ChatMessage(
218220
{

packages/core/src/codewhispererChat/controllers/chat/model.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,6 @@ export interface ViewDiff {
9696
totalCodeBlocks?: number
9797
}
9898

99-
export interface OpenDiff {
100-
command: string | undefined
101-
tabID: string
102-
messageId: string
103-
filePath: string
104-
referenceTrackerInformation?: CodeReference[]
105-
}
106-
10799
export type ChatPromptCommandType =
108100
| 'help'
109101
| 'clear'

packages/core/src/codewhispererChat/view/messages/messageListener.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ export class UIMessageListener {
6969
case 'view_diff':
7070
this.processViewDiff(msg)
7171
break
72-
case 'open-diff':
73-
this.processOpenDiff(msg)
74-
break
7572
case 'code_was_copied_to_clipboard':
7673
this.processCodeWasCopiedToClipboard(msg)
7774
break
@@ -224,14 +221,6 @@ export class UIMessageListener {
224221
})
225222
}
226223

227-
private processOpenDiff(msg: any) {
228-
this.chatControllerMessagePublishers.processOpenDiff.publish({
229-
command: msg.command,
230-
tabID: msg.tabID || msg.tabId,
231-
...msg,
232-
})
233-
}
234-
235224
private processCodeWasCopiedToClipboard(msg: any) {
236225
this.chatControllerMessagePublishers.processCopyCodeToClipboard.publish({
237226
command: msg.command,

0 commit comments

Comments
 (0)