Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/preferences/browser/settingsSearchMenu.ts
5270 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { IActionViewItemOptions } from '../../../../base/browser/ui/actionbar/actionViewItems.js';
7
import { AnchorAlignment } from '../../../../base/browser/ui/contextview/contextview.js';
8
import { DropdownMenuActionViewItem } from '../../../../base/browser/ui/dropdown/dropdownActionViewItem.js';
9
import { IAction, IActionRunner, Separator } from '../../../../base/common/actions.js';
10
import { SuggestController } from '../../../../editor/contrib/suggest/browser/suggestController.js';
11
import { localize } from '../../../../nls.js';
12
import { IContextMenuService } from '../../../../platform/contextview/browser/contextView.js';
13
import { SuggestEnabledInput } from '../../codeEditor/browser/suggestEnabledInput/suggestEnabledInput.js';
14
import { ADVANCED_SETTING_TAG, EXTENSION_SETTING_TAG, FEATURE_SETTING_TAG, GENERAL_TAG_SETTING_TAG, ID_SETTING_TAG, LANGUAGE_SETTING_TAG, MODIFIED_SETTING_TAG, POLICY_SETTING_TAG } from '../common/preferences.js';
15
16
export class SettingsSearchFilterDropdownMenuActionViewItem extends DropdownMenuActionViewItem {
17
private readonly suggestController: SuggestController | null;
18
19
constructor(
20
action: IAction,
21
options: IActionViewItemOptions,
22
actionRunner: IActionRunner | undefined,
23
private readonly searchWidget: SuggestEnabledInput,
24
@IContextMenuService contextMenuService: IContextMenuService
25
) {
26
super(action,
27
{ getActions: () => this.getActions() },
28
contextMenuService,
29
{
30
...options,
31
actionRunner,
32
classNames: action.class,
33
anchorAlignmentProvider: () => AnchorAlignment.RIGHT,
34
menuAsChild: true
35
}
36
);
37
38
this.suggestController = SuggestController.get(this.searchWidget.inputWidget);
39
}
40
41
override render(container: HTMLElement): void {
42
super.render(container);
43
}
44
45
private doSearchWidgetAction(queryToAppend: string, triggerSuggest: boolean) {
46
this.searchWidget.setValue(this.searchWidget.getValue().trimEnd() + ' ' + queryToAppend);
47
this.searchWidget.focus();
48
if (triggerSuggest && this.suggestController) {
49
this.suggestController.triggerSuggest();
50
}
51
}
52
53
/**
54
* The created action appends a query to the search widget search string. It optionally triggers suggestions.
55
*/
56
private createAction(id: string, label: string, tooltip: string, queryToAppend: string, triggerSuggest: boolean): IAction {
57
return {
58
id,
59
label,
60
tooltip,
61
class: undefined,
62
enabled: true,
63
run: () => { this.doSearchWidgetAction(queryToAppend, triggerSuggest); }
64
};
65
}
66
67
/**
68
* The created action appends a query to the search widget search string, if the query does not exist.
69
* Otherwise, it removes the query from the search widget search string.
70
* The action does not trigger suggestions after adding or removing the query.
71
*/
72
private createToggleAction(id: string, label: string, tooltip: string, queryToAppend: string): IAction {
73
const splitCurrentQuery = this.searchWidget.getValue().split(' ');
74
const queryContainsQueryToAppend = splitCurrentQuery.includes(queryToAppend);
75
return {
76
id,
77
label,
78
tooltip,
79
class: undefined,
80
enabled: true,
81
checked: queryContainsQueryToAppend,
82
run: () => {
83
if (!queryContainsQueryToAppend) {
84
const trimmedCurrentQuery = this.searchWidget.getValue().trimEnd();
85
const newQuery = trimmedCurrentQuery ? trimmedCurrentQuery + ' ' + queryToAppend : queryToAppend;
86
this.searchWidget.setValue(newQuery);
87
} else {
88
const queryWithRemovedTags = this.searchWidget.getValue().split(' ')
89
.filter(word => word !== queryToAppend).join(' ');
90
this.searchWidget.setValue(queryWithRemovedTags);
91
}
92
this.searchWidget.focus();
93
}
94
};
95
}
96
97
private createMutuallyExclusiveToggleAction(id: string, label: string, tooltip: string, filter: string, excludeFilters: string[]): IAction {
98
const isFilterEnabled = this.searchWidget.getValue().split(' ').includes(filter);
99
return {
100
id,
101
label,
102
tooltip,
103
class: undefined,
104
enabled: true,
105
checked: isFilterEnabled,
106
run: () => {
107
if (isFilterEnabled) {
108
const queryWithRemovedTags = this.searchWidget.getValue().split(' ')
109
.filter(word => word !== filter).join(' ');
110
this.searchWidget.setValue(queryWithRemovedTags);
111
} else {
112
let newQuery = this.searchWidget.getValue().split(' ')
113
.filter(word => !excludeFilters.includes(word) && word !== filter)
114
.join(' ')
115
.trimEnd();
116
newQuery = newQuery ? newQuery + ' ' + filter : filter;
117
this.searchWidget.setValue(newQuery);
118
}
119
this.searchWidget.focus();
120
}
121
};
122
}
123
124
getActions(): IAction[] {
125
return [
126
this.createToggleAction(
127
'modifiedSettingsSearch',
128
localize('modifiedSettingsSearch', "Modified"),
129
localize('modifiedSettingsSearchTooltip', "Add or remove modified settings filter"),
130
`@${MODIFIED_SETTING_TAG}`
131
),
132
new Separator(),
133
this.createAction(
134
'extSettingsSearch',
135
localize('extSettingsSearch', "Extension ID..."),
136
localize('extSettingsSearchTooltip', "Add extension ID filter"),
137
`@${EXTENSION_SETTING_TAG}`,
138
true
139
),
140
this.createAction(
141
'featuresSettingsSearch',
142
localize('featureSettingsSearch', "Feature..."),
143
localize('featureSettingsSearchTooltip', "Add feature filter"),
144
`@${FEATURE_SETTING_TAG}`,
145
true
146
),
147
this.createAction(
148
'tagSettingsSearch',
149
localize('tagSettingsSearch', "Tag..."),
150
localize('tagSettingsSearchTooltip', "Add tag filter"),
151
`@${GENERAL_TAG_SETTING_TAG}`,
152
true
153
),
154
this.createAction(
155
'langSettingsSearch',
156
localize('langSettingsSearch', "Language..."),
157
localize('langSettingsSearchTooltip', "Add language ID filter"),
158
`@${LANGUAGE_SETTING_TAG}`,
159
true
160
),
161
this.createAction(
162
'idSettingsSearch',
163
localize('idSettingsSearch', "Setting ID..."),
164
localize('idSettingsSearchTooltip', "Add Setting ID filter"),
165
`@${ID_SETTING_TAG}`,
166
false
167
),
168
new Separator(),
169
this.createToggleAction(
170
'onlineSettingsSearch',
171
localize('onlineSettingsSearch', "Online services"),
172
localize('onlineSettingsSearchTooltip', "Show settings for online services"),
173
'@tag:usesOnlineServices'
174
),
175
this.createToggleAction(
176
'policySettingsSearch',
177
localize('policySettingsSearch', "Organization policies"),
178
localize('policySettingsSearchTooltip', "Show organization policy settings"),
179
`@${POLICY_SETTING_TAG}`
180
),
181
new Separator(),
182
this.createMutuallyExclusiveToggleAction(
183
'stableSettingsSearch',
184
localize('stableSettings', "Stable"),
185
localize('stableSettingsSearchTooltip', "Show stable settings"),
186
`@stable`,
187
['@tag:preview', '@tag:experimental']
188
),
189
this.createMutuallyExclusiveToggleAction(
190
'previewSettingsSearch',
191
localize('previewSettings', "Preview"),
192
localize('previewSettingsSearchTooltip', "Show preview settings"),
193
`@tag:preview`,
194
['@stable', '@tag:experimental']
195
),
196
this.createMutuallyExclusiveToggleAction(
197
'experimentalSettingsSearch',
198
localize('experimental', "Experimental"),
199
localize('experimentalSettingsSearchTooltip', "Show experimental settings"),
200
`@tag:experimental`,
201
['@stable', '@tag:preview']
202
),
203
new Separator(),
204
this.createToggleAction(
205
'advancedSettingsSearch',
206
localize('advancedSettingsSearch', "Advanced"),
207
localize('advancedSettingsSearchTooltip', "Show advanced settings"),
208
`@tag:${ADVANCED_SETTING_TAG}`,
209
),
210
];
211
}
212
}
213
214