Skip to content

Commit

Permalink
Improve actions
Browse files Browse the repository at this point in the history
- Fix modified action to act as a toggle
- Make some actions toggle suggestions
  • Loading branch information
rzhao271 committed Apr 11, 2022
1 parent 22420ef commit 1ed33eb
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions src/vs/workbench/contrib/preferences/browser/settingsSearchMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AnchorAlignment } from 'vs/base/browser/ui/contextview/contextview';
import { DropdownMenuActionViewItem } from 'vs/base/browser/ui/dropdown/dropdownActionViewItem';
import { IAction, IActionRunner } from 'vs/base/common/actions';
import { localize } from 'vs/nls';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { SuggestEnabledInput } from 'vs/workbench/contrib/codeEditor/browser/suggestEnabledInput/suggestEnabledInput';
import { EXTENSION_SETTING_TAG, FEATURE_SETTING_TAG, GENERAL_TAG_SETTING_TAG, ID_SETTING_TAG, LANGUAGE_SETTING_TAG, MODIFIED_SETTING_TAG } from 'vs/workbench/contrib/preferences/common/preferences';
Expand All @@ -16,7 +17,8 @@ export class SettingsSearchFilterDropdownMenuActionViewItem extends DropdownMenu
action: IAction,
actionRunner: IActionRunner | undefined,
private readonly searchWidget: SuggestEnabledInput,
@IContextMenuService contextMenuService: IContextMenuService
@IContextMenuService contextMenuService: IContextMenuService,
@ICommandService private readonly commandService: ICommandService
) {
super(action,
{ getActions: () => this.getActions() },
Expand All @@ -34,60 +36,89 @@ export class SettingsSearchFilterDropdownMenuActionViewItem extends DropdownMenu
super.render(container);
}

private appendToSearchWidgetValue(s: string) {
this.searchWidget.setValue(this.searchWidget.getValue().trimEnd() + ' ' + s);
private doSearchWidgetAction(queryToAppend: string, triggerSuggest: boolean) {
this.searchWidget.setValue(this.searchWidget.getValue().trimEnd() + ' ' + queryToAppend);
this.searchWidget.focus();
if (triggerSuggest) {
this.commandService.executeCommand('editor.action.triggerSuggest');
}
}

private createAction(id: string, label: string, tooltip: string, queryToAppend: string): IAction {
private createAction(id: string, label: string, tooltip: string, queryToAppend: string, triggerSuggest: boolean): IAction {
return {
id,
label,
tooltip,
class: undefined,
enabled: true,
checked: false,
run: () => { this.appendToSearchWidgetValue(queryToAppend); },
run: () => { this.doSearchWidgetAction(queryToAppend, triggerSuggest); },
dispose: () => { }
};
}

private createModifiedAction(): IAction {
// The modified action works slightly differently than the other actions.
// It is more like a checkbox on/off toggle.
const queryContainsModifiedTag = this.searchWidget.getValue().split(' ').some(word => word === `@${MODIFIED_SETTING_TAG}`);
return {
id: 'modifiedSettingsSearch',
label: localize('modifiedSettingsSearch', "Modified"),
tooltip: localize('modifiedSettingsSearchTooltip', "View modified settings only"),
class: undefined,
enabled: true,
checked: queryContainsModifiedTag,
run: () => {
// Append the tag, otherwise remove it from the query.
if (!queryContainsModifiedTag) {
this.searchWidget.setValue(this.searchWidget.getValue().trimEnd() + ` @${MODIFIED_SETTING_TAG}`);
} else {
const queryWithoutModifiedTag = this.searchWidget.getValue().split(' ').filter(word => word !== `@${MODIFIED_SETTING_TAG}`).join(' ');
this.searchWidget.setValue(queryWithoutModifiedTag);
}
this.searchWidget.focus();
},
dispose: () => { }
};
}

getActions(): IAction[] {
return [
this.createAction(
'modifiedSettingsSearch',
localize('modifiedSettingsSearch', "Modified"),
localize('modifiedSettingsSearchTooltip', "View modified settings only"),
`@${MODIFIED_SETTING_TAG} `
),
this.createModifiedAction(),
this.createAction(
'extSettingsSearch',
localize('extSettingsSearch', "Extension ID"),
localize('extSettingsSearchTooltip', "Add extension ID filter"),
`@${EXTENSION_SETTING_TAG}`
`@${EXTENSION_SETTING_TAG}`,
false
),
this.createAction(
'featuresSettingsSearch',
localize('featureSettingsSearch', "Feature"),
localize('featureSettingsSearchTooltip', "Add feature filter"),
`@${FEATURE_SETTING_TAG}`
`@${FEATURE_SETTING_TAG}`,
true
),
this.createAction(
'idSettingsSearch',
localize('idSettingsSearch', "Setting ID"),
localize('idSettingsSearchTooltip', "Add setting ID filter"),
`@${ID_SETTING_TAG}`
`@${ID_SETTING_TAG}`,
false
),
this.createAction(
'langSettingsSearch',
localize('langSettingsSearch', "Language"),
localize('langSettingsSearchTooltip', "Add language ID filter"),
`@${LANGUAGE_SETTING_TAG}`
`@${LANGUAGE_SETTING_TAG}`,
true
),
this.createAction(
'tagSettingsSearch',
localize('tagSettingsSearch', "Tag"),
localize('tagSettingsSearchTooltip', "Add tag filter"),
`@${GENERAL_TAG_SETTING_TAG}`
`@${GENERAL_TAG_SETTING_TAG}`,
true
),
];
}
Expand Down

0 comments on commit 1ed33eb

Please sign in to comment.