Skip to content

Commit c79afbb

Browse files
Group by document and not editor (#382)
* Group by document and not editor * Updated comment * Update src/util/targetUtils.ts Co-authored-by: Pokey Rule <[email protected]> * Change comment into correct format for typescript * Added test * Use hat token maps split key Co-authored-by: Pokey Rule <[email protected]>
1 parent 6cb9dea commit c79afbb

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import * as assert from "assert";
2+
import * as vscode from "vscode";
3+
import * as sinon from "sinon";
4+
import { getCursorlessApi } from "../../util/getExtensionApi";
5+
import HatTokenMap from "../../core/HatTokenMap";
6+
7+
suite("Group by document", async function () {
8+
this.timeout("100s");
9+
this.retries(3);
10+
11+
teardown(() => {
12+
sinon.restore();
13+
});
14+
15+
test("Group by document", runTest);
16+
});
17+
18+
async function runTest() {
19+
const graph = (await getCursorlessApi()).graph!;
20+
21+
await vscode.commands.executeCommand("workbench.action.closeAllEditors");
22+
23+
const document = await vscode.workspace.openTextDocument({
24+
language: "plaintext",
25+
content: "hello world",
26+
});
27+
28+
const editor1 = await vscode.window.showTextDocument(document);
29+
const editor2 = await vscode.window.showTextDocument(
30+
document,
31+
vscode.ViewColumn.Beside
32+
);
33+
34+
await graph.hatTokenMap.addDecorations();
35+
const hatMap = await graph.hatTokenMap.getReadableMap(false);
36+
37+
const hat1 = hatMap
38+
.getEntries()
39+
.find(([, token]) => token.editor === editor1 && token.text === "hello");
40+
const hat2 = hatMap
41+
.getEntries()
42+
.find(([, token]) => token.editor === editor2 && token.text === "world");
43+
44+
const { hatStyle: hatStyle1, character: char1 } = HatTokenMap.splitKey(
45+
hat1![0]
46+
);
47+
const { hatStyle: hatStyle2, character: char2 } = HatTokenMap.splitKey(
48+
hat2![0]
49+
);
50+
51+
await vscode.commands.executeCommand(
52+
"cursorless.command",
53+
"swap each with whale",
54+
"swapTargets",
55+
[
56+
{
57+
type: "primitive",
58+
mark: {
59+
type: "decoratedSymbol",
60+
symbolColor: hatStyle1,
61+
character: char1,
62+
},
63+
},
64+
{
65+
type: "primitive",
66+
mark: {
67+
type: "decoratedSymbol",
68+
symbolColor: hatStyle2,
69+
character: char2,
70+
},
71+
},
72+
]
73+
);
74+
75+
assert.deepStrictEqual(document.getText(), "world hello");
76+
}

src/util/targetUtils.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ export async function runForEachEditor<T, U>(
2929
getEditor: (target: T) => TextEditor,
3030
func: (editor: TextEditor, editorTargets: T[]) => Promise<U>
3131
): Promise<U[]> {
32+
// Actually group by document and not editor. If the same document is open in multiple editors we want to perform all actions in one editor or an concurrency error will occur.
33+
const getDocument = (target: T) => getEditor(target).document;
34+
const editorMap = groupBy(targets, getDocument);
3235
return await Promise.all(
33-
Array.from(groupBy(targets, getEditor), async ([editor, editorTargets]) =>
34-
func(editor, editorTargets)
35-
)
36+
Array.from(editorMap.values(), async (editorTargets) => {
37+
// Just pick any editor with the given document open; doesn't matter which
38+
const editor = getEditor(editorTargets[0]);
39+
return func(editor, editorTargets);
40+
})
3641
);
3742
}
3843

0 commit comments

Comments
 (0)