/* 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 . */
import {
generatedToOriginalId,
originalToGeneratedId,
} from "devtools/client/shared/source-map-loader/index";
import assert from "../../utils/assert";
import { recordEvent } from "../../utils/telemetry";
import { updateBreakpointsForNewPrettyPrintedSource } from "../breakpoints";
import { createLocation } from "../../utils/location";
import {
getPrettySourceURL,
isGenerated,
isJavaScript,
} from "../../utils/source";
import { isFulfilled } from "../../utils/async-value";
import { getOriginalLocation } from "../../utils/source-maps";
import { prefs } from "../../utils/prefs";
import {
loadGeneratedSourceText,
loadOriginalSourceText,
} from "./loadSourceText";
import { mapFrames } from "../pause";
import { selectSpecificLocation } from "../sources";
import { createPrettyPrintOriginalSource } from "../../client/firefox/create";
import {
getSource,
getFirstSourceActorForGeneratedSource,
getSourceByURL,
getSelectedLocation,
getThreadContext,
} from "../../selectors";
import { selectSource } from "./select";
import DevToolsUtils from "devtools/shared/DevToolsUtils";
const LINE_BREAK_REGEX = /\r\n?|\n|\u2028|\u2029/g;
function matchAllLineBreaks(str) {
return Array.from(str.matchAll(LINE_BREAK_REGEX));
}
function getPrettyOriginalSourceURL(generatedSource) {
return getPrettySourceURL(generatedSource.url || generatedSource.id);
}
export async function prettyPrintSource(
sourceMapLoader,
prettyPrintWorker,
generatedSource,
content,
actors
) {
if (!content || !isFulfilled(content)) {
throw new Error("Cannot pretty-print a file that has not loaded");
}
const contentValue = content.value;
if (
(!isJavaScript(generatedSource, contentValue) && !generatedSource.isHTML) ||
contentValue.type !== "text"
) {
throw new Error(
`Can't prettify ${contentValue.contentType} files, only HTML and Javascript.`
);
}
const url = getPrettyOriginalSourceURL(generatedSource);
let prettyPrintWorkerResult;
if (generatedSource.isHTML) {
prettyPrintWorkerResult = await prettyPrintHtmlFile({
prettyPrintWorker,
generatedSource,
content,
actors,
});
} else {
prettyPrintWorkerResult = await prettyPrintWorker.prettyPrint({
sourceText: contentValue.value,
indent: " ".repeat(prefs.indentSize),
url,
});
}
// The source map URL service used by other devtools listens to changes to
// sources based on their actor IDs, so apply the sourceMap there too.
const generatedSourceIds = [
generatedSource.id,
...actors.map(item => item.actor),
];
await sourceMapLoader.setSourceMapForGeneratedSources(
generatedSourceIds,
prettyPrintWorkerResult.sourceMap
);
return {
text: prettyPrintWorkerResult.code,
contentType: contentValue.contentType,
};
}
/**
* Pretty print inline script inside an HTML file
*
* @param {Object} options
* @param {PrettyPrintDispatcher} options.prettyPrintWorker: The prettyPrint worker
* @param {Object} options.generatedSource: The HTML source we want to pretty print
* @param {Object} options.content
* @param {Array} options.actors: An array of the HTML file inline script sources data
*
* @returns Promise