Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(amazonq): Fix edge cases of context command updates #6686

Merged
merged 6 commits into from
Feb 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 44 additions & 32 deletions packages/core/src/amazonq/lsp/lspClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,37 @@ export async function activate(extensionContext: ExtensionContext) {

let savedDocument: vscode.Uri | undefined = undefined

const onAdd = async (filePaths: string[]) => {
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
await LspClient.instance.updateIndex(filePaths, 'add')
await waitUntil(
async () => {
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
if (newIndexSeqNum > indexSeqNum) {
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
return true
}
return false
},
{ interval: 500, timeout: 5_000, truthy: true }
)
}
const onRemove = async (filePaths: string[]) => {
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
await LspClient.instance.updateIndex(filePaths, 'remove')
await waitUntil(
async () => {
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
if (newIndexSeqNum > indexSeqNum) {
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
return true
}
return false
},
{ interval: 500, timeout: 5_000, truthy: true }
)
}

toDispose.push(
vscode.workspace.onDidSaveTextDocument((document) => {
if (document.uri.scheme !== 'file') {
Expand All @@ -326,42 +357,23 @@ export async function activate(extensionContext: ExtensionContext) {
if (savedDocument && editor && editor.document.uri.fsPath !== savedDocument.fsPath) {
void LspClient.instance.updateIndex([savedDocument.fsPath], 'update')
}
// user created a new empty file using File -> New File
// these events will not be captured by vscode.workspace.onDidCreateFiles
// because it was created by File Explorer(Win) or Finder(MacOS)
// TODO: consider using a high performance fs watcher
if (editor?.document.getText().length === 0) {
void onAdd([editor.document.uri.fsPath])
}
}),
vscode.workspace.onDidCreateFiles(async (e) => {
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
await LspClient.instance.updateIndex(
e.files.map((f) => f.fsPath),
'add'
)
await waitUntil(
async () => {
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
if (newIndexSeqNum > indexSeqNum) {
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
return true
}
return false
},
{ interval: 500, timeout: 10_000, truthy: true }
)
await onAdd(e.files.map((f) => f.fsPath))
}),
vscode.workspace.onDidDeleteFiles(async (e) => {
const indexSeqNum = await LspClient.instance.getIndexSequenceNumber()
await LspClient.instance.updateIndex(
e.files.map((f) => f.fsPath),
'remove'
)
await waitUntil(
async () => {
const newIndexSeqNum = await LspClient.instance.getIndexSequenceNumber()
if (newIndexSeqNum > indexSeqNum) {
await vscode.commands.executeCommand(`aws.amazonq.updateContextCommandItems`)
return true
}
return false
},
{ interval: 500, timeout: 10_000, truthy: true }
)
await onRemove(e.files.map((f) => f.fsPath))
}),
vscode.workspace.onDidRenameFiles(async (e) => {
await onRemove(e.files.map((f) => f.oldUri.fsPath))
await onAdd(e.files.map((f) => f.newUri.fsPath))
})
)

Expand Down
Loading