/* 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/>. */
/** * Utils for working with Source URLs * @module utils/source
*/
export const javascriptLikeExtensions = new Set(["marko", "es6", "vue", "jsm"]);
function getPath(source) { const { path } = source.displayURL;
let lastIndex = path.lastIndexOf("/");
let nextToLastIndex = path.lastIndexOf("/", lastIndex - 1);
const result = []; do {
result.push(path.slice(nextToLastIndex + 1, lastIndex));
lastIndex = nextToLastIndex;
nextToLastIndex = path.lastIndexOf("/", lastIndex - 1);
} while (lastIndex !== nextToLastIndex);
result.push("");
return result;
}
export function shouldBlackbox(source) { if (!source) { returnfalse;
}
if (!source.url) { returnfalse;
}
returntrue;
}
/** * Checks if the frame is within a line ranges which are blackboxed * in the source. * * @param {Object} frame * The current frame * @param {Object} blackboxedRanges * The currently blackboxedRanges for all the sources. * @param {Boolean} isFrameBlackBoxed * If the frame is within the blackboxed range * or not.
*/
export function isFrameBlackBoxed(frame, blackboxedRanges) { const { source } = frame.location; return (
!!blackboxedRanges[source.url] &&
(!blackboxedRanges[source.url].length ||
!!findBlackBoxRange(source, blackboxedRanges, {
start: frame.location.line,
end: frame.location.line,
}))
);
}
/** * Checks if a blackbox range exist for the line range. * That is if any start and end lines overlap any of the * blackbox ranges * * @param {Object} source * The current selected source * @param {Object} blackboxedRanges * The store of blackboxedRanges * @param {Object} lineRange * The start/end line range `{ start: <Number>, end: <Number> }` * @return {Object} blackboxRange * The first matching blackbox range that all or part of the * specified lineRange sits within.
*/
export function findBlackBoxRange(source, blackboxedRanges, lineRange) { const ranges = blackboxedRanges[source.url]; if (!ranges || !ranges.length) { returnnull;
}
/** * Checks if a source line is blackboxed * @param {Array} ranges - Line ranges that are blackboxed * @param {Number} line * @param {Boolean} isSourceOnIgnoreList - is the line in a source that is on * the sourcemap ignore lists then the line is blackboxed. * @returns boolean
*/
export function isLineBlackboxed(ranges, line, isSourceOnIgnoreList) { if (isSourceOnIgnoreList) { returntrue;
}
if (!ranges) { returnfalse;
} // If the whole source is ignored , then the line is // ignored. if (!ranges.length) { returntrue;
} return !!ranges.find(
range => line >= range.start.line && line <= range.end.line
);
}
/** * Returns true if the specified url and/or content type are specific to * javascript files. * * @return boolean * True if the source is likely javascript. * * @memberof utils/source * @static
*/
export function isJavaScript(source, content) { const extension = source.displayURL.fileExtension; const contentType = content.type === "wasm" ? null : content.contentType; return (
javascriptLikeExtensions.has(extension) ||
!!(contentType && contentType.includes("javascript"))
);
}
export function getFormattedSourceId(id) { if (typeof id != "string") {
console.error( "Expected source id to be a string, got", typeof id, " | id:",
id
); return"";
} return id.substring(id.lastIndexOf("/") + 1);
}
/** * Provides a middle-truncated filename displayed in Tab titles
*/
export function getTruncatedFileName(source) { return truncateMiddleText(source.longName, 30);
}
/* Gets path for files with same filename for editor tabs, breakpoints, etc. * Pass the source, and list of other sources * * @memberof utils/source * @static
*/
// Find sources that have the same filename, but different paths // as the original source const similarSources = sources.filter(source => { const rawSource = getRawSourceURL(source.url); return rawSourceURL != rawSource && filename == source.shortName;
});
if (!similarSources.length) { return undefined;
}
// get an array of source path directories e.g. ['a/b/c.html'] => [['b', 'a']] const paths = new Array(similarSources.length + 1);
paths[0] = getPath(mySource); for (let i = 0; i < similarSources.length; ++i) {
paths[i + 1] = getPath(similarSources[i]);
}
// create an array of similar path directories and one dis-similar directory // for example [`a/b/c.html`, `a1/b/c.html`] => ['b', 'a'] // where 'b' is the similar directory and 'a' is the dis-similar directory.
let displayPath = ""; for (let i = 0; i < paths[0].length; i++) {
let similar = false; for (let k = 1; k < paths.length; ++k) { if (paths[k][i] === paths[0][i]) {
similar = true; break;
}
}
/** * Gets a readable source URL for display purposes. * If the source does not have a URL, the source ID will be returned instead. * * @memberof utils/source * @static
*/
export function getFileURL(source, truncate = true) { const { url, id } = source; if (!url) { return getFormattedSourceId(id);
}
/** * Returns amount of lines in the source. If source is a WebAssembly binary, * the function returns amount of bytes.
*/
export function getSourceLineCount(content) { if (content.type === "wasm") { const { binary } = content.value; return binary.length;
}
let count = 0;
for (let i = 0; i < content.value.length; ++i) { if (content.value[i] === "\n") {
++count;
}
}
return count + 1;
}
function getNthLine(str, lineNum) {
let startIndex = -1;
/** * Compute the CSS classname string to use for the icon of a given source. * * @param {Object} source * The reducer source object. * @param {Boolean} isBlackBoxed * To be set to true, when the given source is blackboxed. * @param {Boolean} hasPrettyTab * To be set to true, if the given source isn't the pretty printed one, * but another tab for that source is opened pretty printed. * @return String * The classname to use.
*/
export function getSourceClassnames(
source,
isBlackBoxed,
hasPrettyTab = false
) { // Conditionals should be ordered by priority of icon! const defaultClassName = "file";
if (!source || !source.url) { return defaultClassName;
}
// In the SourceTree, we don't show the pretty printed sources, // but still want to show the pretty print icon when a pretty printed tab // for the current source is opened. if (isPretty(source) || hasPrettyTab) { return"prettyPrint";
}
if (isBlackBoxed) { return"blackBox";
}
if (isUrlExtension(source.url)) { return"extension";
}
export function getRelativeUrl(source, root) { const { group, path } = source.displayURL; if (!root) { return path;
}
// + 1 removes the leading "/" const url = group + path; return url.slice(url.indexOf(root) + root.length + 1);
}
export function isUrlExtension(url) { return url.includes("moz-extension:") || url.includes("chrome-extension");
}
/** * Checks that source url matches one of the glob patterns * * @param {Object} source * @param {String} excludePatterns String of comma-seperated glob patterns * @return {return} Boolean value specifies if the string matches any of the patterns.
*/
export function matchesGlobPatterns(source, excludePatterns) { if (!excludePatterns) { returnfalse;
} const patterns = excludePatterns
.split(",")
.map(pattern => pattern.trim())
.filter(pattern => pattern !== "");
if (!patterns.length) { returnfalse;
}
return micromatch.contains( // Makes sure we format the url or id exactly the way its displayed in the search ui, // as user wil usually create glob patterns based on what is seen in the ui.
source.url ? getRelativePath(source.url) : getFormattedSourceId(source.id),
patterns
);
}
¤ Dauer der Verarbeitung: 0.24 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.