forked from eclipse-theia/theia
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide API to filter unwanted contributions
Fixes eclipse-theia#9069 Signed-off-by: Tobias Ortmayr [email protected] Contributed on behalf of STMicroelectronics
- Loading branch information
Showing
9 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/core/src/common/contribution-filter/contribution-filter-registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 STMicroelectronics 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 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { injectable, multiInject, optional } from 'inversify'; | ||
import { ContributionFilter, ContributionType } from './contribution-filter'; | ||
import { applyFilters } from './filter'; | ||
|
||
const GENERIC_CONTRIBUTION_FILTER_KEY = '*'; | ||
@injectable() | ||
export class ContributionFilterRegistry { | ||
registry: Map<ContributionType, ContributionFilter[]>; | ||
constructor(@multiInject(ContributionFilter) @optional() contributionFilters: ContributionFilter[] = []) { | ||
this.registry = new Map(); | ||
contributionFilters.forEach(filter => { | ||
if (!filter.contributions) { | ||
this.addFilter(GENERIC_CONTRIBUTION_FILTER_KEY, filter); | ||
} else { | ||
filter.contributions.forEach(type => { | ||
this.addFilter(type, filter); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
private addFilter(type: ContributionType, filter: ContributionFilter): void { | ||
this.getOrCreate(type).push(filter); | ||
} | ||
|
||
private getOrCreate(type: ContributionType): ContributionFilter[] { | ||
let value = this.registry.get(type); | ||
if (!value) { | ||
value = []; | ||
this.registry.set(type, value); | ||
} | ||
return value; | ||
} | ||
|
||
get(type: ContributionType): ContributionFilter[] { | ||
const genericFilters = this.registry.get(GENERIC_CONTRIBUTION_FILTER_KEY) || []; | ||
const filters = this.registry.get(type) || []; | ||
filters.push(...genericFilters); | ||
return filters; | ||
} | ||
|
||
applyFilters<T extends Object>(toFilter: T[], type: ContributionType): T[] { | ||
const filters = this.get(type); | ||
return applyFilters<T>(toFilter, filters); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/core/src/common/contribution-filter/contribution-filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 STMicroelectronics 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 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { Filter, IdBasedFilter, NameBasedFilter } from './filter'; | ||
import { interfaces } from 'inversify'; | ||
export type ContributionType = interfaces.ServiceIdentifier<unknown>; | ||
|
||
export const ContributionFilter = Symbol('ContributionFilter'); | ||
export interface ContributionFilter extends Filter<Object> { | ||
/** | ||
* contribution types for which this filter is applicable | ||
*/ | ||
contributions?: ContributionType[]; | ||
} | ||
|
||
export abstract class IdBasedContributionFilter extends IdBasedFilter<Object> implements ContributionFilter { | ||
contributions?: ContributionType[]; | ||
} | ||
|
||
export abstract class NameBasedContributionFilter extends NameBasedFilter<Object> implements ContributionFilter { | ||
contributions?: ContributionType[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 STMicroelectronics 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 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
import { injectable } from 'inversify'; | ||
|
||
export const Filter = Symbol('Filter'); | ||
|
||
export interface Filter<T extends Object> { | ||
/** | ||
* Evaluates this filter on the given argument | ||
* @param toTest Object that should be tested | ||
* @returns true if the object should be filtered out false otherwise | ||
*/ | ||
test(toTest: T): Boolean; | ||
} | ||
|
||
export function applyFilters<T extends Object>(toFilter: T[], filters: Filter<T>[], negate: boolean = false): T[] { | ||
if (filters.length === 0) { | ||
return toFilter; | ||
} | ||
return toFilter.filter(object => { | ||
const result = filters.every(filter => !filter.test(object)); | ||
return negate ? !result : result; | ||
}); | ||
} | ||
|
||
export type PatternMatchingType = 'equals' | 'includes' | 'regex'; | ||
|
||
@injectable() | ||
export abstract class StringBasedFilter<T extends Object> implements Filter<T> { | ||
|
||
abstract patterns: string[]; | ||
patterMatchingType: PatternMatchingType = 'equals'; | ||
ignoreCase = false; | ||
|
||
abstract toFilterString(toFilter: T): string | undefined; | ||
|
||
protected doTest(filterStr: string): boolean { | ||
const patterns = this.ignoreCase ? this.patterns.map(pattern => pattern.toLowerCase()) : this.patterns; | ||
if (this.patterMatchingType === 'includes') { | ||
return patterns.find(pattern => filterStr.includes(pattern)) !== undefined; | ||
} else if (this.patterMatchingType === 'equals') { | ||
return patterns.find(pattern => filterStr === pattern) !== undefined; | ||
} else { | ||
// eslint-disable-next-line no-null/no-null | ||
return patterns.find(pattern => filterStr.match(new RegExp(pattern)) !== null) !== undefined; | ||
} | ||
} | ||
|
||
test(contribution: T): boolean { | ||
let filterStr = this.toFilterString(contribution); | ||
if (!filterStr) { | ||
return false; | ||
} | ||
if (this.ignoreCase) { | ||
filterStr = filterStr.toLowerCase(); | ||
} | ||
return this.doTest(filterStr); | ||
} | ||
} | ||
|
||
@injectable() | ||
export abstract class NameBasedFilter<T extends Object> extends StringBasedFilter<T> { | ||
toFilterString(toTest: T): string { | ||
return toTest.constructor.name; | ||
} | ||
} | ||
|
||
@injectable() | ||
export abstract class IdBasedFilter<T extends Object> extends StringBasedFilter<T> { | ||
toFilterString(toTest: T): string | undefined { | ||
if (Identifable.is(toTest)) { | ||
return toTest.id; | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
export interface Identifable { | ||
id: string; | ||
} | ||
|
||
export namespace Identifable { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function is(object: any): object is Identifable { | ||
return 'id' in object && typeof object['id'] === 'string'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 STMicroelectronics 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 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
export * from './contribution-filter'; | ||
export * from './contribution-filter-registry'; | ||
export * from './filter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters