forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource-documents.js
More file actions
166 lines (138 loc) · 4.05 KB
/
source-documents.js
File metadata and controls
166 lines (138 loc) · 4.05 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
/* 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
import { getMode } from "../source";
import { isWasm, getWasmLineNumberFormatter, renderWasmText } from "../wasm";
import { resizeBreakpointGutter, resizeToggleButton } from "../ui";
import SourceEditor from "./source-editor";
import type { Source, SourceRecord } from "../../types";
import type { SymbolDeclarations } from "../../workers/parser";
let sourceDocs = {};
function getDocument(key: string) {
return sourceDocs[key];
}
function hasDocument(key: string) {
return !!getDocument(key);
}
function setDocument(key: string, doc: any) {
sourceDocs[key] = doc;
}
function removeDocument(key: string) {
delete sourceDocs[key];
}
function clearDocuments() {
sourceDocs = {};
}
function resetLineNumberFormat(editor: SourceEditor) {
const cm = editor.codeMirror;
cm.setOption("lineNumberFormatter", number => number);
resizeBreakpointGutter(cm);
resizeToggleButton(cm);
}
function updateLineNumberFormat(editor: SourceEditor, sourceId: string) {
if (!isWasm(sourceId)) {
return resetLineNumberFormat(editor);
}
const cm = editor.codeMirror;
const lineNumberFormatter = getWasmLineNumberFormatter(sourceId);
cm.setOption("lineNumberFormatter", lineNumberFormatter);
resizeBreakpointGutter(cm);
resizeToggleButton(cm);
}
function updateDocument(editor: SourceEditor, source: SourceRecord) {
if (!source) {
return;
}
const sourceId = source.get("id");
const doc = getDocument(sourceId) || editor.createDocument();
editor.replaceDocument(doc);
updateLineNumberFormat(editor, sourceId);
}
function clearEditor(editor: SourceEditor) {
const doc = editor.createDocument();
editor.replaceDocument(doc);
editor.setText("");
editor.setMode({ name: "text" });
resetLineNumberFormat(editor);
}
function showLoading(editor: SourceEditor) {
if (hasDocument("loading")) {
return;
}
const doc = editor.createDocument();
setDocument("loading", doc);
editor.replaceDocument(doc);
editor.setText(L10N.getStr("loadingText"));
editor.setMode({ name: "text" });
}
function showErrorMessage(editor: Object, msg: string) {
let error;
if (msg.includes("WebAssembly binary source is not available")) {
error = L10N.getStr("wasmIsNotAvailable");
} else {
error = L10N.getFormatStr("errorLoadingText3", msg);
}
const doc = editor.createDocument();
editor.replaceDocument(doc);
editor.setText(error);
editor.setMode({ name: "text" });
resetLineNumberFormat(editor);
}
function setEditorText(editor: Object, source: Source) {
const { text, id: sourceId } = source;
if (source.isWasm) {
const wasmLines = renderWasmText(sourceId, (text: any));
// cm will try to split into lines anyway, saving memory
const wasmText = { split: () => wasmLines, match: () => false };
editor.setText(wasmText);
} else {
editor.setText(text);
}
}
/**
* Handle getting the source document or creating a new
* document with the correct mode and text.
*/
function showSourceText(
editor: Object,
source: Source,
symbols?: SymbolDeclarations
) {
if (!source) {
return;
}
if (hasDocument(source.id)) {
const doc = getDocument(source.id);
if (editor.codeMirror.doc === doc) {
const mode = getMode(source, symbols);
if (doc.mode.name !== mode.name) {
editor.setMode(mode);
}
return;
}
editor.replaceDocument(doc);
updateLineNumberFormat(editor, source.id);
editor.setMode(getMode(source, symbols));
return doc;
}
const doc = editor.createDocument();
setDocument(source.id, doc);
editor.replaceDocument(doc);
setEditorText(editor, source);
editor.setMode(getMode(source, symbols));
updateLineNumberFormat(editor, source.id);
}
export {
getDocument,
setDocument,
hasDocument,
removeDocument,
clearDocuments,
updateLineNumberFormat,
updateDocument,
clearEditor,
showSourceText,
showErrorMessage,
showLoading
};