From a9d9abf075aa188ddf1b4e9c25cb4cc728b5b8fd Mon Sep 17 00:00:00 2001 From: Pokey Rule Date: Sat, 18 Dec 2021 23:01:23 +0000 Subject: [PATCH] Fix that mark for rewrap action (#406) --- src/actions/Rewrap.ts | 54 ++++++++++++++++--- .../recorded/actions/curlyRepackRound.yml | 4 -- .../recorded/actions/squareRepackHarp.yml | 2 - .../recorded/actions/squareRepackLeper.yml | 2 - .../recorded/actions/squareRepackPair.yml | 2 - .../recorded/actions/squareRepackThis.yml | 2 - 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/actions/Rewrap.ts b/src/actions/Rewrap.ts index 475fa471f..1843a7ff5 100644 --- a/src/actions/Rewrap.ts +++ b/src/actions/Rewrap.ts @@ -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 { - 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 }; } } diff --git a/src/test/suite/fixtures/recorded/actions/curlyRepackRound.yml b/src/test/suite/fixtures/recorded/actions/curlyRepackRound.yml index 79f8a5c96..ea183bad7 100644 --- a/src/test/suite/fixtures/recorded/actions/curlyRepackRound.yml +++ b/src/test/suite/fixtures/recorded/actions/curlyRepackRound.yml @@ -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}}] diff --git a/src/test/suite/fixtures/recorded/actions/squareRepackHarp.yml b/src/test/suite/fixtures/recorded/actions/squareRepackHarp.yml index db35d3a24..531856ea3 100644 --- a/src/test/suite/fixtures/recorded/actions/squareRepackHarp.yml +++ b/src/test/suite/fixtures/recorded/actions/squareRepackHarp.yml @@ -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}}] diff --git a/src/test/suite/fixtures/recorded/actions/squareRepackLeper.yml b/src/test/suite/fixtures/recorded/actions/squareRepackLeper.yml index c31058814..4195092d7 100644 --- a/src/test/suite/fixtures/recorded/actions/squareRepackLeper.yml +++ b/src/test/suite/fixtures/recorded/actions/squareRepackLeper.yml @@ -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}}] diff --git a/src/test/suite/fixtures/recorded/actions/squareRepackPair.yml b/src/test/suite/fixtures/recorded/actions/squareRepackPair.yml index 4240ef604..b61acd753 100644 --- a/src/test/suite/fixtures/recorded/actions/squareRepackPair.yml +++ b/src/test/suite/fixtures/recorded/actions/squareRepackPair.yml @@ -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}}] diff --git a/src/test/suite/fixtures/recorded/actions/squareRepackThis.yml b/src/test/suite/fixtures/recorded/actions/squareRepackThis.yml index d01e15621..3e91e57bd 100644 --- a/src/test/suite/fixtures/recorded/actions/squareRepackThis.yml +++ b/src/test/suite/fixtures/recorded/actions/squareRepackThis.yml @@ -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}}]