Skip to content

Group by document and not editor #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 17, 2022
76 changes: 76 additions & 0 deletions src/test/suite/groupByDocument.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as assert from "assert";
import * as vscode from "vscode";
import * as sinon from "sinon";
import { getCursorlessApi } from "../../util/getExtensionApi";
import HatTokenMap from "../../core/HatTokenMap";

suite("Group by document", async function () {
this.timeout("100s");
this.retries(3);

teardown(() => {
sinon.restore();
});

test("Group by document", runTest);
});

async function runTest() {
const graph = (await getCursorlessApi()).graph!;

await vscode.commands.executeCommand("workbench.action.closeAllEditors");

const document = await vscode.workspace.openTextDocument({
language: "plaintext",
content: "hello world",
});

const editor1 = await vscode.window.showTextDocument(document);
const editor2 = await vscode.window.showTextDocument(
document,
vscode.ViewColumn.Beside
);

await graph.hatTokenMap.addDecorations();
const hatMap = await graph.hatTokenMap.getReadableMap(false);

const hat1 = hatMap
.getEntries()
.find(([, token]) => token.editor === editor1 && token.text === "hello");
const hat2 = hatMap
.getEntries()
.find(([, token]) => token.editor === editor2 && token.text === "world");

const { hatStyle: hatStyle1, character: char1 } = HatTokenMap.splitKey(
hat1![0]
);
const { hatStyle: hatStyle2, character: char2 } = HatTokenMap.splitKey(
hat2![0]
);

await vscode.commands.executeCommand(
"cursorless.command",
"swap each with whale",
"swapTargets",
[
{
type: "primitive",
mark: {
type: "decoratedSymbol",
symbolColor: hatStyle1,
character: char1,
},
},
{
type: "primitive",
mark: {
type: "decoratedSymbol",
symbolColor: hatStyle2,
character: char2,
},
},
]
);

assert.deepStrictEqual(document.getText(), "world hello");
}
11 changes: 8 additions & 3 deletions src/util/targetUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@ export async function runForEachEditor<T, U>(
getEditor: (target: T) => TextEditor,
func: (editor: TextEditor, editorTargets: T[]) => Promise<U>
): Promise<U[]> {
// 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.
const getDocument = (target: T) => getEditor(target).document;
const editorMap = groupBy(targets, getDocument);
return await Promise.all(
Array.from(groupBy(targets, getEditor), async ([editor, editorTargets]) =>
func(editor, editorTargets)
)
Array.from(editorMap.values(), async (editorTargets) => {
// Just pick any editor with the given document open; doesn't matter which
const editor = getEditor(editorTargets[0]);
return func(editor, editorTargets);
})
);
}

Expand Down