UNPKG

5.45 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 WITH Classpath-exception-2.0
16// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.TreeSearch = void 0;
28const inversify_1 = require("inversify");
29const disposable_1 = require("../../common/disposable");
30const event_1 = require("../../common/event");
31const tree_1 = require("./tree");
32const fuzzy_search_1 = require("./fuzzy-search");
33const tree_iterator_1 = require("./tree-iterator");
34const label_provider_1 = require("../label-provider");
35let TreeSearch = class TreeSearch {
36 constructor() {
37 this.disposables = new disposable_1.DisposableCollection();
38 this.filteredNodesEmitter = new event_1.Emitter();
39 this._filterResult = [];
40 this._filteredNodes = [];
41 this._filteredNodesAndParents = new Set();
42 }
43 init() {
44 this.disposables.push(this.filteredNodesEmitter);
45 }
46 getHighlights() {
47 return new Map(this._filterResult.map(m => [m.item.id, this.toCaptionHighlight(m)]));
48 }
49 /**
50 * Resolves to all the visible tree nodes that match the search pattern.
51 */
52 async filter(pattern) {
53 const { root } = this.tree;
54 this._filteredNodesAndParents = new Set();
55 if (!pattern || !root) {
56 this._filterResult = [];
57 this._filteredNodes = [];
58 this.fireFilteredNodesChanged(this._filteredNodes);
59 return [];
60 }
61 const items = [...new tree_iterator_1.TopDownTreeIterator(root)];
62 const transform = (node) => this.labelProvider.getName(node);
63 this._filterResult = await this.fuzzySearch.filter({
64 items,
65 pattern,
66 transform
67 });
68 this._filteredNodes = this._filterResult.map(({ item }) => {
69 this.addAllParentsToFilteredSet(item);
70 return item;
71 });
72 this.fireFilteredNodesChanged(this._filteredNodes);
73 return this._filteredNodes.slice();
74 }
75 addAllParentsToFilteredSet(node) {
76 let toAdd = node;
77 while (toAdd && !this._filteredNodesAndParents.has(toAdd.id)) {
78 this._filteredNodesAndParents.add(toAdd.id);
79 toAdd = toAdd.parent;
80 }
81 ;
82 }
83 /**
84 * Returns with the filtered nodes after invoking the `filter` method.
85 */
86 get filteredNodes() {
87 return this._filteredNodes.slice();
88 }
89 /**
90 * Event that is fired when the filtered nodes have been changed.
91 */
92 get onFilteredNodesChanged() {
93 return this.filteredNodesEmitter.event;
94 }
95 passesFilters(node) {
96 return this._filteredNodesAndParents.has(node.id);
97 }
98 dispose() {
99 this.disposables.dispose();
100 }
101 fireFilteredNodesChanged(nodes) {
102 this.filteredNodesEmitter.fire(nodes);
103 }
104 toCaptionHighlight(match) {
105 return {
106 ranges: match.ranges.map(this.mapRange.bind(this))
107 };
108 }
109 mapRange(range) {
110 const { offset, length } = range;
111 return {
112 offset,
113 length
114 };
115 }
116};
117__decorate([
118 (0, inversify_1.inject)(tree_1.Tree),
119 __metadata("design:type", Object)
120], TreeSearch.prototype, "tree", void 0);
121__decorate([
122 (0, inversify_1.inject)(fuzzy_search_1.FuzzySearch),
123 __metadata("design:type", fuzzy_search_1.FuzzySearch)
124], TreeSearch.prototype, "fuzzySearch", void 0);
125__decorate([
126 (0, inversify_1.inject)(label_provider_1.LabelProvider),
127 __metadata("design:type", label_provider_1.LabelProvider)
128], TreeSearch.prototype, "labelProvider", void 0);
129__decorate([
130 (0, inversify_1.postConstruct)(),
131 __metadata("design:type", Function),
132 __metadata("design:paramtypes", []),
133 __metadata("design:returntype", void 0)
134], TreeSearch.prototype, "init", null);
135TreeSearch = __decorate([
136 (0, inversify_1.injectable)()
137], TreeSearch);
138exports.TreeSearch = TreeSearch;
139//# sourceMappingURL=tree-search.js.map
\No newline at end of file