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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/* 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/>. */
import React, { Component } from "devtools/client/shared/vendor/react";
import { div } from "devtools/client/shared/vendor/react-dom-factories";
import PropTypes from "devtools/client/shared/vendor/react-prop-types";
import { connect } from "devtools/client/shared/vendor/react-redux";
import { basename } from "../utils/path";
import { createLocation } from "../utils/location";
const fuzzyAldrin = require("resource://devtools/client/shared/vendor/fuzzaldrin-plus.js");
const { throttle } = require("resource://devtools/shared/throttle.js");
import actions from "../actions/index";
import {
getDisplayedSourcesList,
getQuickOpenQuery,
getQuickOpenType,
getSelectedLocation,
getSettledSourceTextContent,
getSourceTabs,
getBlackBoxRanges,
getProjectDirectoryRoot,
} from "../selectors/index";
import { memoizeLast } from "../utils/memoizeLast";
import { searchKeys } from "../constants";
import {
formatSymbol,
parseLineColumn,
formatShortcutResults,
formatSourceForList,
} from "../utils/quick-open";
import Modal from "./shared/Modal";
import SearchInput from "./shared/SearchInput";
import ResultList from "./shared/ResultList";
const maxResults = 100;
const SIZE_BIG = { size: "big" };
const SIZE_DEFAULT = {};
function filter(values, query, key = "value") {
const preparedQuery = fuzzyAldrin.prepareQuery(query);
return fuzzyAldrin.filter(values, query, {
key,
maxResults,
preparedQuery,
});
}
export class QuickOpenModal extends Component {
// Put it on the class so it can be retrieved in tests
static UPDATE_RESULTS_THROTTLE = 100;
constructor(props) {
super(props);
this.state = { results: null, selectedIndex: 0 };
}
static get propTypes() {
return {
closeQuickOpen: PropTypes.func.isRequired,
displayedSources: PropTypes.array.isRequired,
blackBoxRanges: PropTypes.object.isRequired,
highlightLineRange: PropTypes.func.isRequired,
clearHighlightLineRange: PropTypes.func.isRequired,
query: PropTypes.string.isRequired,
searchType: PropTypes.oneOf([
"functions",
"goto",
"gotoSource",
"other",
"shortcuts",
"sources",
"variables",
]).isRequired,
selectSpecificLocation: PropTypes.func.isRequired,
selectedContentLoaded: PropTypes.bool,
selectedLocation: PropTypes.object,
setQuickOpenQuery: PropTypes.func.isRequired,
openedTabUrls: PropTypes.array.isRequired,
toggleShortcutsModal: PropTypes.func.isRequired,
projectDirectoryRoot: PropTypes.string,
getFunctionSymbols: PropTypes.func.isRequired,
};
}
setResults(results) {
if (results) {
results = results.slice(0, maxResults);
}
this.setState({ results });
}
componentDidMount() {
const { query, shortcutsModalEnabled, toggleShortcutsModal } = this.props;
this.updateResults(query);
if (shortcutsModalEnabled) {
toggleShortcutsModal();
}
}
componentDidUpdate(prevProps) {
const queryChanged = prevProps.query !== this.props.query;
if (queryChanged) {
this.updateResults(this.props.query);
}
}
closeModal = () => {
this.props.closeQuickOpen();
};
dropGoto = query => {
const index = query.indexOf(":");
return index !== -1 ? query.slice(0, index) : query;
};
formatSources = memoizeLast(
(displayedSources, openedTabUrls, blackBoxRanges, projectDirectoryRoot) => {
// Note that we should format all displayed sources,
// the actual filtering will only be done late from `searchSources()`
return displayedSources.map(source => {
const isBlackBoxed = !!blackBoxRanges[source.url];
const hasTabOpened = openedTabUrls.includes(source.url);
return formatSourceForList(
source,
hasTabOpened,
isBlackBoxed,
projectDirectoryRoot
);
});
}
);
searchSources = query => {
const {
displayedSources,
openedTabUrls,
blackBoxRanges,
projectDirectoryRoot,
} = this.props;
const sources = this.formatSources(
displayedSources,
openedTabUrls,
blackBoxRanges,
projectDirectoryRoot
);
const results =
query == "" ? sources : filter(sources, this.dropGoto(query));
return this.setResults(results);
};
searchSymbols = async query => {
const { getFunctionSymbols, selectedLocation } = this.props;
if (!selectedLocation) {
return this.setResults([]);
}
let results = await getFunctionSymbols(selectedLocation, maxResults);
if (query === "@" || query === "#") {
results = results.map(formatSymbol);
return this.setResults(results);
}
results = filter(results, query.slice(1), "name");
results = results.map(formatSymbol);
return this.setResults(results);
};
searchShortcuts = query => {
const results = formatShortcutResults();
if (query == "?") {
this.setResults(results);
} else {
this.setResults(filter(results, query.slice(1)));
}
};
/**
* This method is called when we just opened the modal and the query input is empty
*/
showTopSources = () => {
const { openedTabUrls, blackBoxRanges, projectDirectoryRoot } = this.props;
let { displayedSources } = this.props;
// If there is some tabs opened, only show tab's sources.
// Otherwise, we display all visible sources (per SourceTree definition),
// setResults will restrict the number of results to a maximum limit.
if (openedTabUrls.length) {
displayedSources = displayedSources.filter(
source => !!source.url && openedTabUrls.includes(source.url)
);
}
this.setResults(
this.formatSources(
displayedSources,
openedTabUrls,
blackBoxRanges,
projectDirectoryRoot
)
);
};
updateResults = throttle(query => {
if (this.isGotoQuery()) {
return;
}
if (query == "" && !this.isShortcutQuery()) {
this.showTopSources();
return;
}
if (this.isSymbolSearch()) {
this.searchSymbols(query);
return;
}
if (this.isShortcutQuery()) {
this.searchShortcuts(query);
return;
}
this.searchSources(query);
}, QuickOpenModal.UPDATE_RESULTS_THROTTLE);
setModifier = item => {
if (["@", "#", ":"].includes(item.id)) {
this.props.setQuickOpenQuery(item.id);
}
};
selectResultItem = (e, item) => {
if (item == null) {
return;
}
if (this.isShortcutQuery()) {
this.setModifier(item);
return;
}
if (this.isGotoSourceQuery()) {
const location = parseLineColumn(this.props.query);
this.gotoLocation({ ...location, source: item.source });
return;
}
if (this.isSymbolSearch()) {
this.gotoLocation({
line:
item.location && item.location.start ? item.location.start.line : 0,
});
return;
}
this.gotoLocation({ source: item.source, line: 0 });
};
onSelectResultItem = item => {
const { selectedLocation, highlightLineRange, clearHighlightLineRange } =
this.props;
if (
selectedLocation == null ||
!this.isSymbolSearch() ||
!this.isFunctionQuery()
) {
return;
}
if (item.location) {
highlightLineRange({
start: item.location.start.line,
end: item.location.end.line,
sourceId: selectedLocation.source.id,
});
} else {
clearHighlightLineRange();
}
};
traverseResults = e => {
const direction = e.key === "ArrowUp" ? -1 : 1;
const { selectedIndex, results } = this.state;
const resultCount = this.getResultCount();
const index = selectedIndex + direction;
const nextIndex = (index + resultCount) % resultCount || 0;
this.setState({ selectedIndex: nextIndex });
if (results != null) {
this.onSelectResultItem(results[nextIndex]);
}
};
gotoLocation = location => {
const { selectSpecificLocation, selectedLocation } = this.props;
if (location != null) {
selectSpecificLocation(
createLocation({
source: location.source || selectedLocation?.source,
line: location.line,
column: location.column,
})
);
this.closeModal();
}
};
onChange = e => {
const { selectedLocation, selectedContentLoaded, setQuickOpenQuery } =
this.props;
setQuickOpenQuery(e.target.value);
const noSource = !selectedLocation || !selectedContentLoaded;
if ((noSource && this.isSymbolSearch()) || this.isGotoQuery()) {
return;
}
// Wait for the next tick so that reducer updates are complete.
const targetValue = e.target.value;
setTimeout(() => this.updateResults(targetValue), 0);
};
onKeyDown = e => {
const { query } = this.props;
const { results, selectedIndex } = this.state;
const isGoToQuery = this.isGotoQuery();
if (!results && !isGoToQuery) {
return;
}
if (e.key === "Enter") {
if (isGoToQuery) {
const location = parseLineColumn(query);
this.gotoLocation(location);
return;
}
if (results) {
this.selectResultItem(e, results[selectedIndex]);
return;
}
}
if (e.key === "Tab") {
this.closeModal();
return;
}
if (["ArrowUp", "ArrowDown"].includes(e.key)) {
e.preventDefault();
this.traverseResults(e);
}
};
getResultCount = () => {
const { results } = this.state;
return results && results.length ? results.length : 0;
};
// Query helpers
isFunctionQuery = () => this.props.searchType === "functions";
isSymbolSearch = () => this.isFunctionQuery();
isGotoQuery = () => this.props.searchType === "goto";
isGotoSourceQuery = () => this.props.searchType === "gotoSource";
isShortcutQuery = () => this.props.searchType === "shortcuts";
isSourcesQuery = () => this.props.searchType === "sources";
isSourceSearch = () => this.isSourcesQuery() || this.isGotoSourceQuery();
/* eslint-disable react/no-danger */
renderHighlight(candidateString, query) {
const options = {
wrap: {
tagOpen: '<mark class="highlight">',
tagClose: "</mark>",
},
};
const html = fuzzyAldrin.wrap(candidateString, query, options);
return div({
dangerouslySetInnerHTML: {
__html: html,
},
});
}
highlightMatching = (query, results) => {
let newQuery = query;
if (newQuery === "") {
return results;
}
newQuery = query.replace(/[@:#?]/gi, " ");
return results.map(result => {
if (typeof result.title == "string") {
return {
...result,
title: this.renderHighlight(
result.title,
basename(newQuery),
"title"
),
};
}
return result;
});
};
shouldShowErrorEmoji() {
const { query } = this.props;
if (this.isGotoQuery()) {
return !/^:\d*$/.test(query);
}
return !!query && !this.getResultCount();
}
getSummaryMessage() {
let summaryMsg = "";
if (this.isGotoQuery()) {
summaryMsg = L10N.getStr("shortcuts.gotoLine");
} else if (this.isFunctionQuery() && !this.state.results) {
summaryMsg = L10N.getStr("loadingText");
}
return summaryMsg;
}
render() {
const { query } = this.props;
const { selectedIndex, results } = this.state;
const items = this.highlightMatching(query, results || []);
const expanded = !!items && !!items.length;
return React.createElement(
Modal,
{
handleClose: this.closeModal,
},
React.createElement(SearchInput, {
query,
hasPrefix: true,
count: this.getResultCount(),
placeholder: L10N.getStr("sourceSearch.search2"),
summaryMsg: this.getSummaryMessage(),
showErrorEmoji: this.shouldShowErrorEmoji(),
isLoading: false,
onChange: this.onChange,
onKeyDown: this.onKeyDown,
handleClose: this.closeModal,
expanded,
showClose: false,
searchKey: searchKeys.QUICKOPEN_SEARCH,
showExcludePatterns: false,
showSearchModifiers: false,
selectedItemId:
expanded && items[selectedIndex] ? items[selectedIndex].id : "",
...(this.isSourceSearch() ? SIZE_BIG : SIZE_DEFAULT),
}),
results &&
React.createElement(ResultList, {
key: "results",
items,
selected: selectedIndex,
selectItem: this.selectResultItem,
ref: "resultList",
expanded,
...(this.isSourceSearch() ? SIZE_BIG : SIZE_DEFAULT),
})
);
}
}
/* istanbul ignore next: ignoring testing of redux connection stuff */
function mapStateToProps(state) {
const selectedLocation = getSelectedLocation(state);
const displayedSources = getDisplayedSourcesList(state);
const tabs = getSourceTabs(state);
const openedTabUrls = [...new Set(tabs.map(tab => tab.url))];
return {
displayedSources,
blackBoxRanges: getBlackBoxRanges(state),
projectDirectoryRoot: getProjectDirectoryRoot(state),
selectedLocation,
selectedContentLoaded: selectedLocation
? !!getSettledSourceTextContent(state, selectedLocation)
: undefined,
query: getQuickOpenQuery(state),
searchType: getQuickOpenType(state),
openedTabUrls,
};
}
export default connect(mapStateToProps, {
selectSpecificLocation: actions.selectSpecificLocation,
setQuickOpenQuery: actions.setQuickOpenQuery,
highlightLineRange: actions.highlightLineRange,
clearHighlightLineRange: actions.clearHighlightLineRange,
closeQuickOpen: actions.closeQuickOpen,
getFunctionSymbols: actions.getFunctionSymbols,
})(QuickOpenModal);
|