forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensionQuery.ts
More file actions
81 lines (63 loc) · 2.38 KB
/
Copy pathextensionQuery.ts
File metadata and controls
81 lines (63 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. 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[command]) {
return subcommands[command].map(subcommand => `@${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;
}
}