Skip to content

Commit 858b603

Browse files
General bug fixes (#3176)
1 parent 512fe6e commit 858b603

31 files changed

Lines changed: 264 additions & 105 deletions

‎packages/common/src/types/Position.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TextDocument } from "..";
2-
import { Range } from "..";
2+
import { Range, stringToInteger } from "..";
33

44
export class Position {
55
/**
@@ -27,7 +27,13 @@ export class Position {
2727
`Invalid concise position format: "${concise}". Expected "line:character" format.`,
2828
);
2929
}
30-
const [line, character] = parts.map((s) => parseInt(s, 10));
30+
const line = stringToInteger(parts[0]);
31+
const character = stringToInteger(parts[1]);
32+
if (line == null || character == null || line < 0 || character < 0) {
33+
throw new Error(
34+
`Invalid concise position format: "${concise}". Line and character should be non-negative integers.`,
35+
);
36+
}
3137
return new Position(line, character);
3238
}
3339

‎packages/common/src/util/CompositeKeyMap.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Based on https://stackoverflow.com/a/54523103
77
*/
88
export class CompositeKeyMap<K, V> {
9-
private map: Record<string, V> = {};
9+
private map = new Map<string, V>();
1010

1111
/**
1212
*
@@ -19,25 +19,25 @@ export class CompositeKeyMap<K, V> {
1919
}
2020

2121
set(key: K, item: V): this {
22-
this.map[this.hash(key)] = item;
22+
this.map.set(this.hash(key), item);
2323
return this;
2424
}
2525

2626
has(key: K): boolean {
27-
return this.hash(key) in this.map;
27+
return this.map.has(this.hash(key));
2828
}
2929

3030
get(key: K): V | undefined {
31-
return this.map[this.hash(key)];
31+
return this.map.get(this.hash(key));
3232
}
3333

3434
delete(key: K): this {
35-
delete this.map[this.hash(key)];
35+
this.map.delete(this.hash(key));
3636
return this;
3737
}
3838

3939
clear(): this {
40-
this.map = {};
40+
this.map.clear();
4141
return this;
4242
}
4343
}

‎packages/common/src/util/Notifier.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export class Notifier<T extends Arr = []> {
1616
* Notify all listeners that something has changed
1717
*/
1818
notifyListeners = (...args: [...T]): void => {
19-
this.listeners.forEach((listener) => listener(...args));
19+
// Iterate over a snapshot so listener removal during notification doesn't
20+
// affect which listeners are notified in this cycle.
21+
this.listeners.slice().forEach((listener) => listener(...args));
2022
};
2123

2224
/**

‎packages/common/src/util/blendColors.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ export function blendColors(base: string, top: string): string {
1616
const topRgba = tinycolor(top).toRgb();
1717
const blendedAlpha = 1 - (1 - topRgba.a) * (1 - baseRgba.a);
1818

19-
function interpolateChannel(channel: "r" | "g" | "b"): number {
19+
// If both colors are fully transparent, channel interpolation divides by zero.
20+
// In this case, the blend is simply fully transparent.
21+
if (blendedAlpha === 0) {
22+
return tinycolor({ r: 0, g: 0, b: 0, a: 0 }).toHex8String();
23+
}
24+
25+
const interpolateChannel = (channel: "r" | "g" | "b"): number => {
2026
return Math.round(
2127
(topRgba[channel] * topRgba.a) / blendedAlpha +
2228
(baseRgba[channel] * baseRgba.a * (1 - topRgba.a)) / blendedAlpha,
2329
);
24-
}
30+
};
2531

2632
return tinycolor({
2733
r: interpolateChannel("r"),
@@ -32,6 +38,10 @@ export function blendColors(base: string, top: string): string {
3238
}
3339

3440
export function blendMultipleColors(colors: string[]): string {
41+
if (colors.length === 0) {
42+
throw new Error("blendMultipleColors requires at least one color");
43+
}
44+
3545
let color = colors[0];
3646
for (let i = 1; i < colors.length; i++) {
3747
color = blendColors(color, colors[i]);

‎packages/common/src/util/stringUtils.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,25 @@ export function uriEncodeHashId(text: string): string {
4242
.replaceAll(" ", "-")
4343
.replace(/[^a-z0-9-]/g, "");
4444
}
45+
46+
/**
47+
* Converts a string to an integer, throwing an error if the string is not a valid integer.
48+
*
49+
* This function trims the input string and checks if it can be parsed as an integer.
50+
* If the trimmed string is not empty and can be parsed as an integer, the integer value is returned.
51+
* Otherwise, `undefined` is returned indicating that the input is not a valid integer.
52+
*
53+
* @param input The string to be converted to an integer
54+
* @returns The integer value represented by the input string, or `undefined` if the input is not a valid integer
55+
*/
56+
export function stringToInteger(input: string): number | undefined {
57+
const trimmedValue = input.trim();
58+
if (trimmedValue.length === 0) {
59+
return undefined;
60+
}
61+
const value = Number(trimmedValue);
62+
if (Number.isInteger(value)) {
63+
return value;
64+
}
65+
return undefined;
66+
}

‎packages/cursorless-engine/src/actions/EditNew/runNotebookCellTargets.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function runEditNewNotebookCellTargets(
1616

1717
if (destination.insertionMode === "to") {
1818
throw Error(
19-
`Unsupported insertion mode '${destination.insertionMode}' for notebookcapell`,
19+
`Unsupported insertion mode '${destination.insertionMode}' for notebook cell`,
2020
);
2121
}
2222

‎packages/cursorless-engine/src/core/IndividualHatMap.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class IndividualHatMap implements ReadOnlyHatMap {
6767
this.map = {};
6868
this.documentTokenLists = new Map();
6969
this.deregisterFunctions.forEach((func) => func());
70+
this.deregisterFunctions = [];
7071

7172
// Iterate through the hats in the new assignment, registering them with the
7273
// rangeUpdater so that their ranges stay up to date
@@ -124,5 +125,6 @@ export class IndividualHatMap implements ReadOnlyHatMap {
124125
dispose() {
125126
this.isExpired = true;
126127
this.deregisterFunctions.forEach((func) => func());
128+
this.deregisterFunctions = [];
127129
}
128130
}

‎packages/cursorless-engine/src/core/updateSelections/RangeUpdater.ts‎

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ import { updateRangeInfos } from "./updateRangeInfos";
2121
export class RangeUpdater {
2222
private rangeInfoLists: Map<string, FullRangeInfo[][]> = new Map();
2323
private replaceEditLists: Map<string, Edit[][]> = new Map();
24-
private disposable!: Disposable;
24+
private disposables: Disposable[] = [];
2525

2626
constructor() {
2727
this.listenForDocumentChanges();
2828
}
2929

30-
private getDocumentRangeInfoLists(document: TextDocument) {
31-
return getDefault(this.rangeInfoLists, document.uri.toString(), () => []);
32-
}
33-
34-
private getDocumentReplaceEditLists(document: TextDocument) {
35-
return getDefault(this.replaceEditLists, document.uri.toString(), () => []);
36-
}
37-
3830
/**
3931
* Registers a list of range infos to be kept up to date. It is ok to
4032
* add to this list after registering it; any items in the list at the time of
@@ -49,11 +41,26 @@ export class RangeUpdater {
4941
document: TextDocument,
5042
rangeInfoList: FullRangeInfo[],
5143
): () => void {
52-
const documentRangeInfoLists = this.getDocumentRangeInfoLists(document);
44+
const key = document.uri.toString();
45+
const documentRangeInfoLists = getDefault(
46+
this.rangeInfoLists,
47+
key,
48+
() => [],
49+
);
5350

5451
documentRangeInfoLists.push(rangeInfoList);
5552

56-
return () => pull(documentRangeInfoLists, rangeInfoList);
53+
return () => {
54+
pull(documentRangeInfoLists, rangeInfoList);
55+
// If there are no more lists for this document, remove the entry from the map to avoid memory leaks
56+
// Note that we check that the value in the map is the same array that we modified before deleting.
57+
if (
58+
documentRangeInfoLists.length === 0 &&
59+
this.rangeInfoLists.get(key) === documentRangeInfoLists
60+
) {
61+
this.rangeInfoLists.delete(key);
62+
}
63+
};
5764
}
5865

5966
/**
@@ -76,15 +83,29 @@ export class RangeUpdater {
7683
document: TextDocument,
7784
replaceEditList: Edit[],
7885
): () => void {
79-
const documentReplaceEditLists = this.getDocumentReplaceEditLists(document);
86+
const key = document.uri.toString();
87+
const documentReplaceEditLists = getDefault(
88+
this.replaceEditLists,
89+
key,
90+
() => [],
91+
);
8092

8193
documentReplaceEditLists.push(replaceEditList);
8294

83-
return () => pull(documentReplaceEditLists, replaceEditList);
95+
return () => {
96+
pull(documentReplaceEditLists, replaceEditList);
97+
if (
98+
documentReplaceEditLists.length === 0 &&
99+
this.replaceEditLists.get(key) === documentReplaceEditLists
100+
) {
101+
this.replaceEditLists.delete(key);
102+
}
103+
};
84104
}
85105

86106
private *documentRangeInfoGenerator(document: TextDocument) {
87-
const documentRangeInfoLists = this.getDocumentRangeInfoLists(document);
107+
const documentRangeInfoLists =
108+
this.rangeInfoLists.get(document.uri.toString()) ?? [];
88109

89110
for (const rangeInfoLists of documentRangeInfoLists) {
90111
for (const rangeInfo of rangeInfoLists) {
@@ -94,11 +115,10 @@ export class RangeUpdater {
94115
}
95116

96117
private listenForDocumentChanges() {
97-
this.disposable = ide().onDidChangeTextDocument(
98-
(event: TextDocumentChangeEvent) => {
99-
const documentReplaceEditLists = this.getDocumentReplaceEditLists(
100-
event.document,
101-
);
118+
this.disposables.push(
119+
ide().onDidChangeTextDocument((event: TextDocumentChangeEvent) => {
120+
const documentReplaceEditLists =
121+
this.replaceEditLists.get(event.document.uri.toString()) ?? [];
102122

103123
const extendedEvent: ExtendedTextDocumentChangeEvent = {
104124
...event,
@@ -116,12 +136,18 @@ export class RangeUpdater {
116136
extendedEvent,
117137
this.documentRangeInfoGenerator(event.document),
118138
);
119-
},
139+
}),
140+
141+
ide().onDidCloseTextDocument((document) => {
142+
const key = document.uri.toString();
143+
this.rangeInfoLists.delete(key);
144+
this.replaceEditLists.delete(key);
145+
}),
120146
);
121147
}
122148

123149
dispose() {
124-
this.disposable.dispose();
150+
this.disposables.forEach((disposable) => disposable.dispose());
125151
}
126152
}
127153

‎packages/cursorless-engine/src/core/updateSelections/updateSelections.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,12 @@ async function callFunctionAndUpdateFullSelectionInfos(
205205
flatten(originalSelectionInfos),
206206
);
207207

208-
await func();
209-
210-
unsubscribe();
211-
212-
return selectionInfosToSelections(originalSelectionInfos);
208+
try {
209+
await func();
210+
return selectionInfosToSelections(originalSelectionInfos);
211+
} finally {
212+
unsubscribe();
213+
}
213214
}
214215

215216
/**

‎packages/cursorless-engine/src/cursorlessEngine.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export async function createCursorlessEngine({
9090
hatTokenMap,
9191
debug,
9292
keyboardTargetUpdater,
93+
customSpokenFormGenerator,
9394
);
9495

9596
const commandRunnerDecorators: CommandRunnerDecorator[] = [];
@@ -131,6 +132,7 @@ export async function createCursorlessEngine({
131132
},
132133
},
133134
scopeProvider: createScopeProvider(
135+
ide,
134136
languageDefinitions,
135137
storedTargets,
136138
customSpokenFormGenerator,
@@ -146,6 +148,7 @@ export async function createCursorlessEngine({
146148
}
147149

148150
function createScopeProvider(
151+
ide: IDE,
149152
languageDefinitions: LanguageDefinitions,
150153
storedTargets: StoredTargetMap,
151154
customSpokenFormGenerator: CustomSpokenFormGeneratorImpl,
@@ -172,6 +175,7 @@ function createScopeProvider(
172175
supportChecker,
173176
infoProvider,
174177
);
178+
ide.disposeOnExit(rangeWatcher, infoProvider, supportWatcher);
175179

176180
return {
177181
provideScopeRanges: rangeProvider.provideScopeRanges,

0 commit comments

Comments
 (0)