Skip to content

Commit 988244c

Browse files
Added actions sort and reverse (#162)
* Added actions sort and reverse * cleanup
1 parent df20a98 commit 988244c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/Types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export type ActionType =
279279
| "move"
280280
| "outdentLines"
281281
| "paste"
282+
| "reverse"
282283
| "replace"
283284
| "scrollToBottom"
284285
| "scrollToCenter"
@@ -287,6 +288,7 @@ export type ActionType =
287288
| "setSelection"
288289
| "setSelectionAfter"
289290
| "setSelectionBefore"
291+
| "sort"
290292
| "swap"
291293
| "unfold"
292294
| "wrap";

src/actions/Sort.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
Action,
3+
ActionReturnValue,
4+
ActionPreferences,
5+
Graph,
6+
TypedSelection,
7+
} from "../Types";
8+
9+
export class Sort implements Action {
10+
targetPreferences: ActionPreferences[] = [{ insideOutsideType: "inside" }];
11+
12+
constructor(private graph: Graph) {
13+
this.run = this.run.bind(this);
14+
}
15+
16+
protected sortTexts(texts: string[]) {
17+
return texts.sort();
18+
}
19+
20+
async run(targets: TypedSelection[][]): Promise<ActionReturnValue> {
21+
const { returnValue: unsortedTexts } = await this.graph.actions.getText.run(
22+
targets,
23+
{
24+
showDecorations: false,
25+
}
26+
);
27+
28+
const sortedTexts = this.sortTexts(unsortedTexts);
29+
30+
return this.graph.actions.replaceWithText.run(targets, sortedTexts);
31+
}
32+
}
33+
34+
export class Reverse extends Sort {
35+
protected sortTexts(texts: string[]) {
36+
return texts.reverse();
37+
}
38+
}

src/actions/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { FindInFiles } from "./Find";
2525
import Replace from "./Replace";
2626
import { CopyLinesUp, CopyLinesDown } from "./CopyLines";
2727
import SetBreakpoint from "./SetBreakpoint";
28+
import { Sort, Reverse } from "./Sort";
2829
import Call from "./Call";
2930

3031
class Actions implements ActionRecord {
@@ -57,6 +58,7 @@ class Actions implements ActionRecord {
5758
move = new Move(this.graph);
5859
outdentLines = new OutdentLines(this.graph);
5960
paste = new Paste(this.graph);
61+
reverse = new Reverse(this.graph);
6062
replace = new Replace(this.graph);
6163
scrollToBottom = new ScrollToBottom(this.graph);
6264
scrollToCenter = new ScrollToCenter(this.graph);
@@ -65,6 +67,7 @@ class Actions implements ActionRecord {
6567
setSelection = new SetSelection(this.graph);
6668
setSelectionAfter = new SetSelectionAfter(this.graph);
6769
setSelectionBefore = new SetSelectionBefore(this.graph);
70+
sort = new Sort(this.graph);
6871
swap = new Swap(this.graph);
6972
unfold = new Unfold(this.graph);
7073
wrap = new Wrap(this.graph);

0 commit comments

Comments
 (0)