-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathReferencesProvider.ts
50 lines (40 loc) · 1.71 KB
/
ReferencesProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Location, ReferenceParams } from "vscode-languageserver-protocol";
import { AugmentedLiquidSourceCode, DocumentManager } from "../documents";
import { BaseReferencesProvider } from "./BaseReferencesProvider";
import { findCurrentNode, SourceCodeType } from "@shopify/theme-check-common";
import { BlockReferencesProvider } from "./providers/BlockReferencesProvider";
import { SnippetReferencesProvider } from "./providers/SnippetReferencesProvider";
export type GetLiquidFiles = (rootUri: string) => Promise<AugmentedLiquidSourceCode[]>;
export class ReferencesProvider {
private providers: BaseReferencesProvider[];
constructor(
private documentManager: DocumentManager,
private getLiquidFiles: GetLiquidFiles,
) {
this.providers = [
new BlockReferencesProvider(this.documentManager, this.getLiquidFiles),
new SnippetReferencesProvider(this.documentManager, this.getLiquidFiles),
];
}
async references(params: ReferenceParams): Promise<Location[] | undefined> {
try {
const document = this.documentManager.get(params.textDocument.uri)
if (!document || document.type !== SourceCodeType.LiquidHtml || document.ast instanceof Error) {
return;
}
const [currentNode, ancestors] = findCurrentNode(document.ast, document.textDocument.offsetAt(params.position));
const promises = this.providers.map((provider) => provider.references(currentNode, ancestors, params));
const locations = [];
for(const promise of promises) {
const result = await promise;
if (result) {
locations.push(...result);
}
}
return locations;
} catch (error) {
console.error(error);
return;
}
}
}