UNPKG

@theia/navigator

Version:
130 lines 5.63 kB
"use strict"; // ***************************************************************************** // 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 // ***************************************************************************** Object.defineProperty(exports, "__esModule", { value: true }); exports.FileNavigatorFilterPredicate = exports.FileNavigatorFilter = void 0; const tslib_1 = require("tslib"); const inversify_1 = require("@theia/core/shared/inversify"); const minimatch_1 = require("minimatch"); const event_1 = require("@theia/core/lib/common/event"); const filesystem_preferences_1 = require("@theia/filesystem/lib/common/filesystem-preferences"); const navigator_preferences_1 = require("../common/navigator-preferences"); /** * Filter for omitting elements from the navigator. For more details on the exclusion patterns, * one should check either the manual with `man 5 gitignore` or just [here](https://git-scm.com/docs/gitignore). */ let FileNavigatorFilter = class FileNavigatorFilter { constructor(preferences) { this.preferences = preferences; this.emitter = new event_1.Emitter(); } init() { this.doInit(); } async doInit() { this.filterPredicate = this.createFilterPredicate(this.filesPreferences['files.exclude']); this.filesPreferences.onPreferenceChanged(event => this.onFilesPreferenceChanged(event)); this.preferences.onPreferenceChanged(event => this.onPreferenceChanged(event)); } async filter(items) { return (await items).filter(item => this.filterItem(item)); } get onFilterChanged() { return this.emitter.event; } filterItem(item) { return this.filterPredicate.filter(item); } fireFilterChanged() { this.emitter.fire(undefined); } onFilesPreferenceChanged(event) { const { preferenceName } = event; if (preferenceName === 'files.exclude') { const filesExcludes = this.filesPreferences['files.exclude']; this.filterPredicate = this.createFilterPredicate(filesExcludes); this.fireFilterChanged(); } } onPreferenceChanged(event) { } createFilterPredicate(exclusions) { return new FileNavigatorFilterPredicate(this.interceptExclusions(exclusions)); } toggleHiddenFiles() { this.showHiddenFiles = !this.showHiddenFiles; const filesExcludes = this.filesPreferences['files.exclude']; this.filterPredicate = this.createFilterPredicate(filesExcludes || {}); this.fireFilterChanged(); } interceptExclusions(exclusions) { return { ...exclusions, '**/.*': this.showHiddenFiles }; } }; exports.FileNavigatorFilter = FileNavigatorFilter; tslib_1.__decorate([ (0, inversify_1.inject)(filesystem_preferences_1.FileSystemPreferences), tslib_1.__metadata("design:type", Object) ], FileNavigatorFilter.prototype, "filesPreferences", void 0); tslib_1.__decorate([ (0, inversify_1.postConstruct)(), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", void 0) ], FileNavigatorFilter.prototype, "init", null); exports.FileNavigatorFilter = FileNavigatorFilter = tslib_1.__decorate([ (0, inversify_1.injectable)(), tslib_1.__param(0, (0, inversify_1.inject)(navigator_preferences_1.FileNavigatorPreferences)), tslib_1.__metadata("design:paramtypes", [Object]) ], FileNavigatorFilter); (function (FileNavigatorFilter) { let Predicate; (function (Predicate) { /** * Wraps a bunch of predicates and returns with a new one that evaluates to `true` if * each of the wrapped predicates evaluates to `true`. Otherwise, `false`. */ function and(...predicates) { return { filter: id => predicates.every(predicate => predicate.filter(id)) }; } Predicate.and = and; })(Predicate = FileNavigatorFilter.Predicate || (FileNavigatorFilter.Predicate = {})); })(FileNavigatorFilter || (exports.FileNavigatorFilter = FileNavigatorFilter = {})); /** * Concrete filter navigator filter predicate that is decoupled from the preferences. */ class FileNavigatorFilterPredicate { constructor(exclusions) { const patterns = Object.keys(exclusions).map(pattern => ({ pattern, enabled: exclusions[pattern] })).filter(object => object.enabled).map(object => object.pattern); this.delegate = FileNavigatorFilter.Predicate.and(...patterns.map(pattern => this.createDelegate(pattern))); } filter(item) { return this.delegate.filter(item); } createDelegate(pattern) { const delegate = new minimatch_1.Minimatch(pattern, { matchBase: true }); return { filter: item => !delegate.match(item.id) }; } } exports.FileNavigatorFilterPredicate = FileNavigatorFilterPredicate; //# sourceMappingURL=navigator-filter.js.map