Skip to content

Use built in action for copy and paste #141

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 4 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/CommandAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import { runOnTargetsForEachEditor } from "./targetUtils";
import { focusEditor } from "./setSelectionsAndFocusEditor";
import { flatten } from "lodash";
import { callFunctionAndUpdateSelections } from "./updateSelections";
import { ensureSingleEditor } from "./targetUtils";

export default class CommandAction implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];
private ensureSingleEditor: boolean;

constructor(private graph: Graph, private command: string) {
constructor(
private graph: Graph,
private command: string,
{ ensureSingleEditor = false } = {}
) {
this.run = this.run.bind(this);
this.ensureSingleEditor = ensureSingleEditor;
}

private async runCommandAndUpdateSelections(targets: TypedSelection[]) {
Expand Down Expand Up @@ -57,14 +64,20 @@ export default class CommandAction implements Action {
);
}

async run([targets]: [
TypedSelection[],
TypedSelection[]
]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced
);
async run(
[targets]: [TypedSelection[]],
{ showDecorations = true } = {}
): Promise<ActionReturnValue> {
if (showDecorations) {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.referenced
);
}

if (this.ensureSingleEditor) {
ensureSingleEditor(targets);
}

const originalEditor = window.activeTextEditor;

Expand Down
70 changes: 70 additions & 0 deletions src/actions/CutCopyPaste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../Types";
import { performInsideOutsideAdjustment } from "../performInsideOutsideAdjustment";
import CommandAction from "../CommandAction";
import displayPendingEditDecorations from "../editDisplayUtils";
import { getOutsideOverflow } from "../targetUtils";
import { zip } from "lodash";

export class Cut implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: null }];

constructor(private graph: Graph) {
this.run = this.run.bind(this);
}

async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
const insideTargets = targets.map((target) =>
performInsideOutsideAdjustment(target, "inside")
);
const outsideTargets = targets.map((target) =>
performInsideOutsideAdjustment(target, "outside")
);
const outsideTargetDecorations = zip(insideTargets, outsideTargets).flatMap(
([inside, outside]) => getOutsideOverflow(inside!, outside!)
);
const options = { showDecorations: false };

await Promise.all([
displayPendingEditDecorations(
insideTargets,
this.graph.editStyles.referenced
),
displayPendingEditDecorations(
outsideTargetDecorations,
this.graph.editStyles.pendingDelete
),
]);

await this.graph.actions.copy.run([insideTargets], options);

const { thatMark } = await this.graph.actions.delete.run(
[outsideTargets],
options
);

return {
returnValue: null,
thatMark,
};
}
}

const OPTIONS = { ensureSingleEditor: true };

export class Copy extends CommandAction {
constructor(graph: Graph) {
super(graph, "editor.action.clipboardCopyAction", OPTIONS);
}
}

export class Paste extends CommandAction {
constructor(graph: Graph) {
super(graph, "editor.action.clipboardPasteAction", OPTIONS);
}
}
84 changes: 0 additions & 84 deletions src/actions/Paste.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/actions/copy.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/actions/cut.ts

This file was deleted.

15 changes: 10 additions & 5 deletions src/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ export default class Delete implements Action {
this.run = this.run.bind(this);
}

async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.pendingDelete,
);
async run(
[targets]: [TypedSelection[]],
{ showDecorations = true } = {}
): Promise<ActionReturnValue> {
if (showDecorations) {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.pendingDelete
);
}

const thatMark = flatten(
await runOnTargetsForEachEditor(targets, async (editor, targets) => {
Expand Down
4 changes: 1 addition & 3 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ActionRecord, Graph } from "../Types";
import Clear from "./clear";
import Copy from "./copy";
import Cut from "./cut";
import { Cut, Copy, Paste } from "./CutCopyPaste";
import Delete from "./delete";
import ExtractVariable from "./extractVariable";
import { Fold, Unfold } from "./fold";
Expand All @@ -15,7 +14,6 @@ import Wrap from "./wrap";
import { ScrollToTop, ScrollToCenter, ScrollToBottom } from "./Scroll";
import { IndentLines, OutdentLines } from "./Indent";
import { CommentLines } from "./Comment";
import Paste from "./Paste";
import { Bring, Move, Swap } from "./BringMoveSwap";
import {
InsertEmptyLineAbove,
Expand Down
47 changes: 46 additions & 1 deletion src/targetUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TextEditor } from "vscode";
import { TextEditor, Selection, Position } from "vscode";
import { groupBy } from "./itertools";
import { TypedSelection } from "./Types";
import update from "immutability-helper";

export function ensureSingleEditor(targets: TypedSelection[]) {
if (targets.length === 0) {
Expand Down Expand Up @@ -42,3 +43,47 @@ export async function runOnTargetsForEachEditor<T>(
): Promise<T[]> {
return runForEachEditor(targets, (target) => target.selection.editor, func);
}

/** Get the possible leading and trailing overflow ranges of the outside target compared to the inside target */
export function getOutsideOverflow(
insideTarget: TypedSelection,
outsideTarget: TypedSelection
): TypedSelection[] {
const { start: insideStart, end: insideEnd } =
insideTarget.selection.selection;
const { start: outsideStart, end: outsideEnd } =
outsideTarget.selection.selection;
const result = [];
if (outsideStart.isBefore(insideStart)) {
result.push(
createTypeSelection(
insideTarget.selection.editor,
outsideStart,
insideStart
)
);
}
if (outsideEnd.isAfter(insideEnd)) {
result.push(
createTypeSelection(insideTarget.selection.editor, insideEnd, outsideEnd)
);
}
return result;
}

function createTypeSelection(
editor: TextEditor,
start: Position,
end: Position
): TypedSelection {
return {
selection: {
editor,
selection: new Selection(start, end),
},
selectionType: "token",
selectionContext: {},
insideOutsideType: "inside",
position: "contents",
};
}