forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (152 loc) · 4.79 KB
/
index.js
File metadata and controls
188 lines (152 loc) · 4.79 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
export * from "./source-documents";
export * from "./get-token-location";
export * from "./source-search";
export * from "../ui";
export * from "./create-editor";
import { shouldPrettyPrint } from "../source";
import { findNext, findPrev } from "./source-search";
import { isWasm, lineToWasmOffset, wasmOffsetToLine } from "../wasm";
import { isOriginalId } from "devtools-source-map";
import type { AstLocation } from "../../workers/parser";
import type { EditorPosition, EditorRange } from "../editor/types";
import type { Location } from "../../types";
type Editor = Object;
let editor: ?Editor;
export function setEditor(_editor: Editor) {
editor = _editor;
}
export function getEditor() {
return editor;
}
export function removeEditor() {
editor = null;
}
export function shouldShowPrettyPrint(selectedSource) {
if (!selectedSource) {
return false;
}
return shouldPrettyPrint(selectedSource);
}
export function shouldShowFooter(selectedSource, horizontal) {
if (!horizontal) {
return true;
}
if (!selectedSource) {
return false;
}
return (
shouldShowPrettyPrint(selectedSource) ||
isOriginalId(selectedSource.get("id"))
);
}
export function traverseResults(e, ctx, query, dir, modifiers) {
e.stopPropagation();
e.preventDefault();
if (dir == "prev") {
findPrev(ctx, query, true, modifiers);
} else if (dir == "next") {
findNext(ctx, query, true, modifiers);
}
}
export function toEditorLine(sourceId: string, lineOrOffset: number): number {
if (isWasm(sourceId)) {
// TODO ensure offset is always "mappable" to edit line.
return wasmOffsetToLine(sourceId, lineOrOffset) || 0;
}
return lineOrOffset ? lineOrOffset - 1 : 1;
}
export function toEditorPosition(location: Location): EditorPosition {
return {
line: toEditorLine(location.sourceId, location.line),
column: isWasm(location.sourceId) || !location.column ? 0 : location.column
};
}
export function toEditorRange(
sourceId: string,
location: AstLocation
): EditorRange {
const { start, end } = location;
return {
start: toEditorPosition({ ...start, sourceId }),
end: toEditorPosition({ ...end, sourceId })
};
}
export function toSourceLine(sourceId: string, line: number): ?number {
return isWasm(sourceId) ? lineToWasmOffset(sourceId, line) : line + 1;
}
export function scrollToColumn(codeMirror: any, line: number, column: number) {
const { top, left } = codeMirror.charCoords(
{ line: line, ch: column },
"local"
);
if (!isVisible(codeMirror, top, left)) {
const scroller = codeMirror.getScrollerElement();
const centeredX = Math.max(left - scroller.offsetWidth / 2, 0);
const centeredY = Math.max(top - scroller.offsetHeight / 2, 0);
codeMirror.scrollTo(centeredX, centeredY);
}
}
function isVisible(codeMirror: any, top: number, left: number) {
function withinBounds(x, min, max) {
return x >= min && x <= max;
}
const scrollArea = codeMirror.getScrollInfo();
const charWidth = codeMirror.defaultCharWidth();
const inXView = withinBounds(
left,
scrollArea.left,
scrollArea.left + (scrollArea.clientWidth - 30) - charWidth
);
const fontHeight = codeMirror.defaultTextHeight();
const inYView = withinBounds(
top,
scrollArea.top,
scrollArea.top + scrollArea.clientHeight - fontHeight
);
return inXView && inYView;
}
export function markText(_editor: any, className, { start, end }: EditorRange) {
return _editor.codeMirror.markText(
{ ch: start.column, line: start.line },
{ ch: end.column, line: end.line },
{ className }
);
}
export function lineAtHeight(_editor, sourceId, event) {
const _editorLine = _editor.codeMirror.lineAtHeight(event.clientY);
return toSourceLine(sourceId, _editorLine);
}
export function getSourceLocationFromMouseEvent(_editor, selectedLocation, e) {
const { line, ch } = _editor.codeMirror.coordsChar({
left: e.clientX,
top: e.clientY
});
return {
sourceId: selectedLocation.sourceId,
line: line + 1,
column: ch + 1
};
}
export function forEachLine(codeMirror, iter) {
codeMirror.operation(() => {
codeMirror.doc.iter(0, codeMirror.lineCount(), iter);
});
}
export function removeLineClass(codeMirror, line, className) {
codeMirror.removeLineClass(line, "line", className);
}
export function clearLineClass(codeMirror, className) {
forEachLine(codeMirror, line => {
removeLineClass(codeMirror, line, className);
});
}
export function getTextForLine(codeMirror, line) {
return codeMirror.getLine(line - 1).trim();
}
export function getCursorLine(codeMirror) {
return codeMirror.getCursor().line;
}