Skip to content

Commit

Permalink
fix: clean up PreferencesActionContribution
Browse files Browse the repository at this point in the history
Fixes #180319
Fixes #175598
  • Loading branch information
rzhao271 committed Jul 3, 2023
1 parent 89229f7 commit 85ae28f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { ConfigureLanguageBasedSettingsAction } from 'vs/workbench/contrib/prefe
import { SettingsEditorContribution } from 'vs/workbench/contrib/preferences/browser/preferencesEditor';
import { preferencesOpenSettingsIcon } from 'vs/workbench/contrib/preferences/browser/preferencesIcons';
import { SettingsEditor2, SettingsFocusContext } from 'vs/workbench/contrib/preferences/browser/settingsEditor2';
import { CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDINGS_SEARCH_FOCUS, CONTEXT_KEYBINDING_FOCUS, CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_EDITOR_IN_USER_TAB, CONTEXT_SETTINGS_JSON_EDITOR, CONTEXT_SETTINGS_ROW_FOCUS, CONTEXT_SETTINGS_SEARCH_FOCUS, CONTEXT_TOC_ROW_FOCUS, CONTEXT_WHEN_FOCUS, KEYBINDINGS_EDITOR_COMMAND_ACCEPT_WHEN, KEYBINDINGS_EDITOR_COMMAND_ADD, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_HISTORY, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, KEYBINDINGS_EDITOR_COMMAND_COPY, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND_TITLE, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_DEFINE_WHEN, KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS, KEYBINDINGS_EDITOR_COMMAND_RECORD_SEARCH_KEYS, KEYBINDINGS_EDITOR_COMMAND_REJECT_WHEN, KEYBINDINGS_EDITOR_COMMAND_REMOVE, KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_SEARCH, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR, KEYBINDINGS_EDITOR_COMMAND_SORTBY_PRECEDENCE, KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_EXTENSION_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS, REQUIRE_TRUSTED_WORKSPACE_SETTING_TAG, SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, SETTINGS_EDITOR_COMMAND_SHOW_CONTEXT_MENU } from 'vs/workbench/contrib/preferences/common/preferences';
import { CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDINGS_SEARCH_FOCUS, CONTEXT_KEYBINDING_FOCUS, CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_JSON_EDITOR, CONTEXT_SETTINGS_ROW_FOCUS, CONTEXT_SETTINGS_SEARCH_FOCUS, CONTEXT_TOC_ROW_FOCUS, CONTEXT_WHEN_FOCUS, KEYBINDINGS_EDITOR_COMMAND_ACCEPT_WHEN, KEYBINDINGS_EDITOR_COMMAND_ADD, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_HISTORY, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, KEYBINDINGS_EDITOR_COMMAND_COPY, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND_TITLE, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_DEFINE_WHEN, KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS, KEYBINDINGS_EDITOR_COMMAND_RECORD_SEARCH_KEYS, KEYBINDINGS_EDITOR_COMMAND_REJECT_WHEN, KEYBINDINGS_EDITOR_COMMAND_REMOVE, KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_SEARCH, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR, KEYBINDINGS_EDITOR_COMMAND_SORTBY_PRECEDENCE, KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_EXTENSION_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS, REQUIRE_TRUSTED_WORKSPACE_SETTING_TAG, SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, SETTINGS_EDITOR_COMMAND_SHOW_CONTEXT_MENU } from 'vs/workbench/contrib/preferences/common/preferences';
import { PreferencesContribution } from 'vs/workbench/contrib/preferences/common/preferencesContribution';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
Expand All @@ -57,8 +57,6 @@ const SETTINGS_EDITOR_COMMAND_FOCUS_CONTROL = 'settings.action.focusSettingContr
const SETTINGS_EDITOR_COMMAND_FOCUS_UP = 'settings.action.focusLevelUp';

const SETTINGS_EDITOR_COMMAND_SWITCH_TO_JSON = 'settings.switchToJSON';
const SETTINGS_EDITOR_COMMAND_SWITCH_TO_APPLICATION_JSON = 'settings.switchToApplicationJSON';
const SETTINGS_EDITOR_COMMAND_SWITCH_TO_CURRENT_PROFILE_JSON = 'settings.switchToCurrentProfileJSON';
const SETTINGS_EDITOR_COMMAND_FILTER_ONLINE = 'settings.filterByOnline';
const SETTINGS_EDITOR_COMMAND_FILTER_UNTRUSTED = 'settings.filterUntrusted';

Expand Down Expand Up @@ -122,7 +120,7 @@ Registry.as<IEditorFactoryRegistry>(EditorExtensions.EditorFactory).registerEdit

