Skip to content

Commit 817b055

Browse files
committed
Add retry logic for language provider calls to improve referencing files search reliability
- sometimes we get 'no references found' on first run, and correct references on a second run └── apps └── editor └── src ├── commands │ └── select-referencing-files-commands │ ├── index.ts │ ├── select-referencing-files-commands.ts │ └── utils │ ├── get-referencing-files-for-position.ts │ ├── get-referencing-files-for-uris.ts │ └── prompt-for-referencing-files.ts └── extension.ts
1 parent b27a4ca commit 817b055

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎apps/editor/src/commands/select-referencing-files-commands/select-referencing-files-commands.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,21 @@ export const select_referencing_files_commands = (
105105
title: t('command.select-referencing-files.processing')
106106
},
107107
async () => {
108-
const definitions = await vscode.commands.executeCommand<any[]>(
108+
let definitions = await vscode.commands.executeCommand<any[]>(
109109
'vscode.executeDefinitionProvider',
110110
target_uri,
111111
target_position
112112
)
113113

114+
if (!definitions || definitions.length === 0) {
115+
await new Promise((resolve) => setTimeout(resolve, 500))
116+
definitions = await vscode.commands.executeCommand<any[]>(
117+
'vscode.executeDefinitionProvider',
118+
target_uri,
119+
target_position
120+
)
121+
}
122+
114123
const ignore_paths: string[] = []
115124

116125
if (definitions) {

‎apps/editor/src/commands/select-referencing-files-commands/utils/get-referencing-files-for-position.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ export const get_referencing_files_for_position = async (params: {
88
workspace_provider: WorkspaceProvider
99
ignore_paths: string[]
1010
}): Promise<{ file_path: string; range: vscode.Range }[]> => {
11-
const locations = await vscode.commands.executeCommand<vscode.Location[]>(
11+
let locations = await vscode.commands.executeCommand<vscode.Location[]>(
1212
'vscode.executeReferenceProvider',
1313
params.uri,
1414
params.position
1515
)
1616

17+
if (!locations || locations.length === 0) {
18+
await new Promise((resolve) => setTimeout(resolve, 500))
19+
locations = await vscode.commands.executeCommand<vscode.Location[]>(
20+
'vscode.executeReferenceProvider',
21+
params.uri,
22+
params.position
23+
)
24+
}
25+
1726
if (!locations || locations.length === 0) return []
1827

1928
const file_map = new Map<string, vscode.Range>()

‎apps/editor/src/commands/select-referencing-files-commands/utils/get-referencing-files-for-uris.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,17 @@ export const get_referencing_files_for_uris = async (params: {
2929
if (is_unsupported_for_symbol_search) {
3030
positions.push(new vscode.Position(0, 0))
3131
} else {
32-
const symbols = await vscode.commands.executeCommand<
32+
let symbols = await vscode.commands.executeCommand<
3333
vscode.DocumentSymbol[] | vscode.SymbolInformation[]
3434
>('vscode.executeDocumentSymbolProvider', uri)
3535

36+
if (!symbols || symbols.length === 0) {
37+
await new Promise((resolve) => setTimeout(resolve, 500))
38+
symbols = await vscode.commands.executeCommand<
39+
vscode.DocumentSymbol[] | vscode.SymbolInformation[]
40+
>('vscode.executeDocumentSymbolProvider', uri)
41+
}
42+
3643
if (symbols) {
3744
const top_level_containers = new Set<string>()
3845

@@ -71,15 +78,29 @@ export const get_referencing_files_for_uris = async (params: {
7178
}
7279
}
7380

81+
let has_retried_references = false
82+
7483
for (let i = 0; i < positions.length; i++) {
7584
if (params.token.isCancellationRequested) {
7685
break
7786
}
7887
const position = positions[i]
7988

80-
const locations = await vscode.commands.executeCommand<
81-
vscode.Location[]
82-
>('vscode.executeReferenceProvider', uri, position)
89+
let locations = await vscode.commands.executeCommand<vscode.Location[]>(
90+
'vscode.executeReferenceProvider',
91+
uri,
92+
position
93+
)
94+
95+
if ((!locations || locations.length === 0) && !has_retried_references) {
96+
has_retried_references = true
97+
await new Promise((resolve) => setTimeout(resolve, 500))
98+
locations = await vscode.commands.executeCommand<vscode.Location[]>(
99+
'vscode.executeReferenceProvider',
100+
uri,
101+
position
102+
)
103+
}
83104

84105
if (locations) {
85106
for (const loc of locations) {

0 commit comments

Comments
 (0)