Skip to content

Commit da3aec1

Browse files
Reuse same editor for tests (#3216)
Opening a new editor for each test case wastes a lot of performance. This change reuses the same editor as much as possible.
1 parent 86da9f4 commit da3aec1

28 files changed

Lines changed: 168 additions & 129 deletions

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,8 @@ export class LanguageDefinitionsImpl implements LanguageDefinitions {
5959
private treeSitter: TreeSitter,
6060
private treeSitterQueryProvider: RawTreeSitterQueryProvider,
6161
) {
62-
const isTesting = ide.runMode === "test";
63-
6462
this.disposables.push(
6563
ide.onDidOpenTextDocument((document) => {
66-
// During testing we open untitled documents that all have the same uri and version which breaks our cache
67-
if (isTesting) {
68-
treeSitterQueryCache.clear();
69-
}
7064
this.loadLanguage(document.languageId).catch((err) => {
7165
void showError(
7266
this.ide.messages,

packages/cursorless-vscode-e2e/src/suite/backwardCompatibility.vscode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CURSORLESS_COMMAND_ID } from "@cursorless/common";
2-
import { getCursorlessApi, openNewEditor } from "@cursorless/vscode-common";
2+
import { getCursorlessApi, getReusableEditor } from "@cursorless/vscode-common";
33
import * as assert from "assert";
44
import * as vscode from "vscode";
55
import { endToEndTestSetup } from "../endToEndTestSetup";
@@ -13,7 +13,7 @@ suite("Backward compatibility", async function () {
1313
async function runTest() {
1414
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
1515

16-
const editor = await openNewEditor("");
16+
const editor = await getReusableEditor("");
1717

1818
editor.selections = [new vscode.Selection(0, 0, 0, 0)];
1919

packages/cursorless-vscode-e2e/src/suite/breakpoints.vscode.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LATEST_VERSION } from "@cursorless/common";
22
import {
33
getCursorlessApi,
4-
openNewEditor,
4+
getReusableEditor,
55
runCursorlessCommand,
66
} from "@cursorless/vscode-common";
77
import * as assert from "assert";
@@ -27,7 +27,7 @@ suite("breakpoints", async function () {
2727

2828
async function breakpointAdd() {
2929
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
30-
await openNewEditor(" hello");
30+
await getReusableEditor(" hello");
3131
await hatTokenMap.allocateHats();
3232
await toggleBreakpoint();
3333

@@ -40,7 +40,7 @@ async function breakpointAdd() {
4040

4141
async function breakpointTokenAdd() {
4242
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
43-
await openNewEditor(" hello");
43+
await getReusableEditor(" hello");
4444
await hatTokenMap.allocateHats();
4545
await toggleTokenBreakpoint();
4646

@@ -53,7 +53,7 @@ async function breakpointTokenAdd() {
5353

5454
async function breakpointRemove() {
5555
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
56-
const editor = await openNewEditor(" hello");
56+
const editor = await getReusableEditor(" hello");
5757
await hatTokenMap.allocateHats();
5858

5959
vscode.debug.addBreakpoints([
@@ -71,7 +71,7 @@ async function breakpointRemove() {
7171

7272
async function breakpointTokenRemove() {
7373
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
74-
const editor = await openNewEditor(" hello");
74+
const editor = await getReusableEditor(" hello");
7575
await hatTokenMap.allocateHats();
7676

7777
vscode.debug.addBreakpoints([

packages/cursorless-vscode-e2e/src/suite/containingTokenTwice.vscode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LATEST_VERSION } from "@cursorless/common";
22
import {
33
getCursorlessApi,
4-
openNewEditor,
4+
getReusableEditor,
55
runCursorlessCommand,
66
} from "@cursorless/vscode-common";
77
import { assert } from "chai";
@@ -19,7 +19,7 @@ suite("Take token twice", async function () {
1919

2020
async function runTest() {
2121
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
22-
const editor = await openNewEditor("a)");
22+
const editor = await getReusableEditor("a)");
2323
await hatTokenMap.allocateHats();
2424

2525
for (let i = 0; i < 2; ++i) {

packages/cursorless-vscode-e2e/src/suite/explicitMark.vscode.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { LATEST_VERSION } from "@cursorless/common";
22
import {
33
getCursorlessApi,
4-
openNewEditor,
4+
getReusableEditor,
55
runCursorlessCommand,
66
} from "@cursorless/vscode-common";
77
import * as assert from "assert";
@@ -16,7 +16,7 @@ suite("Explicit mark", async function () {
1616

1717
async function explicitMark() {
1818
const { ide } = (await getCursorlessApi()).testHelpers!;
19-
const editor = await openNewEditor("foo bar baz");
19+
const editor = await getReusableEditor("foo bar baz");
2020
const editorId = ide.visibleTextEditors[0].id;
2121

2222
await runCursorlessCommand({

packages/cursorless-vscode-e2e/src/suite/fold.vscode.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { LATEST_VERSION } from "@cursorless/common";
2-
import { openNewEditor, runCursorlessCommand } from "@cursorless/vscode-common";
2+
import {
3+
getReusableEditor,
4+
runCursorlessCommand,
5+
} from "@cursorless/vscode-common";
36
import * as assert from "node:assert";
47
import * as vscode from "vscode";
58
import { endToEndTestSetup } from "../endToEndTestSetup";
@@ -12,9 +15,10 @@ suite("fold", async function () {
1215
});
1316

1417
async function foldMade() {
15-
const editor = await openNewEditor("function myFunk() {\n\n}", {
16-
languageId: "typescript",
17-
});
18+
const editor = await getReusableEditor(
19+
"function myFunk() {\n\n}",
20+
"typescript",
21+
);
1822

1923
await runCursorlessCommand({
2024
version: LATEST_VERSION,
@@ -38,9 +42,10 @@ async function foldMade() {
3842
}
3943

4044
async function unfoldMade() {
41-
const editor = await openNewEditor("function myFunk() {\n\n}", {
42-
languageId: "typescript",
43-
});
45+
const editor = await getReusableEditor(
46+
"function myFunk() {\n\n}",
47+
"typescript",
48+
);
4449
await vscode.commands.executeCommand("editor.fold", {
4550
selectionLines: [0],
4651
});

packages/cursorless-vscode-e2e/src/suite/followLink.vscode.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { LATEST_VERSION } from "@cursorless/common";
12
import { getFixturePath, isWindows } from "@cursorless/node-common";
2-
import { openNewEditor, runCursorlessCommand } from "@cursorless/vscode-common";
3+
import {
4+
getReusableEditor,
5+
runCursorlessCommand,
6+
} from "@cursorless/vscode-common";
37
import * as assert from "assert";
48
import * as vscode from "vscode";
59
import { endToEndTestSetup } from "../endToEndTestSetup";
6-
import { LATEST_VERSION } from "@cursorless/common";
710

811
suite("followLink", async function () {
912
endToEndTestSetup(this);
@@ -13,9 +16,10 @@ suite("followLink", async function () {
1316
});
1417

1518
async function followDefinition() {
16-
const editor = await openNewEditor("const foo = 'hello';\nconst bar = foo;", {
17-
languageId: "typescript",
18-
});
19+
const editor = await getReusableEditor(
20+
"const foo = 'hello';\nconst bar = foo;",
21+
"typescript",
22+
);
1923
await vscode.commands.executeCommand("revealLine", {
2024
lineNumber: 1,
2125
at: "top",
@@ -48,7 +52,7 @@ async function followLink() {
4852
const linkTextContent = isWindows()
4953
? `file:///${filename}`
5054
: `file://${filename}`;
51-
await openNewEditor(linkTextContent);
55+
await getReusableEditor(linkTextContent);
5256

5357
await runCursorlessCommand({
5458
version: LATEST_VERSION,

packages/cursorless-vscode-e2e/src/suite/instanceAcrossSplit.vscode.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ async function runTest(
103103
/** The editor containing the "instance" */
104104
const instanceEditor = spyIde.activeTextEditor!;
105105
/** The editor in which "from" is run */
106-
const fromEditor = await openNewEditor(" aaa bbb aaa aaa", {
107-
openBeside: true,
108-
});
106+
const fromEditor = await openNewEditor(" aaa bbb aaa aaa", "plaintext", true);
109107
const { document: fromDocument } = fromEditor;
110108
fromEditor.selections = [new Selection(0, 0, 0, 0)];
111109

packages/cursorless-vscode-e2e/src/suite/keyboard/basic.vscode.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getCursorlessApi, openNewEditor } from "@cursorless/vscode-common";
1+
import {
2+
getCursorlessApi,
3+
getReusableEditor,
4+
openNewEditor,
5+
} from "@cursorless/vscode-common";
26
import { assert } from "chai";
37
import * as vscode from "vscode";
48
import { endToEndTestSetup, sleepWithBackoff } from "../../endToEndTestSetup";
@@ -191,7 +195,7 @@ suite("Basic keyboard test", async function () {
191195

192196
async function checkKeyboardStartup() {
193197
await getCursorlessApi();
194-
const editor = await openNewEditor("");
198+
const editor = await getReusableEditor("");
195199

196200
// Type the letter
197201
await typeText("a");
@@ -202,9 +206,7 @@ async function checkKeyboardStartup() {
202206
async function basic() {
203207
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
204208

205-
const editor = await openNewEditor("function foo() {}\n", {
206-
languageId: "typescript",
207-
});
209+
const editor = await getReusableEditor("function foo() {}\n", "typescript");
208210
await hatTokenMap.allocateHats();
209211

210212
editor.selection = new vscode.Selection(1, 0, 1, 0);
@@ -232,7 +234,7 @@ async function basic() {
232234
async function noAutomaticTokenExpansion() {
233235
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
234236

235-
const editor = await openNewEditor("aaa");
237+
const editor = await getReusableEditor("aaa");
236238
await hatTokenMap.allocateHats();
237239

238240
editor.selection = new vscode.Selection(0, 3, 0, 3);
@@ -251,9 +253,8 @@ async function noAutomaticTokenExpansion() {
251253
async function sequence(t: TestCase) {
252254
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
253255

254-
const editor = await openNewEditor(t.initialContent, {
255-
languageId: "typescript",
256-
});
256+
// This test fails if we use getReusableEditor()
257+
const editor = await openNewEditor(t.initialContent, "typescript");
257258
await hatTokenMap.allocateHats();
258259
editor.selection = new vscode.Selection(1, 0, 1, 0);
259260
await vscode.commands.executeCommand("cursorless.keyboard.modal.modeOn");
@@ -264,9 +265,7 @@ async function sequence(t: TestCase) {
264265
async function vscodeCommand() {
265266
const { hatTokenMap } = (await getCursorlessApi()).testHelpers!;
266267

267-
const editor = await openNewEditor("aaa;\nbbb;\nccc;\n", {
268-
languageId: "typescript",
269-
});
268+
const editor = await getReusableEditor("aaa;\nbbb;\nccc;\n", "typescript");
270269
await hatTokenMap.allocateHats();
271270

272271
editor.selection = new vscode.Selection(0, 0, 0, 0);
@@ -293,7 +292,7 @@ async function vscodeCommand() {
293292
}
294293

295294
async function enterAndLeaveIsNoOp() {
296-
const editor = await openNewEditor("hello");
295+
const editor = await getReusableEditor("hello");
297296

298297
const originalSelection = new vscode.Selection(0, 0, 0, 0);
299298
editor.selection = originalSelection;

packages/cursorless-vscode-e2e/src/suite/performance.vscode.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type {
55
SimpleScopeTypeType,
66
} from "@cursorless/common";
77
import { asyncSafety } from "@cursorless/common";
8-
import { openNewEditor, runCursorlessAction } from "@cursorless/vscode-common";
8+
import {
9+
getReusableEditor,
10+
runCursorlessAction,
11+
} from "@cursorless/vscode-common";
912
import assert from "assert";
1013
import * as vscode from "vscode";
1114
import { endToEndTestSetup } from "../endToEndTestSetup";
@@ -212,7 +215,7 @@ async function testPerformanceCallback(
212215
callback: () => Promise<unknown>,
213216
beforeCallback?: (editor: vscode.TextEditor) => Promise<unknown>,
214217
) {
215-
const editor = await openNewEditor(testData, { languageId: "json" });
218+
const editor = await getReusableEditor(testData, "json");
216219
// This is the position of the last json key in the document
217220
const position = new vscode.Position(editor.document.lineCount - 3, 5);
218221
const selection = new vscode.Selection(position, position);

0 commit comments

Comments
 (0)