Skip to content

Commit

Permalink
Added actions sort and reverse (#162)
Browse files Browse the repository at this point in the history
* Added actions sort and reverse

* cleanup
  • Loading branch information
AndreasArvidsson authored Aug 1, 2021
1 parent df20a98 commit 988244c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ export type ActionType =
| "move"
| "outdentLines"
| "paste"
| "reverse"
| "replace"
| "scrollToBottom"
| "scrollToCenter"
Expand All @@ -287,6 +288,7 @@ export type ActionType =
| "setSelection"
| "setSelectionAfter"
| "setSelectionBefore"
| "sort"
| "swap"
| "unfold"
| "wrap";
Expand Down
38 changes: 38 additions & 0 deletions src/actions/Sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Action,
ActionReturnValue,
ActionPreferences,
Graph,
TypedSelection,
} from "../Types";

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

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

protected sortTexts(texts: string[]) {
return texts.sort();
}

async run(targets: TypedSelection[][]): Promise<ActionReturnValue> {
const { returnValue: unsortedTexts } = await this.graph.actions.getText.run(
targets,
{
showDecorations: false,
}
);

const sortedTexts = this.sortTexts(unsortedTexts);

return this.graph.actions.replaceWithText.run(targets, sortedTexts);
}
}

export class Reverse extends Sort {
protected sortTexts(texts: string[]) {
return texts.reverse();
}
}
3 changes: 3 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { FindInFiles } from "./Find";
import Replace from "./Replace";
import { CopyLinesUp, CopyLinesDown } from "./CopyLines";
import SetBreakpoint from "./SetBreakpoint";
import { Sort, Reverse } from "./Sort";
import Call from "./Call";

class Actions implements ActionRecord {
Expand Down Expand Up @@ -57,6 +58,7 @@ class Actions implements ActionRecord {
move = new Move(this.graph);
outdentLines = new OutdentLines(this.graph);
paste = new Paste(this.graph);
reverse = new Reverse(this.graph);
replace = new Replace(this.graph);
scrollToBottom = new ScrollToBottom(this.graph);
scrollToCenter = new ScrollToCenter(this.graph);
Expand All @@ -65,6 +67,7 @@ class Actions implements ActionRecord {
setSelection = new SetSelection(this.graph);
setSelectionAfter = new SetSelectionAfter(this.graph);
setSelectionBefore = new SetSelectionBefore(this.graph);
sort = new Sort(this.graph);
swap = new Swap(this.graph);
unfold = new Unfold(this.graph);
wrap = new Wrap(this.graph);
Expand Down

0 comments on commit 988244c

Please sign in to comment.