Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit 312a97c

Browse files
authored
Add setting and toggle to omit supplementing reference results with search-based results (#615)
1 parent 1565f67 commit 312a97c

9 files changed

Lines changed: 309 additions & 19 deletions

File tree

extensions/go/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@
5050
{
5151
"action": "externalReferences.toggle",
5252
"when": "panel.activeView.id == 'references'"
53+
},
54+
{
55+
"action": "mixPreciseAndSearchBasedReferences.toggle",
56+
"when": "panel.activeView.id == 'references'"
5357
}
5458
]
5559
},
@@ -69,6 +73,22 @@
6973
"actionItem": {
7074
"label": "${config.go.showExternalReferences && \"Hide other repositories\" || \"Show other repositories\"}"
7175
}
76+
},
77+
{
78+
"id": "mixPreciseAndSearchBasedReferences.toggle",
79+
"command": "updateConfiguration",
80+
"title": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}",
81+
"commandArguments": [
82+
[
83+
"codeIntel.mixPreciseAndSearchBasedReferences"
84+
],
85+
"${!config.codeIntel.mixPreciseAndSearchBasedReferences}",
86+
null,
87+
"json"
88+
],
89+
"actionItem": {
90+
"label": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}"
91+
}
7292
}
7393
],
7494
"configuration": {
@@ -88,6 +108,10 @@
88108
"description": "Whether to fetch multiple precise definitions and references on hover.",
89109
"type": "boolean"
90110
},
111+
"codeIntel.mixPreciseAndSearchBasedReferences": {
112+
"description": "Whether to supplement precise references with search-based results.",
113+
"type": "boolean"
114+
},
91115
"basicCodeIntel.includeForks": {
92116
"description": "Whether to include forked repositories in search results.",
93117
"type": "boolean"

extensions/go/src/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface Settings {
1818
* Whether to fetch multiple precise definitions and references on hover.
1919
*/
2020
'codeIntel.disableRangeQueries'?: boolean
21+
/**
22+
* Whether to supplement precise references with search-based results.
23+
*/
24+
'codeIntel.mixPreciseAndSearchBasedReferences'?: boolean
2125
/**
2226
* Whether to include forked repositories in search results.
2327
*/

extensions/template/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@
4343
"publish": "yarn -s && src -config \"${SRC_CONFIG}\" ext publish -git-head \"${BUILDKITE_COMMIT}\""
4444
},
4545
"contributes": {
46+
"menus": {
47+
"panel/toolbar": [
48+
{
49+
"action": "mixPreciseAndSearchBasedReferences.toggle",
50+
"when": "panel.activeView.id == 'references'"
51+
}
52+
]
53+
},
54+
"actions": [
55+
{
56+
"id": "mixPreciseAndSearchBasedReferences.toggle",
57+
"command": "updateConfiguration",
58+
"title": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}",
59+
"commandArguments": [
60+
[
61+
"codeIntel.mixPreciseAndSearchBasedReferences"
62+
],
63+
"${!config.codeIntel.mixPreciseAndSearchBasedReferences}",
64+
null,
65+
"json"
66+
],
67+
"actionItem": {
68+
"label": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}"
69+
}
70+
}
71+
],
4672
"configuration": {
4773
"title": "Search-based code intelligence settings",
4874
"properties": {
@@ -58,6 +84,10 @@
5884
"description": "Whether to fetch multiple precise definitions and references on hover.",
5985
"type": "boolean"
6086
},
87+
"codeIntel.mixPreciseAndSearchBasedReferences": {
88+
"description": "Whether to supplement precise references with search-based results.",
89+
"type": "boolean"
90+
},
6191
"basicCodeIntel.includeForks": {
6292
"description": "Whether to include forked repositories in search results.",
6393
"type": "boolean"

extensions/typescript/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
{
5555
"action": "externalReferences.toggle",
5656
"when": "panel.activeView.id == 'references'"
57+
},
58+
{
59+
"action": "mixPreciseAndSearchBasedReferences.toggle",
60+
"when": "panel.activeView.id == 'references'"
5761
}
5862
]
5963
},
@@ -73,6 +77,22 @@
7377
"actionItem": {
7478
"label": "${config.typescript.showExternalReferences && \"Hide other repositories\" || \"Show other repositories\"}"
7579
}
80+
},
81+
{
82+
"id": "mixPreciseAndSearchBasedReferences.toggle",
83+
"command": "updateConfiguration",
84+
"title": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}",
85+
"commandArguments": [
86+
[
87+
"codeIntel.mixPreciseAndSearchBasedReferences"
88+
],
89+
"${!config.codeIntel.mixPreciseAndSearchBasedReferences}",
90+
null,
91+
"json"
92+
],
93+
"actionItem": {
94+
"label": "${!!config.codeIntel.mixPreciseAndSearchBasedReferences && \"Hide search-based results when precise results are available\" || \"Mix precise and search-based results\"}"
95+
}
7696
}
7797
],
7898
"configuration": {
@@ -92,6 +112,10 @@
92112
"description": "Whether to fetch multiple precise definitions and references on hover.",
93113
"type": "boolean"
94114
},
115+
"codeIntel.mixPreciseAndSearchBasedReferences": {
116+
"description": "Whether to supplement precise references with search-based results.",
117+
"type": "boolean"
118+
},
95119
"basicCodeIntel.includeForks": {
96120
"description": "Whether to include forked repositories in search results.",
97121
"type": "boolean"

extensions/typescript/src/settings.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export interface Settings {
1818
* Whether to fetch multiple precise definitions and references on hover.
1919
*/
2020
'codeIntel.disableRangeQueries'?: boolean
21+
/**
22+
* Whether to supplement precise references with search-based results.
23+
*/
24+
'codeIntel.mixPreciseAndSearchBasedReferences'?: boolean
2125
/**
2226
* Whether to include forked repositories in search results.
2327
*/

shared/activate.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BehaviorSubject, from, Observable, Subject } from 'rxjs'
2-
import { distinctUntilChanged, map } from 'rxjs/operators'
2+
import { distinctUntilChanged, map, startWith } from 'rxjs/operators'
33
import * as sourcegraph from 'sourcegraph'
44
import { LanguageSpec } from './language-specs/spec'
55
import { Logger, RedactingLogger } from './logging'
@@ -204,18 +204,36 @@ function activateWithoutLSP(
204204
wrapper: ProviderWrapper
205205
): void {
206206
context.subscriptions.add(sourcegraph.languages.registerDefinitionProvider(selector, wrapper.definition()))
207-
208-
context.subscriptions.add(sourcegraph.languages.registerReferenceProvider(selector, wrapper.references()))
209-
210207
context.subscriptions.add(sourcegraph.languages.registerHoverProvider(selector, wrapper.hover()))
211208

212-
// Do not try to register this provider on pre-3.18 instances as it
213-
// didn't exist.
209+
// Do not try to register this provider on pre-3.18 instances as
210+
// it didn't exist.
214211
if (sourcegraph.languages.registerDocumentHighlightProvider) {
215212
context.subscriptions.add(
216213
sourcegraph.languages.registerDocumentHighlightProvider(selector, wrapper.documentHighlights())
217214
)
218215
}
216+
217+
// Re-register the references provider whenever the value of the
218+
// mixPreciseAndSearchBasedReferences setting changes.
219+
220+
let unsubscribeReferencesProvider: sourcegraph.Unsubscribable
221+
const registerReferencesProvider = (): void => {
222+
unsubscribeReferencesProvider?.unsubscribe()
223+
unsubscribeReferencesProvider = sourcegraph.languages.registerReferenceProvider(selector, wrapper.references())
224+
context.subscriptions.add(unsubscribeReferencesProvider)
225+
}
226+
227+
context.subscriptions.add(
228+
from(sourcegraph.configuration)
229+
.pipe(
230+
startWith(false),
231+
map(() => sourcegraph.configuration.get().get('codeIntel.mixPreciseAndSearchBasedReferences') ?? false),
232+
distinctUntilChanged(),
233+
map(registerReferencesProvider)
234+
)
235+
.subscribe()
236+
)
219237
}
220238

221239
/**

shared/providers.test.ts

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createDocumentHighlightProvider,
1111
createHoverProvider,
1212
createReferencesProvider,
13+
clearReferenceResultCache,
1314
} from './providers'
1415
import { API } from './util/api'
1516

@@ -85,7 +86,7 @@ describe('createDefinitionProvider', () => {
8586
assert.deepStrictEqual(await gatherValues(result), [location1, location2])
8687
})
8788

88-
it('falls back to basic when precise results are not found', async () => {
89+
it('falls back to search when precise results are not found', async () => {
8990
const result = createDefinitionProvider(
9091
() => Promise.resolve(null),
9192
() => asyncGeneratorFromValues([location3]),
@@ -107,6 +108,8 @@ describe('createDefinitionProvider', () => {
107108
})
108109

109110
describe('createReferencesProvider', () => {
111+
beforeEach(clearReferenceResultCache)
112+
110113
it('uses LSIF definitions as source of truth', async () => {
111114
const result = createReferencesProvider(
112115
() =>
@@ -205,7 +208,8 @@ describe('createReferencesProvider', () => {
205208
undefined,
206209
undefined,
207210
undefined,
208-
makeStubAPI()
211+
makeStubAPI(),
212+
() => true
209213
).provideReferences(textDocument, position, {
210214
includeDeclaration: false,
211215
}) as Observable<sourcegraph.Badged<sourcegraph.Location>[]>
@@ -244,7 +248,63 @@ describe('createReferencesProvider', () => {
244248
undefined,
245249
undefined,
246250
undefined,
247-
makeStubAPI()
251+
makeStubAPI(),
252+
() => true
253+
).provideReferences(textDocument, position, {
254+
includeDeclaration: false,
255+
}) as Observable<sourcegraph.Badged<sourcegraph.Location>[]>
256+
257+
assert.deepStrictEqual(await gatherValues(result), [
258+
[
259+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
260+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
261+
],
262+
[
263+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
264+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
265+
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
266+
],
267+
[
268+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
269+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
270+
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
271+
{
272+
...location4,
273+
badge: indicators.impreciseBadge,
274+
aggregableBadges: [indicators.searchBasedBadge],
275+
},
276+
],
277+
[
278+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
279+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
280+
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
281+
{
282+
...location4,
283+
badge: indicators.impreciseBadge,
284+
aggregableBadges: [indicators.searchBasedBadge],
285+
},
286+
{
287+
...location9,
288+
badge: indicators.impreciseBadge,
289+
aggregableBadges: [indicators.searchBasedBadge],
290+
},
291+
],
292+
])
293+
})
294+
295+
it('supplements LSIF results with search results (disabled)', async () => {
296+
const result = createReferencesProvider(
297+
() =>
298+
asyncGeneratorFromValues([
299+
[location1, location2],
300+
[location1, location2, location3],
301+
]),
302+
() => asyncGeneratorFromValues([[location4], [location4, location7, location8, location9]]),
303+
undefined,
304+
undefined,
305+
undefined,
306+
makeStubAPI(),
307+
() => false
248308
).provideReferences(textDocument, position, {
249309
includeDeclaration: false,
250310
}) as Observable<sourcegraph.Badged<sourcegraph.Location>[]>
@@ -259,6 +319,36 @@ describe('createReferencesProvider', () => {
259319
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
260320
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
261321
],
322+
])
323+
})
324+
325+
it('supplements LSIF results with search results (toggled)', async () => {
326+
const mixedResults = createReferencesProvider(
327+
() =>
328+
asyncGeneratorFromValues([
329+
[location1, location2],
330+
[location1, location2, location3],
331+
]),
332+
() => asyncGeneratorFromValues([[location4], [location4, location7, location8, location9]]),
333+
undefined,
334+
undefined,
335+
undefined,
336+
makeStubAPI(),
337+
() => true
338+
).provideReferences(textDocument, position, {
339+
includeDeclaration: false,
340+
}) as Observable<sourcegraph.Badged<sourcegraph.Location>[]>
341+
342+
assert.deepStrictEqual(await gatherValues(mixedResults), [
343+
[
344+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
345+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
346+
],
347+
[
348+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
349+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
350+
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
351+
],
262352
[
263353
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
264354
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
@@ -285,6 +375,31 @@ describe('createReferencesProvider', () => {
285375
},
286376
],
287377
])
378+
379+
const preciseResults = createReferencesProvider(
380+
() =>
381+
asyncGeneratorFromValues([
382+
[location1, location2],
383+
[location1, location2, location3],
384+
]),
385+
() => asyncGeneratorFromValues([[location4], [location4, location7, location8, location9]]),
386+
undefined,
387+
undefined,
388+
undefined,
389+
makeStubAPI(),
390+
() => false
391+
).provideReferences(textDocument, position, {
392+
includeDeclaration: false,
393+
}) as Observable<sourcegraph.Badged<sourcegraph.Location>[]>
394+
395+
// Should immediately return all precise results from previous call
396+
assert.deepStrictEqual(await gatherValues(preciseResults), [
397+
[
398+
{ ...location1, aggregableBadges: [indicators.semanticBadge] },
399+
{ ...location2, aggregableBadges: [indicators.semanticBadge] },
400+
{ ...location3, aggregableBadges: [indicators.semanticBadge] },
401+
],
402+
])
288403
})
289404
})
290405

@@ -383,7 +498,7 @@ describe('createHoverProvider', () => {
383498
])
384499
})
385500

386-
it('falls back to basic when precise results are not found', async () => {
501+
it('falls back to search when precise results are not found', async () => {
387502
const result = createHoverProvider(
388503
LSIFSupport.None,
389504
() => Promise.resolve(null),

0 commit comments

Comments
 (0)