forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick-open.js
More file actions
113 lines (98 loc) · 2.85 KB
/
quick-open.js
File metadata and controls
113 lines (98 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* 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, getSourcePath, isThirdParty } from "./source";
import type { Location as BabelLocation } from "babel-traverse";
import type { SourcesMap } from "../reducers/sources";
import type { QuickOpenType } from "../reducers/quick-open";
import type {
SymbolDeclaration,
SymbolDeclarations
} from "../workers/parser/types";
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 modifiers = {
"@": "functions",
"#": "variables",
":": "goto"
};
const modifier = query[0];
return modifiers[modifier];
}
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 type FormattedSymbolDeclaration = {|
id: string,
title: string,
subtitle: string,
value: string,
location: BabelLocation
|};
export type FormattedSymbolDeclarations = {|
variables: Array<FormattedSymbolDeclaration>,
functions: Array<FormattedSymbolDeclaration>
|};
export type FormattedSource = {|
value: string,
title: string,
subtitle: string,
id: string
|};
export function formatSymbol(
symbol: SymbolDeclaration
): FormattedSymbolDeclaration {
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: ?SymbolDeclarations
): FormattedSymbolDeclarations {
if (!symbols) {
return { variables: [], functions: [] };
}
const { variables, functions } = symbols;
return {
variables: variables.map(formatSymbol),
functions: functions.map(formatSymbol)
};
}
export function formatSources(sources: SourcesMap): Array<FormattedSource> {
return sources
.valueSeq()
.toJS()
.filter(source => !isPretty(source) && !isThirdParty(source))
.map(source => ({
value: getSourcePath(source),
title: getSourcePath(source)
.split("/")
.pop(),
subtitle: endTruncateStr(getSourcePath(source), 100),
id: source.id
}))
.filter(formattedSource => formattedSource.value != "");
}