-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathLanguageDefinitions.ts
More file actions
102 lines (90 loc) · 3.18 KB
/
LanguageDefinitions.ts
File metadata and controls
102 lines (90 loc) · 3.18 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {
Disposable,
FileSystem,
Notifier,
Range,
TextDocument,
getCursorlessRepoRoot,
} from "@cursorless/common";
import { join } from "path";
import { SyntaxNode } from "web-tree-sitter";
import { TreeSitter } from "..";
import { ide } from "../singletons/ide.singleton";
import { LanguageDefinition } from "./LanguageDefinition";
/**
* Sentinel value to indicate that a language doesn't have
* a new-style query definition file
*/
const LANGUAGE_UNDEFINED = Symbol("LANGUAGE_UNDEFINED");
/**
* Keeps a map from language ids to {@link LanguageDefinition} instances,
* constructing them as necessary
*/
export class LanguageDefinitions {
private notifier: Notifier = new Notifier();
/**
* Maps from language id to {@link LanguageDefinition} or
* {@link LANGUAGE_UNDEFINED} if language doesn't have new-style definitions.
* We use a sentinel value instead of undefined so that we can distinguish
* between a situation where we haven't yet checked whether a language has a
* new-style query definition and a situation where we've checked and found
* that it doesn't. The former case is represented by `undefined` (due to the
* semantics of {@link Map.get}), while the latter is represented by the
* sentinel value.
*/
private languageDefinitions: Map<
string,
LanguageDefinition | typeof LANGUAGE_UNDEFINED
> = new Map();
private queryDir: string;
private disposables: Disposable[] = [];
constructor(
fileSystem: FileSystem,
private treeSitter: TreeSitter,
) {
// Use the repo root as the root for development mode, so that we can
// we can make hot-reloading work for the queries
this.queryDir = join(
ide().runMode === "development"
? getCursorlessRepoRoot()
: ide().assetsRoot,
"queries",
);
if (ide().runMode === "development") {
this.disposables.push(
fileSystem.watchDir(this.queryDir, () => {
this.languageDefinitions.clear();
this.notifier.notifyListeners();
}),
);
}
}
/**
* Get a language definition for the given language id, if the language
* has a new-style query definition, or return undefined if the language doesn't
*
* @param languageId The language id for which to get a language definition
* @returns A language definition for the given language id, or undefined if
* the given language id doesn't have a new-style query definition
*/
get(languageId: string): LanguageDefinition | undefined {
let definition = this.languageDefinitions.get(languageId);
if (definition == null) {
definition =
LanguageDefinition.create(this.treeSitter, this.queryDir, languageId) ??
LANGUAGE_UNDEFINED;
this.languageDefinitions.set(languageId, definition);
}
return definition === LANGUAGE_UNDEFINED ? undefined : definition;
}
/**
* @deprecated Only for use in legacy containing scope stage
*/
public getNodeAtLocation(document: TextDocument, range: Range): SyntaxNode {
return this.treeSitter.getNodeAtLocation(document, range);
}
onDidChangeDefinition = this.notifier.registerListener;
dispose() {
this.disposables.forEach((disposable) => disposable.dispose());
}
}