1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | var __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 | };
|
23 | var __metadata = (this && this.__metadata) || function (k, v) {
|
24 | if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
25 | };
|
26 | Object.defineProperty(exports, "__esModule", { value: true });
|
27 | exports.TreeSearch = void 0;
|
28 | const inversify_1 = require("inversify");
|
29 | const disposable_1 = require("../../common/disposable");
|
30 | const event_1 = require("../../common/event");
|
31 | const tree_1 = require("./tree");
|
32 | const fuzzy_search_1 = require("./fuzzy-search");
|
33 | const tree_iterator_1 = require("./tree-iterator");
|
34 | const label_provider_1 = require("../label-provider");
|
35 | let 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 |
|
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 |
|
85 |
|
86 | get filteredNodes() {
|
87 | return this._filteredNodes.slice();
|
88 | }
|
89 | |
90 |
|
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);
|
135 | TreeSearch = __decorate([
|
136 | (0, inversify_1.injectable)()
|
137 | ], TreeSearch);
|
138 | exports.TreeSearch = TreeSearch;
|
139 |
|
\ | No newline at end of file |