const OPEN_USER_SETTINGS_UI_TITLE = { value: nls.localize('openSettings2', "Open Settings (UI)"), original: 'Open Settings (UI)' };
const OPEN_USER_SETTINGS_JSON_TITLE = { value: nls.localize('openUserSettingsJson', "Open User Settings (JSON)"), original: 'Open User Settings (JSON)' };
const OPEN_CURRENT_PROFILE_SETTINGS_JSON_TITLE = { value: nls.localize('openCurrentProfileSettingsJson', "Open Current Profile Settings (JSON)"), original: 'Open Current Profile Settings (JSON)' };
const OPEN_APPLICATION_SETTINGS_JSON_TITLE = { value: nls.localize('openApplicationSettingsJson', "Open Application Settings (JSON)"), original: 'Open Application Settings (JSON)' };
const category = { value: nls.localize('preferences', "Preferences"), original: 'Preferences' };

interface IOpenSettingsActionOptions {
Expand Down Expand Up @@ -209,30 +207,27 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
}
});

const that = this;
const registerOpenSettingsJsonCommandDisposable = this._register(new MutableDisposable());
const registerOpenSettingsJsonCommand = () => {
registerOpenSettingsJsonCommandDisposable.value = registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.openSettingsJson',
title: that.userDataProfileService.currentProfile.isDefault ? OPEN_USER_SETTINGS_JSON_TITLE : OPEN_CURRENT_PROFILE_SETTINGS_JSON_TITLE,
category,
f1: true,
});
}
run(accessor: ServicesAccessor, args: IOpenSettingsActionOptions) {
args = sanitizeOpenSettingsArgs(args);
return accessor.get(IPreferencesService).openSettings({ jsonEditor: true, ...args });
}
});
};
registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.openSettingsJson',
title: OPEN_USER_SETTINGS_JSON_TITLE,
category,
f1: true,
});
}
run(accessor: ServicesAccessor, args: IOpenSettingsActionOptions) {
args = sanitizeOpenSettingsArgs(args);
return accessor.get(IPreferencesService).openSettings({ jsonEditor: true, ...args });
}
});

