) => {
const { enabled, query } = this.props;
const { results, selectedIndex } = this.state;
if (!this.isGotoQuery() && (!enabled || !results)) {
return;
}
if (e.key === "Enter") {
if (this.isGotoQuery()) {
const location = parseLineColumn(query);
return this.gotoLocation(location);
}
if (results) {
if (this.isShortcutQuery()) {
return this.setModifier(results[selectedIndex]);
}
return this.selectResultItem(e, results[selectedIndex]);
}
}
if (e.key === "Tab") {
return this.closeModal();
}
if (["ArrowUp", "ArrowDown"].includes(e.key)) {
e.preventDefault();
return this.traverseResults(e);
}
};
getResultCount = () => {
const results = this.state.results;
return results && results.length ? results.length : 0;
};
// Query helpers
isFunctionQuery = () => this.props.searchType === "functions";
isVariableQuery = () => this.props.searchType === "variables";
isSymbolSearch = () => this.isFunctionQuery() || this.isVariableQuery();
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: string, query: string, name: string) => {
const options = {
wrap: {
tagOpen: '',
tagClose: ""
}
};
const html = fuzzyAldrin.wrap(candidateString, query, options);
return ;
};
highlightMatching = (query: string, results: QuickOpenResult[]) => {
let newQuery = query;
if (newQuery === "") {
return results;
}
newQuery = query.replace(/[@:#?]/gi, " ");
return results.map(result => {
return {
...result,
title: this.renderHighlight(result.title, basename(newQuery), "title")
};
});
};
shouldShowErrorEmoji() {
const { query } = this.props;
if (this.isGotoQuery()) {
return !/^:\d*$/.test(query);
}
return !this.getResultCount() && !!query;
}
renderLoading = () => {
const { symbolsLoading } = this.props;
if ((this.isFunctionQuery() || this.isVariableQuery()) && symbolsLoading) {
return (
{L10N.getStr("loadingText")}
);
}
};
render() {
const { enabled, query } = this.props;
const { selectedIndex, results } = this.state;
if (!enabled) {
return null;
}
const newResults = results && results.slice(0, 100);
const items = this.highlightMatching(query, newResults || []);
const expanded = !!items && items.length > 0;
return (
{this.renderLoading()}
{newResults && (
)}
);
}
}
/* istanbul ignore next: ignoring testing of redux connection stuff */
function mapStateToProps(state) {
const selectedSource = getSelectedSource(state);
return {
enabled: getQuickOpenEnabled(state),
sources: formatSources(getRelativeSources(state), getTabs(state).toArray()),
selectedSource,
symbols: formatSymbols(getSymbols(state, selectedSource)),
symbolsLoading: isSymbolsLoading(state, selectedSource),
query: getQuickOpenQuery(state),
searchType: getQuickOpenType(state),
tabs: getTabs(state).toArray()
};
}
/* istanbul ignore next: ignoring testing of redux connection stuff */
export default connect(mapStateToProps, actions)(QuickOpenModal);