Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit b00b8f3

Browse files
committed
feat: add method to get locations with provider name
add TextDocumentLocationProviderRegistry.getLocationsWithProviderName method
1 parent d445230 commit b00b8f3

File tree

7 files changed

+74
-6
lines changed

7 files changed

+74
-6
lines changed

src/client/features/hover.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export class TextDocumentHoverFeature extends Feature<TextDocumentRegistrationOp
4242
}
4343
this.register(this.messages, {
4444
id: uuidv4(),
45-
registerOptions: { documentSelector },
45+
registerOptions: {
46+
documentSelector,
47+
providerName: this.client.name,
48+
},
4649
})
4750
}
4851

src/client/features/location.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export abstract class TextDocumentLocationFeature<
5858
}
5959
this.register(this.messages, {
6060
id: uuidv4(),
61-
registerOptions: { documentSelector },
61+
registerOptions: {
62+
documentSelector,
63+
providerName: this.client.name,
64+
},
6265
})
6366
}
6467

src/client/features/textDocument.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ export class TextDocumentDidOpenFeature extends TextDocumentNotificationFeature<
124124
if (documentSelector && textDocumentSyncOptions && textDocumentSyncOptions.openClose) {
125125
this.register(this.messages, {
126126
id: uuidv4(),
127-
registerOptions: { documentSelector },
127+
registerOptions: {
128+
documentSelector,
129+
providerName: this.client.name,
130+
},
128131
})
129132
}
130133
}
@@ -171,7 +174,10 @@ export class TextDocumentDidCloseFeature extends TextDocumentNotificationFeature
171174
if (documentSelector && textDocumentSyncOptions && textDocumentSyncOptions.openClose) {
172175
this.register(this.messages, {
173176
id: uuidv4(),
174-
registerOptions: { documentSelector },
177+
registerOptions: {
178+
documentSelector,
179+
providerName: this.client.name,
180+
},
175181
})
176182
}
177183
}

src/environment/providers/location.ts

+47-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { map, switchMap } from 'rxjs/operators'
33
import { Location } from 'vscode-languageserver-types'
44
import { ReferenceParams, TextDocumentPositionParams, TextDocumentRegistrationOptions } from '../../protocol'
55
import { FeatureProviderRegistry } from './registry'
6-
import { flattenAndCompact } from './util'
6+
import { flattenAndCompact, flattenAndCompactNonNull } from './util'
77

88
/**
99
* Function signature for retrieving related locations given a location (e.g., definition, implementation, and type
@@ -22,6 +22,23 @@ export class TextDocumentLocationProviderRegistry<
2222
public getLocation(params: P): Observable<L | L[] | null> {
2323
return getLocation<P, L>(this.providers, params)
2424
}
25+
26+
public getLocationsWithProviderName(params: P): Observable<{ providerName: string; location: L }[] | null> {
27+
return getLocationsWithProviderName<P, L>(this.providersWithName, params)
28+
}
29+
30+
public readonly providersWithName: Observable<
31+
{ name: string; provider: ProvideTextDocumentLocationSignature<P, L> }[]
32+
> = this.entries.pipe(
33+
map(providers =>
34+
providers.map(({ provider, registrationOptions }) => {
35+
return {
36+
name: registrationOptions.providerName,
37+
provider,
38+
}
39+
})
40+
)
41+
)
2542
}
2643

2744
/**
@@ -62,6 +79,35 @@ export function getLocations<
6279
)
6380
}
6481

82+
export function getLocationsWithProviderName<
83+
P extends TextDocumentPositionParams = TextDocumentPositionParams,
84+
L extends Location = Location
85+
>(
86+
providersWithName: Observable<{ name: string; provider: ProvideTextDocumentLocationSignature<P, L> }[]>,
87+
params: P
88+
): Observable<{ providerName: string; location: L }[]> {
89+
return providersWithName.pipe(
90+
switchMap(async providersWithName => {
91+
const resultsByProvider = await Promise.all(
92+
providersWithName.map(async ({ provider, name }) => {
93+
const providerRes = await provider(params).toPromise()
94+
return flattenAndCompactNonNull([providerRes]).map(loc => ({
95+
providerName: name,
96+
location: loc,
97+
}))
98+
})
99+
)
100+
const flattenedResults: { providerName: string; location: L }[] = []
101+
for (const providerResults of resultsByProvider) {
102+
for (const res of providerResults) {
103+
flattenedResults.push(res)
104+
}
105+
}
106+
return flattenedResults
107+
})
108+
)
109+
}
110+
65111
/**
66112
* Provides reference results from all extensions.
67113
*

src/environment/providers/registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Entry<O, P> {
99

1010
/** Base class for provider registries for features. */
1111
export abstract class FeatureProviderRegistry<O, P> {
12-
private entries = new BehaviorSubject<Entry<O, P>[]>([])
12+
protected entries = new BehaviorSubject<Entry<O, P>[]>([])
1313

1414
public constructor(initialEntries?: Entry<O, P>[]) {
1515
if (initialEntries) {

src/environment/providers/util.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ export function flattenAndCompact<T>(value: (T | T[] | null)[] | null): T[] | nu
88
const merged = flatten(compact(value))
99
return merged.length === 0 ? null : merged
1010
}
11+
12+
/** Flattens and compacts the argument. If it is null or if the result is empty, it returns null. */
13+
export function flattenAndCompactNonNull<T>(value: (T | T[] | null)[] | null): T[] {
14+
return value ? flatten(compact(value)) : []
15+
}

src/protocol/textDocument.ts

+5
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ export interface TextDocumentRegistrationOptions {
358358
* the document selector provided on the client side will be used.
359359
*/
360360
documentSelector: DocumentSelector | null
361+
362+
/**
363+
* User-visible name of the of the provider being registered.
364+
*/
365+
providerName: string
361366
}
362367

363368
/**

0 commit comments

Comments
 (0)