const that = this;
registerAction2(class extends Action2 {
constructor() {
super({
id: 'workbench.action.openApplicationSettingsJson',
title: OPEN_USER_SETTINGS_JSON_TITLE,
title: OPEN_APPLICATION_SETTINGS_JSON_TITLE,
category,
menu: {
id: MenuId.CommandPalette,
Expand Down Expand Up @@ -304,62 +299,10 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
});
};

const openJsonFromSettingsEditorDisposableStore = this._register(new DisposableStore());
const openJsonFromSettingsEditorDisposable = this._register(new MutableDisposable());
const registerOpenJsonFromSettingsEditorAction = () => {
openJsonFromSettingsEditorDisposableStore.clear();
if (!this.userDataProfileService.currentProfile.isDefault) {
// When the default profile is not active, the action for the User tab needs a dropdown
// because User tab settings in that case are actually saved in two separate files.
const submenuId = MenuId.for('PreferencesSubMenu');
openJsonFromSettingsEditorDisposableStore.add(registerAction2(class extends Action2 {
constructor() {
super({
id: SETTINGS_EDITOR_COMMAND_SWITCH_TO_CURRENT_PROFILE_JSON,
title: OPEN_CURRENT_PROFILE_SETTINGS_JSON_TITLE,
menu: [{ id: submenuId, order: 1 }]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
if (editorPane instanceof SettingsEditor2) {
return editorPane.switchToSettingsFile();
}
return null;
}
}));
openJsonFromSettingsEditorDisposableStore.add(registerAction2(class extends Action2 {
constructor() {
super({
id: SETTINGS_EDITOR_COMMAND_SWITCH_TO_APPLICATION_JSON,
title: OPEN_USER_SETTINGS_JSON_TITLE,
menu: [{ id: submenuId, order: 2 }]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
if (editorPane instanceof SettingsEditor2) {
return editorPane.switchToApplicationSettingsFile();
}
return null;
}
}));
openJsonFromSettingsEditorDisposableStore.add(MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
title: { value: nls.localize('openSettingsJson', "Open Settings (JSON)"), original: 'Open Settings (JSON)' },
submenu: submenuId,
icon: preferencesOpenSettingsIcon,
when: ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_EDITOR_IN_USER_TAB, CONTEXT_SETTINGS_JSON_EDITOR.toNegated()),
group: 'navigation',
order: 1
}));
}

let openSettingsJsonWhen = ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_JSON_EDITOR.toNegated());
if (!this.userDataProfileService.currentProfile.isDefault) {
// If we're not in the default profile, we already created the action for the User tab above,
// so we want to make sure the user is not in the User tab for this more general action.
openSettingsJsonWhen = ContextKeyExpr.and(openSettingsJsonWhen, CONTEXT_SETTINGS_EDITOR_IN_USER_TAB.toNegated());
}
openJsonFromSettingsEditorDisposableStore.add(registerAction2(class extends Action2 {
const openSettingsJsonWhen = ContextKeyExpr.and(CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_JSON_EDITOR.toNegated());
openJsonFromSettingsEditorDisposable.value = registerAction2(class extends Action2 {
constructor() {
super({
id: SETTINGS_EDITOR_COMMAND_SWITCH_TO_JSON,
Expand All @@ -380,17 +323,15 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
}
return null;
}
}));
});
};

registerOpenUserSettingsEditorFromJsonAction();
registerOpenJsonFromSettingsEditorAction();
registerOpenSettingsJsonCommand();

this._register(this.userDataProfileService.onDidChangeCurrentProfile(() => {
registerOpenUserSettingsEditorFromJsonAction();
registerOpenJsonFromSettingsEditorAction();
registerOpenSettingsJsonCommand();
}));

registerAction2(class extends Action2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import { isWorkspaceFolder, IWorkspaceContextService, IWorkspaceFolder, Workbenc
import { settingsEditIcon, settingsScopeDropDownIcon } from 'vs/workbench/contrib/preferences/browser/preferencesIcons';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { CONTEXT_SETTINGS_EDITOR_IN_USER_TAB } from 'vs/workbench/contrib/preferences/common/preferences';

export class FolderSettingsActionViewItem extends BaseActionViewItem {

private _folder: IWorkspaceFolder | null;
Expand Down Expand Up @@ -217,7 +215,6 @@ export class SettingsTargetsWidget extends Widget {
private folderSettingsAction!: Action;
private folderSettings!: FolderSettingsActionViewItem;
private options: ISettingsTargetsWidgetOptions;
private inUserTab: IContextKey<boolean>;

private _settingsTarget: SettingsTarget | null = null;

Expand All @@ -231,15 +228,13 @@ export class SettingsTargetsWidget extends Widget {
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService,
@ILabelService private readonly labelService: ILabelService,
@ILanguageService private readonly languageService: ILanguageService,
@IContextKeyService contextKeyService: IContextKeyService,
@ILanguageService private readonly languageService: ILanguageService
) {
super();
this.options = options ?? {};
this.create(parent);
this._register(this.contextService.onDidChangeWorkbenchState(() => this.onWorkbenchStateChanged()));
this._register(this.contextService.onDidChangeWorkspaceFolders(() => this.update()));
this.inUserTab = CONTEXT_SETTINGS_EDITOR_IN_USER_TAB.bindTo(contextKeyService);
}

private resetLabels() {
Expand Down Expand Up @@ -297,7 +292,6 @@ export class SettingsTargetsWidget extends Widget {
} else {
this.folderSettings.action.checked = false;
}
this.inUserTab.set(this.userLocalSettings.checked);
}

setResultCount(settingsTarget: SettingsTarget, count: number): void {
Expand Down
10 changes: 1 addition & 9 deletions src/vs/workbench/contrib/preferences/browser/settingsEditor2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,17 +707,12 @@ export class SettingsEditor2 extends EditorPane {
}
}

switchToApplicationSettingsFile(): Promise<IEditorPane | undefined> {
const query = parseQuery(this.searchWidget.getValue()).query;
return this.openSettingsFile({ query }, true);
}

switchToSettingsFile(): Promise<IEditorPane | undefined> {
const query = parseQuery(this.searchWidget.getValue()).query;
return this.openSettingsFile({ query });
}

private async openSettingsFile(options?: ISettingsEditorOptions, forceOpenApplicationSettings?: boolean): Promise<IEditorPane | undefined> {
private async openSettingsFile(options?: ISettingsEditorOptions): Promise<IEditorPane | undefined> {
const currentSettingsTarget = this.settingsTargetsWidget.settingsTarget;

const openOptions: IOpenSettingsOptions = { jsonEditor: true, ...options };
Expand All @@ -729,9 +724,6 @@ export class SettingsEditor2 extends EditorPane {
return this.preferencesService.openApplicationSettings(openOptions);
}
}
if (forceOpenApplicationSettings) {
return this.preferencesService.openApplicationSettings(openOptions);
}
return this.preferencesService.openUserSettings(openOptions);
} else if (currentSettingsTarget === ConfigurationTarget.USER_REMOTE) {
return this.preferencesService.openRemoteSettings(openOptions);
Expand Down
1 change: 0 additions & 1 deletion src/vs/workbench/contrib/preferences/common/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const CONTEXT_KEYBINDINGS_EDITOR = new RawContextKey<boolean>('inKeybindi
export const CONTEXT_KEYBINDINGS_SEARCH_FOCUS = new RawContextKey<boolean>('inKeybindingsSearch', false);
export const CONTEXT_KEYBINDING_FOCUS = new RawContextKey<boolean>('keybindingFocus', false);
export const CONTEXT_WHEN_FOCUS = new RawContextKey<boolean>('whenFocus', false);
export const CONTEXT_SETTINGS_EDITOR_IN_USER_TAB = new RawContextKey<boolean>('inSettingsEditorUserTab', false);

export const KEYBINDINGS_EDITOR_COMMAND_SEARCH = 'keybindings.editor.searchKeybindings';
export const KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS = 'keybindings.editor.clearSearchResults';
Expand Down

0 comments on commit 85ae28f

Please sign in to comment.