import { endTruncateStr } from "./utils";
import { isPretty, getFilename } 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 { RelativeSource } from "../selectors/getRelativeSources";
import type { SymbolDeclaration } from "../workers/parser";
export const MODIFIERS = {
export function parseQuickOpenQuery(query: string): QuickOpenType {
const modifierPattern = /^@|#|:|\?$/;
const gotoSourcePattern = /^(\w+)\:/;
const startsWithModifier = modifierPattern.test(query[0]);
const isGotoSource = gotoSourcePattern.test(query);
if (startsWithModifier) {
const modifier = query[0];
return MODIFIERS[modifier];
export function parseLineColumn(query: string) {
const [, line, column] = query.split(":");
const lineNumber = parseInt(line, 10);
const columnNumber = parseInt(column, 10);
if (!isNaN(lineNumber)) {
...(!isNaN(columnNumber) ? { column: columnNumber } : null)
export function formatSourcesForList(source: RelativeSource) {
const title = getFilename(source);
const subtitle = endTruncateStr(source.relativeUrl, 100);
value: source.relativeUrl,
export type QuickOpenResult = {|
location?: BabelLocation,
export type FormattedSymbolDeclarations = {|
variables: Array<QuickOpenResult>,
functions: Array<QuickOpenResult>
export function formatSymbol(symbol: SymbolDeclaration): QuickOpenResult {
id: `${symbol.name}:${symbol.location.start.line}`,
subtitle: `${symbol.location.start.line}`,
location: symbol.location
export function formatSymbols(symbols: ?Symbols): FormattedSymbolDeclarations {
if (!symbols || symbols.loading) {
return { variables: [], functions: [] };
const { variables, functions } = symbols;
variables: variables.map(formatSymbol),
functions: functions.map(formatSymbol)
export function formatShortcutResults(): Array<QuickOpenResult> {
value: L10N.getStr("symbolSearch.search.functionsPlaceholder.title"),
title: `@ ${L10N.getStr("symbolSearch.search.functionsPlaceholder")}`,
value: L10N.getStr("symbolSearch.search.variablesPlaceholder.title"),
title: `# ${L10N.getStr("symbolSearch.search.variablesPlaceholder")}`,
value: L10N.getStr("gotoLineModal.title"),
title: `: ${L10N.getStr("gotoLineModal.placeholder")}`,
export function formatSources(
sources: RelativeSource[]
): Array<QuickOpenResult> {
.filter(source => !isPretty(source))
.map(source => formatSourcesForList(source))
.filter(({ value }) => value != "");