Skip to content

Commit

Permalink
Ensure settings and keybindings views open at correct location (micro…
Browse files Browse the repository at this point in the history
…soft#236412)

* make sure settings and keybindings views open at correct location

* fix tests
  • Loading branch information
benibenj authored Dec 17, 2024
1 parent cece885 commit 2a4ed84
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ import { IUserDataProfileService, CURRENT_PROFILE_CONTEXT } from '../../../servi
import { IUserDataProfilesService } from '../../../../platform/userDataProfile/common/userDataProfile.js';
import { isCodeEditor } from '../../../../editor/browser/editorBrowser.js';
import { Categories } from '../../../../platform/action/common/actionCommonCategories.js';
import { resolveCommandsContext } from '../../../browser/parts/editor/editorCommandsContext.js';
import { IEditorGroup, IEditorGroupsService } from '../../../services/editor/common/editorGroupsService.js';
import { IListService } from '../../../../platform/list/browser/listService.js';

const SETTINGS_EDITOR_COMMAND_SEARCH = 'settings.action.search';

Expand Down Expand Up @@ -791,9 +794,10 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
]
});
}
run(accessor: ServicesAccessor, args: string | undefined) {
const query = typeof args === 'string' ? args : undefined;
return accessor.get(IPreferencesService).openGlobalKeybindingSettings(false, { query });
run(accessor: ServicesAccessor, ...args: unknown[]) {
const query = typeof args[0] === 'string' ? args[0] : undefined;
const groupId = getEditorGroupFromArguments(accessor, args)?.id;
return accessor.get(IPreferencesService).openGlobalKeybindingSettings(false, { query, groupId });
}
}));
this._register(MenuRegistry.appendMenuItem(MenuId.MenubarPreferencesMenu, {
Expand Down Expand Up @@ -834,8 +838,9 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
]
});
}
run(accessor: ServicesAccessor) {
return accessor.get(IPreferencesService).openGlobalKeybindingSettings(true);
run(accessor: ServicesAccessor, ...args: unknown[]) {
const groupId = getEditorGroupFromArguments(accessor, args)?.id;
return accessor.get(IPreferencesService).openGlobalKeybindingSettings(true, { groupId });
}
}));
this._register(registerAction2(class extends Action2 {
Expand All @@ -852,8 +857,9 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
run(accessor: ServicesAccessor, ...args: unknown[]) {
const group = getEditorGroupFromArguments(accessor, args);
const editorPane = group?.activeEditorPane;
if (editorPane instanceof KeybindingsEditor) {
editorPane.search('@source:system');
}
Expand All @@ -873,8 +879,9 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
run(accessor: ServicesAccessor, ...args: unknown[]) {
const group = getEditorGroupFromArguments(accessor, args);
const editorPane = group?.activeEditorPane;
if (editorPane instanceof KeybindingsEditor) {
editorPane.search('@source:extension');
}
Expand All @@ -894,8 +901,9 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
run(accessor: ServicesAccessor, args: unknown[]) {
const group = getEditorGroupFromArguments(accessor, args);
const editorPane = group?.activeEditorPane;
if (editorPane instanceof KeybindingsEditor) {
editorPane.search('@source:user');
}
Expand Down Expand Up @@ -1207,11 +1215,12 @@ class PreferencesActionsContribution extends Disposable implements IWorkbenchCon
for (const folder of this.workspaceContextService.getWorkspace().folders) {
const commandId = `_workbench.openFolderSettings.${folder.uri.toString()}`;
if (!CommandsRegistry.getCommand(commandId)) {
CommandsRegistry.registerCommand(commandId, () => {
CommandsRegistry.registerCommand(commandId, (accessor: ServicesAccessor, ...args: any[]) => {
const groupId = getEditorGroupFromArguments(accessor, args)?.id;
if (this.workspaceContextService.getWorkbenchState() === WorkbenchState.FOLDER) {
return this.preferencesService.openWorkspaceSettings({ jsonEditor: false });
return this.preferencesService.openWorkspaceSettings({ jsonEditor: false, groupId });
} else {
return this.preferencesService.openFolderSettings({ folderUri: folder.uri, jsonEditor: false });
return this.preferencesService.openFolderSettings({ folderUri: folder.uri, jsonEditor: false, groupId });
}
});
MenuRegistry.appendMenuItem(MenuId.EditorTitle, {
Expand Down Expand Up @@ -1261,9 +1270,10 @@ class SettingsEditorTitleContribution extends Disposable implements IWorkbenchCo
}]
});
}
run(accessor: ServicesAccessor, args: IOpenSettingsActionOptions) {
args = sanitizeOpenSettingsArgs(args);
return accessor.get(IPreferencesService).openUserSettings({ jsonEditor: false, ...args });
run(accessor: ServicesAccessor, ...args: unknown[]) {
const sanatizedArgs = sanitizeOpenSettingsArgs(args[0]);
const groupId = getEditorGroupFromArguments(accessor, args)?.id;
return accessor.get(IPreferencesService).openUserSettings({ jsonEditor: false, ...sanatizedArgs, groupId });
}
});
};
Expand All @@ -1289,8 +1299,9 @@ class SettingsEditorTitleContribution extends Disposable implements IWorkbenchCo
}]
});
}
run(accessor: ServicesAccessor) {
const editorPane = accessor.get(IEditorService).activeEditorPane;
run(accessor: ServicesAccessor, ...args: unknown[]) {
const group = getEditorGroupFromArguments(accessor, args);
const editorPane = group?.activeEditorPane;
if (editorPane instanceof SettingsEditor2) {
return editorPane.switchToSettingsFile();
}
Expand All @@ -1300,6 +1311,11 @@ class SettingsEditorTitleContribution extends Disposable implements IWorkbenchCo
}
}

