Skip to content

Commit 36caa76

Browse files
authored
fix(amazonq): Fix edge cases of context command updates (#6686)
## Problem 1. If user create a file using the File>New File buttons, the onCreateFile listener does not capture it. <img width="451" alt="Screenshot 2025-02-26 at 2 46 31 PM" src="https://github.com/user-attachments/assets/e2e53d38-ca0e-413a-91f9-b2845b3549a6" /> 2. Renaming file or folders is not listened and updated. ## Solution 1. Listen to new files created by File>New File buttons 2. Listen to renaming file or folders --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 445c659 commit 36caa76

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

packages/core/src/amazonq/lsp/lspClient.ts

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,37 @@ export async function activate(extensionContext: ExtensionContext) {
315315

316316
let savedDocument: vscode.Uri | undefined = undefined
317317

318+
const onAdd = async (filePaths: string[]) => {
319+
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
320+
await LspClient.instance.updateIndex(filePaths, 'add')
321+
await waitUntil(
322+
async () => {
323+
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
324+
if (newIndexSeqNum > indexSeqNum) {
325+
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
326+
return true
327+
}
328+
return false
329+
},
330+
{ interval: 500, timeout: 5_000, truthy: true }
331+
)
332+
}
333+
const onRemove = async (filePaths: string[]) => {
334+
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
335+
await LspClient.instance.updateIndex(filePaths, 'remove')
336+
await waitUntil(
337+
async () => {
338+
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
339+
if (newIndexSeqNum > indexSeqNum) {
340+
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
341+
return true
342+
}
343+
return false
344+
},
345+
{ interval: 500, timeout: 5_000, truthy: true }
346+
)
347+
}
348+
318349
toDispose.push(
319350
vscode.workspace.onDidSaveTextDocument((document) => {
320351
if (document.uri.scheme !== 'file') {
@@ -326,42 +357,23 @@ export async function activate(extensionContext: ExtensionContext) {
326357
if (savedDocument && editor && editor.document.uri.fsPath !== savedDocument.fsPath) {
327358
void LspClient.instance.updateIndex([savedDocument.fsPath], 'update')
328359
}
360+
// user created a new empty file using File -> New File
361+
// these events will not be captured by vscode.workspace.onDidCreateFiles
362+
// because it was created by File Explorer(Win) or Finder(MacOS)
363+
// TODO: consider using a high performance fs watcher
364+
if (editor?.document.getText().length === 0) {
365+
void onAdd([editor.document.uri.fsPath])
366+
}
329367
}),
330368
vscode.workspace.onDidCreateFiles(async (e) => {
331-
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
332-
await LspClient.instance.updateIndex(
333-
e.files.map((f) => f.fsPath),
334-
'add'
335-
)
336-
await waitUntil(
337-
async () => {
338-
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
339-
if (newIndexSeqNum > indexSeqNum) {
340-
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
341-
return true
342-
}
343-
return false
344-
},
345-
{ interval: 500, timeout: 10_000, truthy: true }
346-
)
369+
await onAdd(e.files.map((f) => f.fsPath))
347370
}),
348371
vscode.workspace.onDidDeleteFiles(async (e) => {
349-
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
350-
await LspClient.instance.updateIndex(
351-
e.files.map((f) => f.fsPath),
352-
'remove'
353-
)
354-
await waitUntil(
355-
async () => {
356-
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
357-
if (newIndexSeqNum > indexSeqNum) {
358-
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
359-
return true
360-
}
361-
return false
362-
},
363-
{ interval: 500, timeout: 10_000, truthy: true }
364-
)
372+
await onRemove(e.files.map((f) => f.fsPath))
373+
}),
374+
vscode.workspace.onDidRenameFiles(async (e) => {
375+
await onRemove(e.files.map((f) => f.oldUri.fsPath))
376+
await onAdd(e.files.map((f) => f.newUri.fsPath))
365377
})
366378
)
367379

0 commit comments

Comments
 (0)