@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
122 lines • 4.21 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2018 TypeFox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { inject, injectable, postConstruct } from 'inversify';
import { DisposableCollection } from '../../common/disposable.js';
import { Emitter } from '../../common/event.js';
import { Tree } from './tree.js';
import { FuzzySearch } from './fuzzy-search.js';
import { TopDownTreeIterator } from './tree-iterator.js';
import { LabelProvider } from '../label-provider.js';
let TreeSearch = class TreeSearch {
constructor() {
this.disposables = new DisposableCollection();
this.filteredNodesEmitter = new Emitter();
this._filterResult = [];
this._filteredNodes = [];
this._filteredNodesAndParents = new Set();
}
init() {
this.disposables.push(this.filteredNodesEmitter);
}
getHighlights() {
return new Map(this._filterResult.map(m => [m.item.id, this.toCaptionHighlight(m)]));
}
/**
* Resolves to all the visible tree nodes that match the search pattern.
*/
async filter(pattern) {
const { root } = this.tree;
this._filteredNodesAndParents = new Set();
if (!pattern || !root) {
this._filterResult = [];
this._filteredNodes = [];
this.fireFilteredNodesChanged(this._filteredNodes);
return [];
}
const items = [...new TopDownTreeIterator(root)];
const transform = (node) => this.labelProvider.getName(node);
this._filterResult = await this.fuzzySearch.filter({
items,
pattern,
transform
});
this._filteredNodes = this._filterResult.map(({ item }) => {
this.addAllParentsToFilteredSet(item);
return item;
});
this.fireFilteredNodesChanged(this._filteredNodes);
return this._filteredNodes.slice();
}
addAllParentsToFilteredSet(node) {
let toAdd = node;
while (toAdd && !this._filteredNodesAndParents.has(toAdd.id)) {
this._filteredNodesAndParents.add(toAdd.id);
toAdd = toAdd.parent;
}
;
}
/**
* Returns with the filtered nodes after invoking the `filter` method.
*/
get filteredNodes() {
return this._filteredNodes.slice();
}
/**
* Event that is fired when the filtered nodes have been changed.
*/
get onFilteredNodesChanged() {
return this.filteredNodesEmitter.event;
}
passesFilters(node) {
return this._filteredNodesAndParents.has(node.id);
}
dispose() {
this.disposables.dispose();
}
fireFilteredNodesChanged(nodes) {
this.filteredNodesEmitter.fire(nodes);
}
toCaptionHighlight(match) {
return {
ranges: match.ranges.map(this.mapRange.bind(this))
};
}
mapRange(range) {
const { offset, length } = range;
return {
offset,
length
};
}
};
__decorate([
inject(Tree)
], TreeSearch.prototype, "tree", void 0);
__decorate([
inject(FuzzySearch)
], TreeSearch.prototype, "fuzzySearch", void 0);
__decorate([
inject(LabelProvider)
], TreeSearch.prototype, "labelProvider", void 0);
__decorate([
postConstruct()
], TreeSearch.prototype, "init", null);
TreeSearch = __decorate([
injectable()
], TreeSearch);
export { TreeSearch };
//# sourceMappingURL=tree-search.js.map