-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathTreeSitterQueryCache.ts
More file actions
61 lines (54 loc) · 1.67 KB
/
TreeSitterQueryCache.ts
File metadata and controls
61 lines (54 loc) · 1.67 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
import type { Position, TextDocument } from "@cursorless/common";
import type { QueryMatch } from "./QueryCapture";
export class TreeSitterQueryCache {
private documentVersion: number = -1;
private documentUri: string = "";
private documentLanguageId: string = "";
private startPosition: Position | undefined;
private endPosition: Position | undefined;
private matches: QueryMatch[] = [];
clear() {
this.documentUri = "";
this.documentVersion = -1;
this.documentLanguageId = "";
this.startPosition = undefined;
this.endPosition = undefined;
this.matches = [];
}
isValid(
document: TextDocument,
startPosition: Position | undefined,
endPosition: Position | undefined,
) {
return (
this.documentVersion === document.version &&
this.documentUri === document.uri.toString() &&
this.documentLanguageId === document.languageId &&
positionsEqual(this.startPosition, startPosition) &&
positionsEqual(this.endPosition, endPosition)
);
}
update(
document: TextDocument,
startPosition: Position | undefined,
endPosition: Position | undefined,
matches: QueryMatch[],
) {
this.documentVersion = document.version;
this.documentUri = document.uri.toString();
this.documentLanguageId = document.languageId;
this.startPosition = startPosition;
this.endPosition = endPosition;
this.matches = matches;
}
get(): QueryMatch[] {
return this.matches;
}
}
function positionsEqual(a: Position | undefined, b: Position | undefined) {
if (a == null || b == null) {
return a === b;
}
return a.isEqual(b);
}
export const treeSitterQueryCache = new TreeSitterQueryCache();