Flow Coverage Report - src/utils/quick-open.js

FilenamePercentTotalCoveredUncovered
src/utils/quick-open.js 99 % 175 174 1
x
 
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
4
5
// @flow
6
7
import { endTruncateStr } from "./utils";
8
import { isPretty, getFilename } from "./source";
9
10
import type { Location as BabelLocation } from "@babel/types";
11
import type { Symbols } from "../reducers/ast";
12
import type { QuickOpenType } from "../reducers/quick-open";
13
import type { RelativeSource } from "../selectors/getRelativeSources";
14
import type { SymbolDeclaration } from "../workers/parser";
15
16
export const MODIFIERS = {
17
  "@": "functions",
18
  "#": "variables",
19
  ":": "goto",
20
  "?": "shortcuts"
21
};
22
23
export function parseQuickOpenQuery(query: string): QuickOpenType {
24
  const modifierPattern = /^@|#|:|\?$/;
25
  const gotoSourcePattern = /^(\w+)\:/;
26
  const startsWithModifier = modifierPattern.test(query[0]);
27
  const isGotoSource = gotoSourcePattern.test(query);
28
29
  if (startsWithModifier) {
30
    const modifier = query[0];
31
1x
    return MODIFIERS[modifier];
32
  }
33
34
  if (isGotoSource) {
35
    return "gotoSource";
36
  }
37
38
  return "sources";
39
}
40
41
export function parseLineColumn(query: string) {
42
  const [, line, column] = query.split(":");
43
  const lineNumber = parseInt(line, 10);
44
  const columnNumber = parseInt(column, 10);
45
  if (!isNaN(lineNumber)) {
46
    return {
47
      line: lineNumber,
48
      ...(!isNaN(columnNumber) ? { column: columnNumber } : null)
49
    };
50
  }
51
}
52
53
export function formatSourcesForList(source: RelativeSource) {
54
  const title = getFilename(source);
55
  const subtitle = endTruncateStr(source.relativeUrl, 100);
56
57
  return {
58
    value: source.relativeUrl,
59
    title,
60
    subtitle,
61
    id: source.id,
62
    url: source.url
63
  };
64
}
65
66
export type QuickOpenResult = {|
67
  id: string,
68
  value: string,
69
  title: string,
70
  subtitle?: string,
71
  location?: BabelLocation,
72
  url?: string
73
|};
74
75
export type FormattedSymbolDeclarations = {|
76
  variables: Array<QuickOpenResult>,
77
  functions: Array<QuickOpenResult>
78
|};
79
80
export function formatSymbol(symbol: SymbolDeclaration): QuickOpenResult {
81
  return {
82
    id: `${symbol.name}:${symbol.location.start.line}`,
83
    title: symbol.name,
84
    subtitle: `${symbol.location.start.line}`,
85
    value: symbol.name,
86
    location: symbol.location
87
  };
88
}
89
90
export function formatSymbols(symbols: ?Symbols): FormattedSymbolDeclarations {
91
  if (!symbols || symbols.loading) {
92
    return { variables: [], functions: [] };
93
  }
94
95
  const { variables, functions } = symbols;
96
97
  return {
98
    variables: variables.map(formatSymbol),
99
    functions: functions.map(formatSymbol)
100
  };
101
}
102
103
export function formatShortcutResults(): Array<QuickOpenResult> {
104
  return [
105
    {
106
      value: L10N.getStr("symbolSearch.search.functionsPlaceholder.title"),
107
      title: `@ ${L10N.getStr("symbolSearch.search.functionsPlaceholder")}`,
108
      id: "@"
109
    },
110
    {
111
      value: L10N.getStr("symbolSearch.search.variablesPlaceholder.title"),
112
      title: `# ${L10N.getStr("symbolSearch.search.variablesPlaceholder")}`,
113
      id: "#"
114
    },
115
    {
116
      value: L10N.getStr("gotoLineModal.title"),
117
      title: `: ${L10N.getStr("gotoLineModal.placeholder")}`,
118
      id: ":"
119
    }
120
  ];
121
}
122
123
export function formatSources(
124
  sources: RelativeSource[]
125
): Array<QuickOpenResult> {
126
  return sources
127
    .filter(source => !isPretty(source))
128
    .map(source => formatSourcesForList(source))
129
    .filter(({ value }) => value != "");
130
}
131