Skip to content

Commit f9a9ec1

Browse files
Properly compare positions for cache validation (#2712)
You can't compare position instances with `===` ## Checklist - [/] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [/] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [/] I have not broken the cheatsheet
1 parent 1920cdd commit f9a9ec1

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/cursorless-engine/src/languages/TreeSitterQuery/treeSitterQueryCache.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { Position, TextDocument } from "@cursorless/common";
22
import type { QueryMatch } from "./QueryCapture";
33

44
export class Cache {
5-
private documentUri: string = "";
65
private documentVersion: number = -1;
6+
private documentUri: string = "";
77
private documentLanguageId: string = "";
88
private startPosition: Position | undefined;
99
private endPosition: Position | undefined;
@@ -24,11 +24,11 @@ export class Cache {
2424
endPosition: Position | undefined,
2525
) {
2626
return (
27-
this.documentUri === document.uri.toString() &&
2827
this.documentVersion === document.version &&
28+
this.documentUri === document.uri.toString() &&
2929
this.documentLanguageId === document.languageId &&
30-
this.startPosition === startPosition &&
31-
this.endPosition === endPosition
30+
positionsEqual(this.startPosition, startPosition) &&
31+
positionsEqual(this.endPosition, endPosition)
3232
);
3333
}
3434

@@ -38,8 +38,8 @@ export class Cache {
3838
endPosition: Position | undefined,
3939
matches: QueryMatch[],
4040
) {
41-
this.documentUri = document.uri.toString();
4241
this.documentVersion = document.version;
42+
this.documentUri = document.uri.toString();
4343
this.documentLanguageId = document.languageId;
4444
this.startPosition = startPosition;
4545
this.endPosition = endPosition;
@@ -51,4 +51,11 @@ export class Cache {
5151
}
5252
}
5353

54+
function positionsEqual(a: Position | undefined, b: Position | undefined) {
55+
if (a == null || b == null) {
56+
return a === b;
57+
}
58+
return a.isEqual(b);
59+
}
60+
5461
export const treeSitterQueryCache = new Cache();

0 commit comments

Comments
 (0)