Skip to content

Commit

Permalink
Added actions: indent, dedent, comment (#116)
Browse files Browse the repository at this point in the history
* Added actions: indent, dedent, comment

* Renames indent classes

* Update Types.ts

* Update index.ts

* Update Comment.ts

* Update index.ts

* Update Types.ts

* Update CommandAction.ts

Co-authored-by: Pokey Rule <pokey.rule@gmail.com>
  • Loading branch information
AndreasArvidsson and pokey authored Jul 21, 2021
1 parent 63b330a commit d95a990
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 3 deletions.
109 changes: 109 additions & 0 deletions src/CommandAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { commands, window, Selection, TextEditor } from "vscode";
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
SelectionWithEditor,
} from "./Types";
import displayPendingEditDecorations from "./editDisplayUtils";
import { runOnTargetsForEachEditor } from "./targetUtils";
import { focusEditor } from "./setSelectionsAndFocusEditor";
import { flatten } from "lodash";

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

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

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

const originalEditor = window.activeTextEditor;

const thatMark = flatten(
await runOnTargetsForEachEditor<SelectionWithEditor[]>(
targets,
async (editor, selections) => {
// For command to the work we have to have the correct editor focused
if (editor !== window.activeTextEditor) {
await focusEditor(editor);
}

const originalSelections = editor.selections;

const newSelections = selections.map(
(selection) => selection.selection.selection
);

editor.selections = newSelections;

await commands.executeCommand(this.command);

// Map of line numbers mapping to the delta in character index for that line.
const lineDeltas = calculateLineChanges(editor, newSelections);

const updateSelection = (selection: Selection) =>
new Selection(
selection.start.line,
selection.start.character +
(lineDeltas[selection.start.line] ?? 0),
selection.end.line,
selection.end.character + (lineDeltas[selection.end.line] ?? 0)
);

const thatMark = newSelections.map((selection) => ({
editor,
selection: updateSelection(selection),
}));

// Reset original selections
editor.selections = originalSelections.map(updateSelection);

return thatMark;
}
)
);

// If necessary focus back original editor
if (originalEditor != null && originalEditor !== window.activeTextEditor) {
await focusEditor(originalEditor);
}

return { returnValue: null, thatMark };
}
}

function calculateLineChanges(
editor: TextEditor,
selectionsBefore: Selection[]
) {
const lineDeltas: { [key: number]: number } = {};
editor.selections.forEach((selectionAfter, index) => {
const selectionBefore = selectionsBefore[index];
const deltaStart =
selectionAfter.start.character - selectionBefore.start.character;
const deltaEnd =
selectionAfter.end.character - selectionBefore.end.character;
const delta =
Math.abs(deltaStart) > Math.abs(deltaEnd) ? deltaStart : deltaEnd;
for (
let i = selectionBefore.start.line;
i <= selectionBefore.end.line;
++i
) {
lineDeltas[i] = delta;
}
});
return lineDeltas;
}
3 changes: 3 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,17 @@ export interface Action {
export type ActionType =
| "bring"
| "clear"
| "commentLines"
| "copy"
| "cut"
| "delete"
| "extractVariable"
| "fold"
| "indentLines"
| "insertLineAfter"
| "insertLineBefore"
| "move"
| "outdentLines"
| "paste"
| "scrollToBottom"
| "scrollToCenter"
Expand Down
8 changes: 8 additions & 0 deletions src/actions/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Graph } from "../Types";
import CommandAction from "../CommandAction";

export class CommentLines extends CommandAction {
constructor(graph: Graph) {
super(graph, "editor.action.commentLine");
}
}
14 changes: 14 additions & 0 deletions src/actions/Indent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Graph } from "../Types";
import CommandAction from "../CommandAction";

export class IndentLines extends CommandAction {
constructor(graph: Graph) {
super(graph, "editor.action.indentLines");
}
}

export class OutdentLines extends CommandAction {
constructor(graph: Graph) {
super(graph, "editor.action.outdentLines");
}
}
2 changes: 1 addition & 1 deletion src/actions/Scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { displayDecorationsWhileRunningFunc } from "../editDisplayUtils";
import { groupBy } from "../itertools";
import { commands, window, workspace } from "vscode";
import { focusEditor } from "./setSelectionsAndFocusEditor";
import { focusEditor } from "../setSelectionsAndFocusEditor";

class Scroll implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];
Expand Down
2 changes: 1 addition & 1 deletion src/actions/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TypedSelection,
} from "../Types";
import { ensureSingleEditor } from "../targetUtils";
import { setSelectionsAndFocusEditor } from "./setSelectionsAndFocusEditor";
import { setSelectionsAndFocusEditor } from "../setSelectionsAndFocusEditor";

export default class Clear implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];
Expand Down
5 changes: 5 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
} from "./setSelection";
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";

Expand All @@ -24,14 +26,17 @@ class Actions implements ActionRecord {

bring = new Bring(this.graph);
clear = new Clear(this.graph);
commentLines = new CommentLines(this.graph);
copy = new Copy(this.graph);
cut = new Cut(this.graph);
delete = new Delete(this.graph);
extractVariable = new ExtractVariable(this.graph);
fold = new Fold(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);
scrollToBottom = new ScrollToBottom(this.graph);
scrollToCenter = new ScrollToCenter(this.graph);
Expand Down
2 changes: 1 addition & 1 deletion src/actions/setSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "../Types";
import { ensureSingleEditor } from "../targetUtils";
import { Selection } from "vscode";
import { setSelectionsAndFocusEditor } from "./setSelectionsAndFocusEditor";
import { setSelectionsAndFocusEditor } from "../setSelectionsAndFocusEditor";

export class SetSelection implements Action {
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];
Expand Down
File renamed without changes.

0 comments on commit d95a990

Please sign in to comment.