diff options
Diffstat (limited to 'devtools/client/debugger/src/workers/pretty-print')
3 files changed, 117 insertions, 0 deletions
diff --git a/devtools/client/debugger/src/workers/pretty-print/index.js b/devtools/client/debugger/src/workers/pretty-print/index.js new file mode 100644 index 0000000000..be10314d2b --- /dev/null +++ b/devtools/client/debugger/src/workers/pretty-print/index.js @@ -0,0 +1,43 @@ +/* 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 { prefs } from "../../utils/prefs"; +import { workerUtils } from "devtools-utils"; +const { WorkerDispatcher } = workerUtils; + +import type { URL } from "../../types"; + +let dispatcher; +let workerPath; + +export const start = (path: string) => { + workerPath = path; +}; +export const stop = () => { + if (dispatcher) { + dispatcher.stop(); + dispatcher = null; + workerPath = null; + } +}; + +type PrettyPrintOpts = { + text: string, + url: URL, +}; + +export async function prettyPrint({ text, url }: PrettyPrintOpts) { + if (!dispatcher) { + dispatcher = new WorkerDispatcher(); + dispatcher.start(workerPath); + } + + return dispatcher.invoke("prettyPrint", { + url, + indent: prefs.indentSize, + sourceText: text, + }); +} diff --git a/devtools/client/debugger/src/workers/pretty-print/moz.build b/devtools/client/debugger/src/workers/pretty-print/moz.build new file mode 100644 index 0000000000..b7223ac81a --- /dev/null +++ b/devtools/client/debugger/src/workers/pretty-print/moz.build @@ -0,0 +1,10 @@ +# vim: set filetype=python: +# 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/. + +DIRS += [] + +CompiledModules( + "index.js", +) diff --git a/devtools/client/debugger/src/workers/pretty-print/worker.js b/devtools/client/debugger/src/workers/pretty-print/worker.js new file mode 100644 index 0000000000..7697d5c88c --- /dev/null +++ b/devtools/client/debugger/src/workers/pretty-print/worker.js @@ -0,0 +1,64 @@ +/* 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 prettyFast from "pretty-fast"; + +import { workerUtils } from "devtools-utils"; +const { workerHandler } = workerUtils; + +type Mappings = { + _array: Mapping[], +}; + +type Mapping = { + originalLine: number, + originalColumn: number, + source?: string, + generatedLine?: number, + generatedColumn?: number, + name?: string, +}; + +type InvertedMapping = { + generated: Object, + source?: any, + original?: any, + name?: string, +}; + +function prettyPrint({ url, indent, sourceText }) { + const prettified = prettyFast(sourceText, { + url, + indent: " ".repeat(indent), + }); + + return { + code: prettified.code, + mappings: invertMappings(prettified.map._mappings), + }; +} + +function invertMappings(mappings: Mappings) { + return mappings._array.map((m: Mapping) => { + const mapping: InvertedMapping = { + generated: { + line: m.originalLine, + column: m.originalColumn, + }, + }; + if (m.source) { + mapping.source = m.source; + mapping.original = { + line: m.generatedLine, + column: m.generatedColumn, + }; + mapping.name = m.name; + } + return mapping; + }); +} + +self.onmessage = workerHandler({ prettyPrint }); |