Skip to content

Commit

Permalink
implemented actions puff, float, drop (#122)
Browse files Browse the repository at this point in the history
* implemented actions puff, float, drop

* remove unnecessary check for reversed selection

* renamed insert line actions

* Update Types.ts

* Update index.ts

Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
  • Loading branch information
AndreasArvidsson and pokey authored Jul 22, 2021
1 parent f0612d9 commit d176211
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,15 @@ export type ActionType =
| "cut"
| "delete"
| "extractVariable"
| "editNewLineAbove"
| "editNewLineBelow"
| "findInFiles"
| "fold"
| "getText"
| "insertEmptyLineAbove"
| "insertEmptyLinesAround"
| "insertEmptyLineBelow"
| "indentLines"
| "insertLineAfter"
| "insertLineBefore"
| "move"
| "outdentLines"
| "paste"
Expand Down
4 changes: 2 additions & 2 deletions src/actions/InsertLine.ts → src/actions/EditNewLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../Types";
import { commands } from "vscode";

export class InsertLineBefore implements Action {
export class EditNewLineAbove implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "outside" }];

constructor(private graph: Graph) {
Expand All @@ -28,7 +28,7 @@ export class InsertLineBefore implements Action {
}
}

export class InsertLineAfter implements Action {
export class EditNewLineBelow implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "outside" }];

constructor(private graph: Graph) {
Expand Down
102 changes: 102 additions & 0 deletions src/actions/InsertEmptyLines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../Types";
import { TextEditor, Selection, Position } from "vscode";
import displayPendingEditDecorations from "../editDisplayUtils";
import { runForEachEditor } from "../targetUtils";

class InsertEmptyLines implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];

constructor(
private graph: Graph,
private insertAbove: boolean,
private insertBelow: boolean
) {
this.run = this.run.bind(this);
}

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

const edits = await runForEachEditor(
targets,
(target) => target.selection.editor,
async (editor: TextEditor, targets: TypedSelection[]) => {
const lines = targets.flatMap((target) => {
const lines = [];
if (this.insertAbove) {
lines.push(target.selection.selection.start.line);
}
if (this.insertBelow) {
lines.push(target.selection.selection.end.line + 1);
}
return lines;
});
return { editor, lines };
}
);

for (const edit of edits) {
await edit.editor.edit((editBuilder) => {
edit.lines.forEach((line) => {
editBuilder.insert(new Position(line, 0), "\n");
});
});
}

const thatMark = targets.map((target) => {
const lines = edits.find(
(edit) => edit.editor === target.selection.editor
)!.lines;
const selection = target.selection.selection;
const offsetAnchor = lines.filter(
(line) => line <= selection.anchor.line
).length;
const offsetActive = lines.filter(
(line) => line <= selection.active.line
).length;
const newSelection = new Selection(
selection.anchor.line + offsetAnchor,
selection.anchor.character,
selection.active.line + offsetActive,
selection.active.character
);
return {
selection: newSelection,
editor: target.selection.editor,
};
});

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

export class InsertEmptyLinesAround extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, true, true);
}
}

export class InsertEmptyLineAbove extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, true, false);
}
}

export class InsertEmptyLineBelow extends InsertEmptyLines {
constructor(graph: Graph) {
super(graph, false, true);
}
}
16 changes: 13 additions & 3 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Cut from "./cut";
import Delete from "./delete";
import ExtractVariable from "./extractVariable";
import { Fold, Unfold } from "./fold";
import { InsertLineBefore, InsertLineAfter } from "./InsertLine";
import { EditNewLineAbove, EditNewLineBelow } from "./EditNewLine";
import {
SetSelection,
SetSelectionBefore,
Expand All @@ -17,6 +17,11 @@ import { IndentLines, OutdentLines } from "./Indent";
import { CommentLines } from "./Comment";
import Paste from "./Paste";
import { Bring, Move, Swap } from "./BringMoveSwap";
import {
InsertEmptyLineAbove,
InsertEmptyLineBelow,
InsertEmptyLinesAround,
} from "./InsertEmptyLines";
import GetText from "./GetText";
import { FindInFiles } from "./Find";

Expand All @@ -25,6 +30,8 @@ class Actions implements ActionRecord {

// TODO NB Remove when user had time to migrate to new talon code
use = new Bring(this.graph);
insertLineBefore = new EditNewLineAbove(this.graph);
insertLineAfter = new EditNewLineBelow(this.graph);

bring = new Bring(this.graph);
clear = new Clear(this.graph);
Expand All @@ -33,12 +40,15 @@ class Actions implements ActionRecord {
cut = new Cut(this.graph);
delete = new Delete(this.graph);
extractVariable = new ExtractVariable(this.graph);
editNewLineAbove = new EditNewLineAbove(this.graph);
editNewLineBelow = new EditNewLineBelow(this.graph);
findInFiles = new FindInFiles(this.graph);
fold = new Fold(this.graph);
getText = new GetText(this.graph);
insertEmptyLineAbove = new InsertEmptyLineAbove(this.graph);
insertEmptyLinesAround = new InsertEmptyLinesAround(this.graph);
insertEmptyLineBelow = new InsertEmptyLineBelow(this.graph);
indentLines = new IndentLines(this.graph);
insertLineBefore = new InsertLineBefore(this.graph);
insertLineAfter = new InsertLineAfter(this.graph);
move = new Move(this.graph);
outdentLines = new OutdentLines(this.graph);
paste = new Paste(this.graph);
Expand Down

0 comments on commit d176211

Please sign in to comment.