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
157 lines (128 loc) · 4.11 KB
/
Copy pathindex.js
File metadata and controls
157 lines (128 loc) · 4.11 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
/* 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 "./getTokenLocation.js";
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 { AstPosition, AstLocation } from "../../workers/parser/types";
import type { EditorPosition, EditorRange } from "../editor/types";
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)) {
return wasmOffsetToLine(sourceId, lineOrOffset);
}
return lineOrOffset ? lineOrOffset - 1 : 1;
}
export function toEditorPosition(location: AstPosition): 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"
);
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);
}
export function toSourceLocation(
sourceId: string,
location: EditorPosition
): AstPosition {
return {
line: toSourceLine(sourceId, location.line),
column: isWasm(sourceId) ? undefined : location.column
};
}
export function markText(editor: any, className, location: EditorRange) {
const { start, end } = location;
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;
}