Skip to content
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

Implemented paste action #115

Merged
merged 2 commits into from
Jul 21, 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
107 changes: 107 additions & 0 deletions src/actions/Paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../Types";
import displayPendingEditDecorations from "../editDisplayUtils";
import { env, Selection } from "vscode";
import { runForEachEditor } from "../targetUtils";
import { computeChangedOffsets } from "../computeChangedOffsets";
import { flatten, zip } from "lodash";
import update from "immutability-helper";

export default class Paste implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];

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

async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
await displayPendingEditDecorations(
targets,
this.graph.editStyles.pendingModification0,
this.graph.editStyles.pendingLineModification0
);

const text = await env.clipboard.readText();
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved

if (text.length === 0) {
throw new Error("Can't paste empty clipboard");
}

const lines = text.trim().split("\n");

const getText =
targets.length === lines.length
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we decided to match VSCode behaviour and check that number of lines is divisible by number of targets and if so split clipboard into blocks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what line 37 does

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That checks equality, not divisibility, no? What am I missing?

Copy link
Member Author

@AndreasArvidsson AndreasArvidsson Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is how vscode paste work. It only splits the clipboard if they are the same amount/length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope! Try it out. Copy 4 lines, then make 2 cursors and paste. It will split up the 4 lines into 2 blocks of 2

Copy link
Member Author

@AndreasArvidsson AndreasArvidsson Jul 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not on windows at least. I tried it multiple times before I made the implementation and I tried it once again now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha wait really? That's really odd that that would differ on platforms. I'll try it one more time today; maybe I messed something up in my testing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Really strange if it differs.

? // Paste each line on each target
(index: number) => lines[index]
: // Paste entire clipboard on each target
() => text;

const edits = targets.map((target, index) => ({
editor: target.selection.editor,
range: target.selection.selection,
originalSelection: target,
newText: getText(index),
}));

const thatMark = flatten(
await runForEachEditor(
edits,
(edit) => edit.editor,
async (editor, edits) => {
const newEdits = zip(edits, computeChangedOffsets(editor, edits)).map(
([originalEdit, changedEdit]) => ({
originalRange: originalEdit!.range,
originalSelection: originalEdit!.originalSelection,
newText: originalEdit!.newText,
newStartOffset: changedEdit!.startOffset,
newEndOffset: changedEdit!.endOffset,
})
);

await editor.edit((editBuilder) => {
newEdits.forEach((edit) => {
if (edit.originalRange.isEmpty) {
editBuilder.insert(edit.originalRange.start, edit.newText);
} else {
editBuilder.replace(edit.originalRange, edit.newText);
}
});
});

return newEdits.map((edit) => {
const start = editor.document.positionAt(edit.newStartOffset);
const end = editor.document.positionAt(edit.newEndOffset);
const isReversed =
edit.originalSelection.selection.selection.isReversed;
const selection = new Selection(
isReversed ? end : start,
isReversed ? start : end
);
return {
editor,
typedSelection: update(edit.originalSelection, {
selection: {
selection: { $set: selection },
},
}),
selection,
};
});
AndreasArvidsson marked this conversation as resolved.
Show resolved Hide resolved
}
)
);

await displayPendingEditDecorations(
thatMark.map(({ typedSelection }) => typedSelection),
this.graph.editStyles.pendingModification0,
this.graph.editStyles.pendingLineModification0
);

return { returnValue: null, thatMark };
}
}
8 changes: 2 additions & 6 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "./setSelection";
import Wrap from "./wrap";
import { ScrollToTop, ScrollToCenter, ScrollToBottom } from "./Scroll";
import Paste from "./Paste";
import { Bring, Move, Swap } from "./BringMoveSwap";

class Actions implements ActionRecord {
Expand All @@ -31,12 +32,7 @@ class Actions implements ActionRecord {
insertLineBefore = new InsertLineBefore(this.graph);
insertLineAfter = new InsertLineAfter(this.graph);
move = new Move(this.graph);
paste: Action = {
run: async ([targets]) => {
throw new Error("Not implemented");
},
targetPreferences: [{ position: "after", insideOutsideType: "outside" }],
};
paste = new Paste(this.graph);
scrollToBottom = new ScrollToBottom(this.graph);
scrollToCenter = new ScrollToCenter(this.graph);
scrollToTop = new ScrollToTop(this.graph);
Expand Down