-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathScroll.ts
More file actions
103 lines (88 loc) · 2.8 KB
/
Scroll.ts
File metadata and controls
103 lines (88 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import {
Action,
ActionPreferences,
ActionReturnValue,
Graph,
TypedSelection,
} from "../typings/Types";
import { displayDecorationsWhileRunningFunc } from "../util/editDisplayUtils";
import { groupBy } from "../util/itertools";
import { commands, window, workspace } from "vscode";
import { focusEditor } from "../util/setSelectionsAndFocusEditor";
class Scroll implements Action {
getTargetPreferences: () => ActionPreferences[] = () => [{ insideOutsideType: "inside" }];
constructor(private graph: Graph, private at: string) {
this.run = this.run.bind(this);
}
async run([targets]: [TypedSelection[]]): Promise<ActionReturnValue> {
const selectionGroups = groupBy(
targets,
(t: TypedSelection) => t.selection.editor
);
const lines = Array.from(selectionGroups, ([editor, targets]) => {
return { lineNumber: getLineNumber(targets, this.at), editor };
});
const scrollCallback = async () => {
const originalEditor = window.activeTextEditor;
for (const lineWithEditor of lines) {
// For reveal line to the work we have to have the correct editor focused
if (lineWithEditor.editor !== window.activeTextEditor) {
await focusEditor(lineWithEditor.editor);
}
await commands.executeCommand("revealLine", {
lineNumber: lineWithEditor.lineNumber,
at: this.at,
});
}
// If necessary focus back original editor
if (
originalEditor != null &&
originalEditor !== window.activeTextEditor
) {
await focusEditor(originalEditor);
}
};
const showAdditionalHighlightBeforeScroll = workspace
.getConfiguration("cursorless")
.get<boolean>("showAdditionalHighlightBeforeScroll")!;
await displayDecorationsWhileRunningFunc(
targets.map((target) => target.selection),
this.graph.editStyles.referenced.line,
scrollCallback,
showAdditionalHighlightBeforeScroll
);
return {
thatMark: targets.map((target) => target.selection),
};
}
}
export class ScrollToTop extends Scroll {
constructor(graph: Graph) {
super(graph, "top");
}
}
export class ScrollToCenter extends Scroll {
constructor(graph: Graph) {
super(graph, "center");
}
}
export class ScrollToBottom extends Scroll {
constructor(graph: Graph) {
super(graph, "bottom");
}
}
function getLineNumber(targets: TypedSelection[], at: string) {
let startLine = Number.MAX_SAFE_INTEGER;
let endLine = 0;
targets.forEach((t: TypedSelection) => {
startLine = Math.min(startLine, t.selection.selection.start.line);
endLine = Math.max(endLine, t.selection.selection.end.line);
});
if (at === "top") {
return startLine;
}
if (at === "bottom") {
return endLine;
}
return Math.floor((startLine + endLine) / 2);
}