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.
- Add documentation - Generialize Stringbased filter by using functions + Utility functions - Remove IdBasedFilter (which cannot be used for contributions) - Fix typos - Add api test
- Loading branch information
Showing
8 changed files
with
222 additions
and
80 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
50 changes: 50 additions & 0 deletions
50
examples/api-samples/src/browser/contribution-filter/sample-filtered-command-contribution.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,50 @@ | ||
/******************************************************************************** | ||
* 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 { Command, CommandContribution, CommandRegistry, ContributionFilter, equals, NameBasedContributionFilter } from '@theia/core/lib/common'; | ||
import { injectable, interfaces } from 'inversify'; | ||
export namespace SampleFilteredCommand { | ||
const EXAMPLE_CATEGORY = 'Examples'; | ||
export const FILTERED: Command = { | ||
id: 'example_command.filtered', | ||
category: EXAMPLE_CATEGORY, | ||
label: 'This command should be filtered out' | ||
}; | ||
} | ||
|
||
@injectable() | ||
/** | ||
* This sample command is used to test the runtime filtering of already bound contributions. | ||
*/ | ||
export class SampleFilteredCommandContribution implements CommandContribution { | ||
|
||
registerCommands(commands: CommandRegistry): void { | ||
commands.registerCommand(SampleFilteredCommand.FILTERED, { execute: () => { } }); | ||
} | ||
} | ||
|
||
@injectable() | ||
export class SampleFilteredCommandContributionFilter extends NameBasedContributionFilter { | ||
|
||
contributions = [CommandContribution]; | ||
doTest(toTest: string): boolean { | ||
return equals(toTest, false, 'SampleFilteredCommandContribution'); | ||
} | ||
} | ||
|
||
export const bindSampleFilteredCommandContribution = (bind: interfaces.Bind) => { | ||
bind(CommandContribution).to(SampleFilteredCommandContribution); | ||
bind(ContributionFilter).to(SampleFilteredCommandContributionFilter); | ||
}; |
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,36 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 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 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
// @ts-check | ||
describe('Contribution filter', function () { | ||
this.timeout(5000); | ||
const { assert } = chai; | ||
|
||
const { CommandRegistry, CommandContribution } = require('@theia/core/lib/common/command'); | ||
const { SampleFilteredCommandContribution, SampleFilteredCommand } = require('@theia/api-samples/lib/browser/contribution-filter/sample-filtered-command-contribution'); | ||
|
||
const container = window.theia.container; | ||
const commands = container.get(CommandRegistry); | ||
|
||
it('filtered command in container but not in registry', async function () { | ||
const allCommands = container.getAll(CommandContribution); | ||
assert.isDefined(allCommands.find(contribution => contribution instanceof SampleFilteredCommandContribution), | ||
'SampleFilteredCommandContribution is not bound in container'); | ||
const filteredCommand = commands.getCommand(SampleFilteredCommand.FILTERED.id); | ||
assert.isUndefined(filteredCommand, 'SampleFilteredCommandContribution should be filtered out but is present in "CommandRegistry"'); | ||
}); | ||
|
||
}); |
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
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
106 changes: 106 additions & 0 deletions
106
packages/core/src/common/contribution-filter/string-based-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,106 @@ | ||
/******************************************************************************** | ||
* 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'; | ||
import { Filter } from './filter'; | ||
|
||
/** | ||
* Specialized `Filter` class for filters based on string level. Test objects | ||
* are converted to a corresponding string representation with the help of the `toString()`-method | ||
* which enable to implement a `doTest()`-method based on string comparison. | ||
*/ | ||
@injectable() | ||
export abstract class StringBasedFilter<T extends Object> implements Filter<T> { | ||
|
||
/** | ||
* Converts the test object to a corresponding string representation | ||
* that is then used in the `doTest()` method. | ||
* @param toTest Test object that should be converted to string. | ||
*/ | ||
abstract toString(toTest: T): string | undefined; | ||
|
||
/** | ||
* Tests wether the object should be filtered out based on its string representation. | ||
* @param testStr String representation of the test object. | ||
* @returns `true` if the object should be filtered out, `false` otherwise | ||
*/ | ||
abstract doTest(testStr: string): boolean; | ||
|
||
test(contribution: T): boolean { | ||
const testStr = this.toString(contribution); | ||
if (typeof testStr !== 'string') { | ||
return false; | ||
} | ||
|
||
return this.doTest(testStr); | ||
} | ||
} | ||
/** | ||
* Utility function to test wether any of the given string patterns is included in the | ||
* tested string. | ||
* @param testStr String that should be tested. | ||
* @param ignoreCase Flag, to indicate wether the test should be case-sensitive or not. | ||
* @param patterns The set of patterns that should be tested for inclusion | ||
* @returns `true` if any of the given patterns is included in the test string, `false` otherwise | ||
*/ | ||
export function includes(testStr: string, ignoreCase: boolean, ...patterns: string[]): boolean { | ||
if (ignoreCase) { | ||
testStr = testStr.toLowerCase(); | ||
patterns = patterns.map(pattern => pattern.toLowerCase()); | ||
} | ||
return patterns.find(pattern => testStr.includes(pattern)) !== undefined; | ||
} | ||
|
||
/** | ||
* Utility function to test wether any of the given string patterns is equal to the | ||
* tested string. | ||
* @param testStr String that should be tested. | ||
* @param ignoreCase Flag, to indicate wether the test should be case-sensitive or not. | ||
* @param patterns The set of patterns that should be tested for equality | ||
* @returns `true` if any of the given patterns is equal to the test string, `false` otherwise | ||
*/ | ||
export function equals(testStr: string, ignoreCase: boolean, ...patterns: string[]): boolean { | ||
if (ignoreCase) { | ||
testStr = testStr.toLowerCase(); | ||
patterns = patterns.map(pattern => pattern.toLowerCase()); | ||
} | ||
return patterns.find(pattern => testStr === pattern) !== undefined; | ||
|
||
} | ||
|
||
/** | ||
* Utility function to test wether a string matches any of the given regular expression patterns. | ||
* @param testStr String that should be tested. | ||
* @param ignoreCase Flag, to indicate wether the test should be case-sensitive or not. | ||
* @param patterns The set of regular expressions that should be matched. | ||
* @returns `true` if the test string matches any of the given regular expressions, `false` otherwise | ||
*/ | ||
export function matches(testStr: string, ignoreCase: boolean, ...patterns: (RegExp | string)[]): boolean { | ||
const flags = ignoreCase ? 'i' : undefined; | ||
const regexps = patterns.map(pattern => new RegExp(pattern, flags)); | ||
return regexps.find(regexp => regexp.test(testStr)) !== undefined; | ||
} | ||
|
||
/** | ||
* Specialized `StringBasedFilter` that can be used to filter objects based on their constructor name. | ||
*/ | ||
@injectable() | ||
export abstract class NameBasedFilter<T extends Object> extends StringBasedFilter<T> { | ||
toString(toTest: T): string { | ||
return toTest.constructor.name; | ||
} | ||
} | ||
|