function getEditorGroupFromArguments(accessor: ServicesAccessor, args: unknown[]): IEditorGroup | undefined {
const context = resolveCommandsContext(args, accessor.get(IEditorService), accessor.get(IEditorGroupsService), accessor.get(IListService));
return context.groupedEditors[0]?.group;
}

const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
registerWorkbenchContribution2(PreferencesActionsContribution.ID, PreferencesActionsContribution, WorkbenchPhase.BlockStartup);
registerWorkbenchContribution2(PreferencesContribution.ID, PreferencesContribution, WorkbenchPhase.BlockStartup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ export class SettingsEditor2 extends EditorPane {
private async openSettingsFile(options?: ISettingsEditorOptions): Promise<IEditorPane | undefined> {
const currentSettingsTarget = this.settingsTargetsWidget.settingsTarget;

const openOptions: IOpenSettingsOptions = { jsonEditor: true, ...options };
const openOptions: IOpenSettingsOptions = { jsonEditor: true, groupId: this.group.id, ...options };
if (currentSettingsTarget === ConfigurationTarget.USER_LOCAL) {
if (options?.revealSetting) {
const configurationProperties = Registry.as<IConfigurationRegistry>(Extensions.Configuration).getConfigurationProperties();
Expand Down
41 changes: 25 additions & 16 deletions src/vs/workbench/services/preferences/browser/preferencesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import { DEFAULT_EDITOR_ASSOCIATION, IEditorPane } from '../../../common/editor.
import { EditorInput } from '../../../common/editor/editorInput.js';
import { SideBySideEditorInput } from '../../../common/editor/sideBySideEditorInput.js';
import { IJSONEditingService } from '../../configuration/common/jsonEditing.js';
import { GroupDirection, IEditorGroupsService } from '../../editor/common/editorGroupsService.js';
import { IEditorService, SIDE_GROUP, SIDE_GROUP_TYPE } from '../../editor/common/editorService.js';
import { GroupDirection, IEditorGroup, IEditorGroupsService } from '../../editor/common/editorGroupsService.js';
import { IEditorService, SIDE_GROUP } from '../../editor/common/editorService.js';
import { KeybindingsEditorInput } from './keybindingsEditorInput.js';
import { DEFAULT_SETTINGS_EDITOR_SETTING, FOLDER_SETTINGS_PATH, IKeybindingsEditorOptions, IKeybindingsEditorPane, IOpenSettingsOptions, IPreferencesEditorModel, IPreferencesService, ISetting, ISettingsEditorOptions, ISettingsGroup, SETTINGS_AUTHORITY, USE_SPLIT_JSON_SETTING, validateSettingsEditorOptions } from '../common/preferences.js';
import { DEFAULT_SETTINGS_EDITOR_SETTING, FOLDER_SETTINGS_PATH, IKeybindingsEditorPane, IOpenKeybindingsEditorOptions, IOpenSettingsOptions, IPreferencesEditorModel, IPreferencesService, ISetting, ISettingsEditorOptions, ISettingsGroup, SETTINGS_AUTHORITY, USE_SPLIT_JSON_SETTING, validateSettingsEditorOptions } from '../common/preferences.js';
import { SettingsEditor2Input } from '../common/preferencesEditorInput.js';
import { defaultKeybindingsContents, DefaultKeybindingsEditorModel, DefaultRawSettingsEditorModel, DefaultSettings, DefaultSettingsEditorModel, Settings2EditorModel, SettingsEditorModel, WorkspaceConfigurationEditorModel } from '../common/preferencesModels.js';
import { IRemoteAgentService } from '../../remote/common/remoteAgentService.js';
Expand All @@ -48,6 +48,7 @@ import { IURLService } from '../../../../platform/url/common/url.js';
import { compareIgnoreCase } from '../../../../base/common/strings.js';
import { IExtensionService } from '../../extensions/common/extensions.js';
import { IProgressService, ProgressLocation } from '../../../../platform/progress/common/progress.js';
import { findGroup } from '../../editor/common/editorGroupFinder.js';

const emptyEditableSettingsContent = '{\n}';

Expand Down Expand Up @@ -248,8 +249,8 @@ export class PreferencesService extends Disposable implements IPreferencesServic
...options,
focusSearch: true
};
await this.editorService.openEditor(input, validateSettingsEditorOptions(options), options.openToSide ? SIDE_GROUP : undefined);
return this.editorGroupService.activeGroup.activeEditorPane!;
const group = await this.getEditorGroupFromOptions(options);
return (await group.openEditor(input, validateSettingsEditorOptions(options)))!;
}

openApplicationSettings(options: IOpenSettingsOptions = {}): Promise<IEditorPane | undefined> {
Expand Down Expand Up @@ -312,7 +313,7 @@ export class PreferencesService extends Disposable implements IPreferencesServic
return this.open(folderSettingsUri, options);
}

async openGlobalKeybindingSettings(textual: boolean, options?: IKeybindingsEditorOptions): Promise<void> {
async openGlobalKeybindingSettings(textual: boolean, options?: IOpenKeybindingsEditorOptions): Promise<void> {
options = { pinned: true, revealIfOpened: true, ...options };
if (textual) {
const emptyContents = '// ' + nls.localize('emptyKeybindingsHeader', "Place your key bindings in this file to override the defaults") + '\n[\n]';
Expand All @@ -322,18 +323,18 @@ export class PreferencesService extends Disposable implements IPreferencesServic
// Create as needed and open in editor
await this.createIfNotExists(editableKeybindings, emptyContents);
if (openDefaultKeybindings) {
const activeEditorGroup = this.editorGroupService.activeGroup;
const sideEditorGroup = this.editorGroupService.addGroup(activeEditorGroup.id, GroupDirection.RIGHT);
const sourceGroupId = options.groupId ?? this.editorGroupService.activeGroup.id;
const sideEditorGroup = this.editorGroupService.addGroup(sourceGroupId, GroupDirection.RIGHT);
await Promise.all([
this.editorService.openEditor({ resource: this.defaultKeybindingsResource, options: { pinned: true, preserveFocus: true, revealIfOpened: true, override: DEFAULT_EDITOR_ASSOCIATION.id }, label: nls.localize('defaultKeybindings', "Default Keybindings"), description: '' }),
this.editorService.openEditor({ resource: this.defaultKeybindingsResource, options: { pinned: true, preserveFocus: true, revealIfOpened: true, override: DEFAULT_EDITOR_ASSOCIATION.id }, label: nls.localize('defaultKeybindings', "Default Keybindings"), description: '' }, sourceGroupId),
this.editorService.openEditor({ resource: editableKeybindings, options }, sideEditorGroup.id)
]);
} else {
await this.editorService.openEditor({ resource: editableKeybindings, options });
await this.editorService.openEditor({ resource: editableKeybindings, options }, options.groupId);
}

} else {
const editor = (await this.editorService.openEditor(this.instantiationService.createInstance(KeybindingsEditorInput), { ...options })) as IKeybindingsEditorPane;
const editor = (await this.editorService.openEditor(this.instantiationService.createInstance(KeybindingsEditorInput), { ...options }, options.groupId)) as IKeybindingsEditorPane;
if (options.query) {
editor.search(options.query);
}
Expand All @@ -345,16 +346,24 @@ export class PreferencesService extends Disposable implements IPreferencesServic
return this.editorService.openEditor({ resource: this.defaultKeybindingsResource, label: nls.localize('defaultKeybindings', "Default Keybindings") });
}

private async getEditorGroupFromOptions(options: IOpenSettingsOptions): Promise<IEditorGroup> {
let group = options?.groupId !== undefined ? this.editorGroupService.getGroup(options.groupId) ?? this.editorGroupService.activeGroup : this.editorGroupService.activeGroup;
if (options.openToSide) {
group = (await this.instantiationService.invokeFunction(findGroup, {}, SIDE_GROUP))[0];
}
return group;
}

private async openSettingsJson(resource: URI, options: IOpenSettingsOptions): Promise<IEditorPane | undefined> {
const group = options?.openToSide ? SIDE_GROUP : undefined;
const group = await this.getEditorGroupFromOptions(options);
const editor = await this.doOpenSettingsJson(resource, options, group);
if (editor && options?.revealSetting) {
await this.revealSetting(options.revealSetting.key, !!options.revealSetting.edit, editor, resource);
}
return editor;
}

private async doOpenSettingsJson(resource: URI, options: ISettingsEditorOptions, group?: SIDE_GROUP_TYPE): Promise<IEditorPane | undefined> {
private async doOpenSettingsJson(resource: URI, options: ISettingsEditorOptions, group: IEditorGroup): Promise<IEditorPane | undefined> {
const openSplitJSON = !!this.configurationService.getValue(USE_SPLIT_JSON_SETTING);
const openDefaultSettings = !!this.configurationService.getValue(DEFAULT_SETTINGS_EDITOR_SETTING);
if (openSplitJSON || openDefaultSettings) {
Expand All @@ -364,15 +373,15 @@ export class PreferencesService extends Disposable implements IPreferencesServic
const configurationTarget = options?.target ?? ConfigurationTarget.USER;
const editableSettingsEditorInput = await this.getOrCreateEditableSettingsEditorInput(configurationTarget, resource);
options = { ...options, pinned: true };
return await this.editorService.openEditor(editableSettingsEditorInput, validateSettingsEditorOptions(options), group);
return await group.openEditor(editableSettingsEditorInput, { ...validateSettingsEditorOptions(options) });
}

private async doOpenSplitJSON(resource: URI, options: ISettingsEditorOptions = {}, group?: SIDE_GROUP_TYPE): Promise<IEditorPane | undefined> {
private async doOpenSplitJSON(resource: URI, options: ISettingsEditorOptions = {}, group: IEditorGroup,): Promise<IEditorPane | undefined> {
const configurationTarget = options.target ?? ConfigurationTarget.USER;
await this.createSettingsIfNotExists(configurationTarget, resource);
const preferencesEditorInput = this.createSplitJsonEditorInput(configurationTarget, resource);
options = { ...options, pinned: true };
return this.editorService.openEditor(preferencesEditorInput, validateSettingsEditorOptions(options), group);
return group.openEditor(preferencesEditorInput, validateSettingsEditorOptions(options));
}

public createSplitJsonEditorInput(configurationTarget: ConfigurationTarget, resource: URI): EditorInput {
Expand Down
7 changes: 6 additions & 1 deletion src/vs/workbench/services/preferences/common/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ export interface ISettingsEditorOptions extends IEditorOptions {
export interface IOpenSettingsOptions extends ISettingsEditorOptions {
jsonEditor?: boolean;
openToSide?: boolean;
groupId?: number;
}

export function validateSettingsEditorOptions(options: ISettingsEditorOptions): ISettingsEditorOptions {
Expand All @@ -231,6 +232,10 @@ export interface IKeybindingsEditorOptions extends IEditorOptions {
query?: string;
}

export interface IOpenKeybindingsEditorOptions extends IKeybindingsEditorOptions {
groupId?: number;
}

export const IPreferencesService = createDecorator<IPreferencesService>('preferencesService');

export interface IPreferencesService {
Expand All @@ -254,7 +259,7 @@ export interface IPreferencesService {
openRemoteSettings(options?: IOpenSettingsOptions): Promise<IEditorPane | undefined>;
openWorkspaceSettings(options?: IOpenSettingsOptions): Promise<IEditorPane | undefined>;
openFolderSettings(options: IOpenSettingsOptions & { folderUri: IOpenSettingsOptions['folderUri'] }): Promise<IEditorPane | undefined>;
openGlobalKeybindingSettings(textual: boolean, options?: IKeybindingsEditorOptions): Promise<void>;
openGlobalKeybindingSettings(textual: boolean, options?: IOpenKeybindingsEditorOptions): Promise<void>;
openDefaultKeybindingsFile(): Promise<IEditorPane | undefined>;
openLanguageSpecificSettings(languageId: string, options?: IOpenSettingsOptions): Promise<IEditorPane | undefined>;
getEditableSettingsURI(configurationTarget: ConfigurationTarget, resource?: URI): Promise<URI | null>;
Expand Down
Loading

0 comments on commit 2a4ed84

Please sign in to comment.