Files
azuredatastudio/src/vs/workbench/contrib/extensions/common/extensionQuery.ts
Anthony Dresser 0b7e7ddbf9 Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973 (#6381)
* Merge from vscode 8e0f348413f4f616c23a88ae30030efa85811973

* disable strict null check
2019-07-15 22:35:46 -07:00

80 lines
2.4 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { flatten } from 'vs/base/common/arrays';
export class Query {
constructor(public value: string, public sortBy: string, public groupBy: string) {
this.value = value.trim();
}
static suggestions(query: string): string[] {
const commands = ['installed', 'outdated', 'enabled', 'disabled', 'builtin', 'recommended', 'sort', 'category', 'tag', 'ext'];
const subcommands = {
'sort': ['installs', 'rating', 'name'],
'category': ['"programming languages"', 'snippets', 'linters', 'themes', 'debuggers', 'formatters', 'keymaps', '"scm providers"', 'other', '"extension packs"', '"language packs"'],
'tag': [''],
'ext': ['']
};
let queryContains = (substr: string) => query.indexOf(substr) > -1;
let hasSort = subcommands.sort.some(subcommand => queryContains(`@sort:${subcommand}`));
let hasCategory = subcommands.category.some(subcommand => queryContains(`@category:${subcommand}`));
return flatten(
commands.map(command => {
if (hasSort && command === 'sort' || hasCategory && command === 'category') {
return [];
}
if ((subcommands as any)[command]) {
return (subcommands as any)[command].map((subcommand: string) => `@${command}:${subcommand}${subcommand === '' ? '' : ' '}`);
}
else {
return [`@${command} `];
}
}));
}
static parse(value: string): Query {
let sortBy = '';
value = value.replace(/@sort:(\w+)(-\w*)?/g, (match, by: string, order: string) => {
sortBy = by;
return '';
});
let groupBy = '';
value = value.replace(/@group:(\w+)(-\w*)?/g, (match, by: string, order: string) => {
groupBy = by;
return '';
});
return new Query(value, sortBy, groupBy);
}
toString(): string {
let result = this.value;
if (this.sortBy) {
result = `${result}${result ? ' ' : ''}@sort:${this.sortBy}`;
}
if (this.groupBy) {
result = `${result}${result ? ' ' : ''}@group:${this.groupBy}`;
}
return result;
}
isValid(): boolean {
return !/@outdated/.test(this.value);
}
equals(other: Query): boolean {
return this.value === other.value && this.sortBy === other.sortBy;
}
}