Skip to content

Commit bc417d8

Browse files
Update cache
1 parent 2bf3105 commit bc417d8

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

packages/cursorless-engine/src/disabledComponents/DisabledLanguageDefinitions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class DisabledLanguageDefinitions implements LanguageDefinitions {
1616
return undefined;
1717
}
1818

19+
clearCache(): void {
20+
// Do nothing
21+
}
22+
1923
getNodeAtLocation(
2024
_document: TextDocument,
2125
_range: Range,

packages/cursorless-engine/src/languages/LanguageDefinition.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ import { TreeSitterQuery } from "./TreeSitterQuery";
1818
import type { QueryCapture } from "./TreeSitterQuery/QueryCapture";
1919
import { validateQueryCaptures } from "./TreeSitterQuery/validateQueryCaptures";
2020

21-
const cache = new LanguageDefinitionCache();
22-
2321
/**
2422
* Represents a language definition for a single language, including the
2523
* tree-sitter query used to extract scopes for the given language
2624
*/
2725
export class LanguageDefinition {
26+
private cache: LanguageDefinitionCache;
27+
2828
private constructor(
2929
/**
3030
* The tree-sitter query used to extract scopes for the given language.
3131
* Note that this query contains patterns for all scope types that the
3232
* language supports using new-style tree-sitter queries
3333
*/
3434
private query: TreeSitterQuery,
35-
) {}
35+
) {
36+
this.cache = new LanguageDefinitionCache();
37+
}
3638

3739
/**
3840
* Construct a language definition for the given language id, if the language
@@ -97,11 +99,15 @@ export class LanguageDefinition {
9799
document: TextDocument,
98100
captureName: SimpleScopeTypeType,
99101
): QueryCapture[] {
100-
if (!cache.isValid(document)) {
101-
cache.update(document, this.getCapturesMap(document));
102+
if (!this.cache.isValid(document)) {
103+
this.cache.update(document, this.getCapturesMap(document));
102104
}
103105

104-
return cache.get(captureName);
106+
return this.cache.get(captureName);
107+
}
108+
109+
clearCache(): void {
110+
this.cache.clear();
105111
}
106112

107113
/**

packages/cursorless-engine/src/languages/LanguageDefinitionCache.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@ import type {
44
TextDocument,
55
} from "@cursorless/common";
66
import type { QueryCapture } from "./TreeSitterQuery/QueryCapture";
7-
import { ide } from "..";
87

98
export class LanguageDefinitionCache {
10-
private languageId: string = "";
119
private documentUri: string = "";
1210
private documentVersion: number = -1;
1311
private captures: StringRecord<QueryCapture[]> = {};
1412

13+
clear() {
14+
this.documentUri = "";
15+
this.documentVersion = -1;
16+
this.captures = {};
17+
}
18+
1519
isValid(document: TextDocument) {
16-
if (ide().runMode === "test") {
17-
return false;
18-
}
1920
return (
20-
this.languageId === document.languageId &&
2121
this.documentUri === document.uri.toString() &&
2222
this.documentVersion === document.version
2323
);
2424
}
2525

2626
update(document: TextDocument, captures: StringRecord<QueryCapture[]>) {
27-
this.languageId = document.languageId;
2827
this.documentUri = document.uri.toString();
2928
this.documentVersion = document.version;
3029
this.captures = captures;

packages/cursorless-engine/src/languages/LanguageDefinitions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export interface LanguageDefinitions {
3232
*/
3333
get(languageId: string): LanguageDefinition | undefined;
3434

35+
/**
36+
* Clear the cache of all language definitions. This is run at the start of a command.
37+
*/
38+
clearCache(): void;
39+
3540
/**
3641
* @deprecated Only for use in legacy containing scope stage
3742
*/
@@ -155,6 +160,14 @@ export class LanguageDefinitionsImpl
155160
return definition === LANGUAGE_UNDEFINED ? undefined : definition;
156161
}
157162

163+
clearCache(): void {
164+
for (const definition of this.languageDefinitions.values()) {
165+
if (definition !== LANGUAGE_UNDEFINED) {
166+
definition.clearCache();
167+
}
168+
}
169+
}
170+
158171
public getNodeAtLocation(document: TextDocument, range: Range): SyntaxNode {
159172
return this.treeSitter.getNodeAtLocation(document, range);
160173
}

packages/cursorless-engine/src/runCommand.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export async function runCommand(
7171
commandRunner = decorator.wrapCommandRunner(readableHatMap, commandRunner);
7272
}
7373

74+
languageDefinitions.clearCache();
75+
7476
const response = await commandRunner.run(commandComplete);
7577

7678
return await unwrapLegacyCommandResponse(command, response);

0 commit comments

Comments
 (0)