UNPKG

4.88 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.TreeSearch = void 0;
19const tslib_1 = require("tslib");
20const inversify_1 = require("inversify");
21const disposable_1 = require("../../common/disposable");
22const event_1 = require("../../common/event");
23const tree_1 = require("./tree");
24const fuzzy_search_1 = require("./fuzzy-search");
25const tree_iterator_1 = require("./tree-iterator");
26const label_provider_1 = require("../label-provider");
27let TreeSearch = class TreeSearch {
28 constructor() {
29 this.disposables = new disposable_1.DisposableCollection();
30 this.filteredNodesEmitter = new event_1.Emitter();
31 this._filterResult = [];
32 this._filteredNodes = [];
33 this._filteredNodesAndParents = new Set();
34 }
35 init() {
36 this.disposables.push(this.filteredNodesEmitter);
37 }
38 getHighlights() {
39 return new Map(this._filterResult.map(m => [m.item.id, this.toCaptionHighlight(m)]));
40 }
41 /**
42 * Resolves to all the visible tree nodes that match the search pattern.
43 */
44 async filter(pattern) {
45 const { root } = this.tree;
46 this._filteredNodesAndParents = new Set();
47 if (!pattern || !root) {
48 this._filterResult = [];
49 this._filteredNodes = [];
50 this.fireFilteredNodesChanged(this._filteredNodes);
51 return [];
52 }
53 const items = [...new tree_iterator_1.TopDownTreeIterator(root)];
54 const transform = (node) => this.labelProvider.getName(node);
55 this._filterResult = await this.fuzzySearch.filter({
56 items,
57 pattern,
58 transform
59 });
60 this._filteredNodes = this._filterResult.map(({ item }) => {
61 this.addAllParentsToFilteredSet(item);
62 return item;
63 });
64 this.fireFilteredNodesChanged(this._filteredNodes);
65 return this._filteredNodes.slice();
66 }
67 addAllParentsToFilteredSet(node) {
68 let toAdd = node;
69 while (toAdd && !this._filteredNodesAndParents.has(toAdd.id)) {
70 this._filteredNodesAndParents.add(toAdd.id);
71 toAdd = toAdd.parent;
72 }
73 ;
74 }
75 /**
76 * Returns with the filtered nodes after invoking the `filter` method.
77 */
78 get filteredNodes() {
79 return this._filteredNodes.slice();
80 }
81 /**
82 * Event that is fired when the filtered nodes have been changed.
83 */
84 get onFilteredNodesChanged() {
85 return this.filteredNodesEmitter.event;
86 }
87 passesFilters(node) {
88 return this._filteredNodesAndParents.has(node.id);
89 }
90 dispose() {
91 this.disposables.dispose();
92 }
93 fireFilteredNodesChanged(nodes) {
94 this.filteredNodesEmitter.fire(nodes);
95 }
96 toCaptionHighlight(match) {
97 return {
98 ranges: match.ranges.map(this.mapRange.bind(this))
99 };
100 }
101 mapRange(range) {
102 const { offset, length } = range;
103 return {
104 offset,
105 length
106 };
107 }
108};
109(0, tslib_1.__decorate)([
110 (0, inversify_1.inject)(tree_1.Tree),
111 (0, tslib_1.__metadata)("design:type", Object)
112], TreeSearch.prototype, "tree", void 0);
113(0, tslib_1.__decorate)([
114 (0, inversify_1.inject)(fuzzy_search_1.FuzzySearch),
115 (0, tslib_1.__metadata)("design:type", fuzzy_search_1.FuzzySearch)
116], TreeSearch.prototype, "fuzzySearch", void 0);
117(0, tslib_1.__decorate)([
118 (0, inversify_1.inject)(label_provider_1.LabelProvider),
119 (0, tslib_1.__metadata)("design:type", label_provider_1.LabelProvider)
120], TreeSearch.prototype, "labelProvider", void 0);
121(0, tslib_1.__decorate)([
122 (0, inversify_1.postConstruct)(),
123 (0, tslib_1.__metadata)("design:type", Function),
124 (0, tslib_1.__metadata)("design:paramtypes", []),
125 (0, tslib_1.__metadata)("design:returntype", void 0)
126], TreeSearch.prototype, "init", null);
127TreeSearch = (0, tslib_1.__decorate)([
128 (0, inversify_1.injectable)()
129], TreeSearch);
130exports.TreeSearch = TreeSearch;
131//# sourceMappingURL=tree-search.js.map
\No newline at end of file