Fix that mark for rewrap action (#406)

This commit is contained in:
Pokey Rule 2021-12-18 23:01:23 +00:00 committed by GitHub
parent 9f75b9d8b6
commit a9d9abf075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 47 additions and 19 deletions

View File

@ -1,4 +1,6 @@
import { flatten, zip } from "lodash";
import { TextEditor } from "vscode";
import { performEditsAndUpdateSelections } from "../core/updateSelections/updateSelections";
import {
Action,
ActionPreferences,
@ -8,6 +10,8 @@ import {
TypedSelection,
} from "../typings/Types";
import { repeat } from "../util/array";
import displayPendingEditDecorations from "../util/editDisplayUtils";
import { runForEachEditor } from "../util/targetUtils";
export default class Rewrap implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [
@ -25,26 +29,62 @@ export default class Rewrap implements Action {
this.run = this.run.bind(this);
}
run(
async run(
[targets]: [TypedSelection[]],
left: string,
right: string
): Promise<ActionReturnValue> {
const boundaries: TypedSelection[] = targets.flatMap((target) => {
const targetInfos = targets.flatMap((target) => {
const boundary = target.selectionContext.boundary;
if (boundary == null || boundary.length !== 2) {
throw Error("Target must have an opening and closing delimiter");
}
return boundary.map((edge) =>
constructSimpleTypedSelection(target.selection.editor, edge)
);
return {
editor: target.selection.editor,
boundary: boundary.map((edge) =>
constructSimpleTypedSelection(target.selection.editor, edge)
),
targetSelection: target.selection.selection,
};
});
const replacementTexts = repeat([left, right], targets.length);
await displayPendingEditDecorations(
targetInfos.flatMap(({ boundary }) => boundary),
this.graph.editStyles.pendingModification0
);
return this.graph.actions.replace.run([boundaries], replacementTexts);
const thatMark = flatten(
await runForEachEditor(
targetInfos,
(targetInfo) => targetInfo.editor,
async (editor, targetInfos) => {
const edits = targetInfos.flatMap((targetInfo) =>
zip(targetInfo.boundary, [left, right]).map(([target, text]) => ({
editor,
range: target!.selection.selection,
text: text!,
}))
);
const [updatedTargetSelections] =
await performEditsAndUpdateSelections(
this.graph.rangeUpdater,
editor,
edits,
[targetInfos.map((targetInfo) => targetInfo.targetSelection)]
);
return updatedTargetSelections.map((selection) => ({
editor,
selection,
}));
}
)
);
return { thatMark };
}
}

View File

@ -28,11 +28,7 @@ finalState:
active: {line: 1, character: 5}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 1}
- anchor: {line: 0, character: 8}
active: {line: 0, character: 9}
- anchor: {line: 1, character: 0}
active: {line: 1, character: 1}
- anchor: {line: 1, character: 6}
active: {line: 1, character: 7}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: parentheses}}]

View File

@ -25,7 +25,5 @@ finalState:
active: {line: 1, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 1}
- anchor: {line: 0, character: 6}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: h}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}]

View File

@ -25,7 +25,5 @@ finalState:
active: {line: 1, character: 0}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 1}
- anchor: {line: 0, character: 6}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: decoratedSymbol, symbolColor: default, character: (}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}]

View File

@ -22,7 +22,5 @@ finalState:
active: {line: 0, character: 2}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 1}
- anchor: {line: 0, character: 6}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}]

View File

@ -20,7 +20,5 @@ finalState:
active: {line: 0, character: 4}
thatMark:
- anchor: {line: 0, character: 0}
active: {line: 0, character: 1}
- anchor: {line: 0, character: 6}
active: {line: 0, character: 7}
fullTargets: [{type: primitive, mark: {type: cursor}, selectionType: token, position: contents, insideOutsideType: inside, modifier: {type: surroundingPair, delimiter: any}}]