forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprettyPrint.js
More file actions
152 lines (126 loc) · 4.44 KB
/
Copy pathprettyPrint.js
File metadata and controls
152 lines (126 loc) · 4.44 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
/* 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 SourceMaps, { generatedToOriginalId } from "devtools-source-map";
import assert from "../../utils/assert";
import { recordEvent } from "../../utils/telemetry";
import { remapBreakpoints } from "../breakpoints";
import { setSymbols } from "./symbols";
import { prettyPrint } from "../../workers/pretty-print";
import {
getPrettySourceURL,
isGenerated,
isJavaScript
} from "../../utils/source";
import { loadSourceText } from "./loadSourceText";
import { mapFrames } from "../pause";
import { selectSpecificLocation } from "../sources";
import {
getSource,
getSourceFromId,
getSourceByURL,
getSelectedLocation,
getThreadContext
} from "../../selectors";
import type { Action, ThunkArgs } from "../types";
import { selectSource } from "./select";
import type { Source, SourceContent, SourceActor, Context } from "../../types";
export async function prettyPrintSource(
sourceMaps: typeof SourceMaps,
generatedSource: Source,
content: SourceContent,
actors: Array<SourceActor>
) {
if (!isJavaScript(generatedSource, content) || content.type !== "text") {
throw new Error("Can't prettify non-javascript files.");
}
const url = getPrettySourceURL(generatedSource.url);
const { code, mappings } = await prettyPrint({
text: content.value,
url: url
});
await sourceMaps.applySourceMap(generatedSource.id, url, code, mappings);
// The source map URL service used by other devtools listens to changes to
// sources based on their actor IDs, so apply the mapping there too.
for (const { actor } of actors) {
await sourceMaps.applySourceMap(actor, url, code, mappings);
}
return {
text: code,
contentType: "text/javascript"
};
}
export function createPrettySource(cx: Context, sourceId: string) {
return async ({ dispatch, getState, sourceMaps }: ThunkArgs) => {
const source = getSourceFromId(getState(), sourceId);
const url = getPrettySourceURL(source.url);
const id = generatedToOriginalId(sourceId, url);
const prettySource: Source = {
id,
url,
relativeUrl: url,
isBlackBoxed: false,
isPrettyPrinted: true,
isWasm: false,
introductionUrl: null,
introductionType: undefined,
isExtension: false
};
dispatch(({ type: "ADD_SOURCE", cx, source: prettySource }: Action));
await dispatch(selectSource(cx, prettySource.id));
return prettySource;
};
}
function selectPrettyLocation(cx: Context, prettySource: Source) {
return async ({ dispatch, sourceMaps, getState }: ThunkArgs) => {
let location = getSelectedLocation(getState());
if (location) {
location = await sourceMaps.getOriginalLocation(location);
return dispatch(
selectSpecificLocation(cx, { ...location, sourceId: prettySource.id })
);
}
return dispatch(selectSource(cx, prettySource.id));
};
}
/**
* Toggle the pretty printing of a source's text. All subsequent calls to
* |getText| will return the pretty-toggled text. Nothing will happen for
* non-javascript files.
*
* @memberof actions/sources
* @static
* @param string id The source form from the RDP.
* @returns Promise
* A promise that resolves to [aSource, prettyText] or rejects to
* [aSource, error].
*/
export function togglePrettyPrint(cx: Context, sourceId: string) {
return async ({ dispatch, getState, client, sourceMaps }: ThunkArgs) => {
const source = getSource(getState(), sourceId);
if (!source) {
return {};
}
if (!source.isPrettyPrinted) {
recordEvent("pretty_print");
}
await dispatch(loadSourceText({ cx, source }));
assert(
isGenerated(source),
"Pretty-printing only allowed on generated sources"
);
const url = getPrettySourceURL(source.url);
const prettySource = getSourceByURL(getState(), url);
if (prettySource) {
return dispatch(selectPrettyLocation(cx, prettySource));
}
const newPrettySource = await dispatch(createPrettySource(cx, sourceId));
await dispatch(selectPrettyLocation(cx, newPrettySource));
const threadcx = getThreadContext(getState());
await dispatch(mapFrames(threadcx));
await dispatch(setSymbols({ cx, source: newPrettySource }));
await dispatch(remapBreakpoints(cx, sourceId));
return newPrettySource;
};
}