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
156 lines (130 loc) · 4.09 KB
/
Copy pathsource-documents.js
File metadata and controls
156 lines (130 loc) · 4.09 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
/* 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 } from "../../types";
import type { SymbolDeclarations } from "../../workers/parser";
type SourceDocuments = { [string]: Object };
let sourceDocs: SourceDocuments = {};
export function getDocument(key: string) {
return sourceDocs[key];
}
export function hasDocument(key: string): boolean {
return !!getDocument(key);
}
export function setDocument(key: string, doc: any) {
sourceDocs[key] = doc;
}
export function removeDocument(key: string) {
delete sourceDocs[key];
}
export function clearDocuments() {
sourceDocs = {};
}
function resetLineNumberFormat(editor: SourceEditor) {
const cm = editor.codeMirror;
cm.setOption("lineNumberFormatter", number => number);
resizeBreakpointGutter(cm);
resizeToggleButton(cm);
}
export 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);
}
export function updateDocument(editor: SourceEditor, source: Source) {
if (!source) {
return;
}
const sourceId = source.id;
const doc = getDocument(sourceId) || editor.createDocument();
editor.replaceDocument(doc);
updateLineNumberFormat(editor, sourceId);
}
export function clearEditor(editor: SourceEditor) {
const doc = editor.createDocument();
editor.replaceDocument(doc);
editor.setText("");
editor.setMode({ name: "text" });
resetLineNumberFormat(editor);
}
export 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" });
}
export 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);
}
}
function setMode(editor, source, symbols) {
const mode = getMode(source, symbols);
const currentMode = editor.codeMirror.getOption("mode");
if (!currentMode || currentMode.name != mode.name) {
editor.setMode(mode);
}
}
/**
* Handle getting the source document or creating a new
* document with the correct mode and text.
*/
export 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) {
setMode(editor, source, symbols);
return;
}
editor.replaceDocument(doc);
updateLineNumberFormat(editor, source.id);
setMode(editor, source, symbols);
return doc;
}
const doc = editor.createDocument();
setDocument(source.id, doc);
editor.replaceDocument(doc);
setEditorText(editor, source);
setMode(editor, source, symbols);
updateLineNumberFormat(editor, source.id);
}