diff options
Diffstat (limited to '')
-rw-r--r-- | devtools/client/debugger/src/utils/quick-open.js | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/devtools/client/debugger/src/utils/quick-open.js b/devtools/client/debugger/src/utils/quick-open.js new file mode 100644 index 0000000000..835244e20a --- /dev/null +++ b/devtools/client/debugger/src/utils/quick-open.js @@ -0,0 +1,159 @@ +/* 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 { endTruncateStr } from "./utils"; +import { + isPretty, + getFilename, + getSourceClassnames, + getSourceQueryString, +} from "./source"; + +import type { Location as BabelLocation } from "@babel/types"; +import type { Symbols } from "../reducers/ast"; +import type { QuickOpenType } from "../reducers/quick-open"; +import type { Tab } from "../reducers/tabs"; +import type { Source } from "../types"; +import type { + SymbolDeclaration, + IdentifierDeclaration, +} from "../workers/parser"; + +export const MODIFIERS = { + "@": "functions", + "#": "variables", + ":": "goto", + "?": "shortcuts", +}; + +export function parseQuickOpenQuery(query: string): QuickOpenType { + const startsWithModifier = + query[0] === "@" || + query[0] === "#" || + query[0] === ":" || + query[0] === "?"; + + if (startsWithModifier) { + const modifier = query[0]; + return MODIFIERS[modifier]; + } + + const isGotoSource = query.includes(":", 1); + + if (isGotoSource) { + return "gotoSource"; + } + + return "sources"; +} + +export function parseLineColumn(query: string) { + const [, line, column] = query.split(":"); + const lineNumber = parseInt(line, 10); + const columnNumber = parseInt(column, 10); + if (!isNaN(lineNumber)) { + return { + line: lineNumber, + ...(!isNaN(columnNumber) ? { column: columnNumber } : null), + }; + } +} + +export function formatSourcesForList( + source: Source, + tabUrls: Set<$PropertyType<Tab, "url">> +) { + const title = getFilename(source); + const relativeUrlWithQuery = `${source.relativeUrl}${getSourceQueryString( + source + ) || ""}`; + const subtitle = endTruncateStr(relativeUrlWithQuery, 100); + const value = relativeUrlWithQuery; + return { + value, + title, + subtitle, + icon: tabUrls.has(source.url) + ? "tab result-item-icon" + : `result-item-icon ${getSourceClassnames(source)}`, + id: source.id, + url: source.url, + }; +} + +export type QuickOpenResult = {| + id: string, + value: string, + title: string | React$Element<"div">, + subtitle?: string, + location?: BabelLocation, + url?: string, + icon?: string, +|}; + +export type FormattedSymbolDeclarations = {| + functions: Array<QuickOpenResult>, +|}; + +export function formatSymbol( + symbol: SymbolDeclaration | IdentifierDeclaration +): QuickOpenResult { + return { + id: `${symbol.name}:${symbol.location.start.line}`, + title: symbol.name, + subtitle: `${symbol.location.start.line}`, + value: symbol.name, + location: symbol.location, + }; +} + +export function formatSymbols(symbols: ?Symbols): FormattedSymbolDeclarations { + if (!symbols || symbols.loading) { + return { functions: [] }; + } + + const { functions } = symbols; + + return { + functions: functions.map(formatSymbol), + }; +} + +export function formatShortcutResults(): Array<QuickOpenResult> { + return [ + { + value: L10N.getStr("symbolSearch.search.functionsPlaceholder.title"), + title: `@ ${L10N.getStr("symbolSearch.search.functionsPlaceholder")}`, + id: "@", + }, + { + value: L10N.getStr("symbolSearch.search.variablesPlaceholder.title"), + title: `# ${L10N.getStr("symbolSearch.search.variablesPlaceholder")}`, + id: "#", + }, + { + value: L10N.getStr("gotoLineModal.title"), + title: `: ${L10N.getStr("gotoLineModal.placeholder")}`, + id: ":", + }, + ]; +} + +export function formatSources( + sources: Source[], + tabUrls: Set<$PropertyType<Tab, "url">> +): Array<QuickOpenResult> { + const formattedSources: Array<QuickOpenResult> = []; + + for (let i = 0; i < sources.length; ++i) { + const source = sources[i]; + + if (!!source.relativeUrl && !isPretty(source)) { + formattedSources.push(formatSourcesForList(source, tabUrls)); + } + } + + return formattedSources; +} |