Skip to content

Commit

Permalink
Simplify preferencesService open code
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Aug 4, 2021
1 parent 6e86978 commit 0e05663
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { KeyChord, KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { isObject } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import 'vs/css!./media/preferences';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
Expand Down Expand Up @@ -126,6 +127,10 @@ interface IOpenSettingsActionOptions {
}

function sanitizeOpenSettingsArgs(args: any): IOpenSettingsActionOptions {
if (!isObject(args)) {
args = {};
}

return {
openToSide: args.openToSide,
query: args.query
Expand Down
50 changes: 20 additions & 30 deletions src/vs/workbench/services/preferences/browser/preferencesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,19 @@ export class PreferencesService extends Disposable implements IPreferencesServic
options = {
...options,
target: ConfigurationTarget.USER_LOCAL,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
};
return this.open(this.userSettingsResource, options);
}

if (!options.jsonEditor) {
return this.openSettings2(options);
}
private open(settingsResource: URI, options: IOpenSettingsOptions): Promise<IEditorPane | undefined> {
options = {
...options,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
};

return this.openSettingsJson(this.userSettingsResource, options);
return options.jsonEditor ?
this.openSettingsJson(settingsResource, options) :
this.openSettings2(options);
}

private async openSettings2(options: IOpenSettingsOptions): Promise<IEditorPane> {
Expand All @@ -233,12 +238,8 @@ export class PreferencesService extends Disposable implements IPreferencesServic
options = {
...options,
target: ConfigurationTarget.USER_LOCAL,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
};

return options.jsonEditor ?
this.openSettingsJson(this.userSettingsResource, options) :
this.openSettings2(options);
return this.open(this.userSettingsResource, options);
}

async openRemoteSettings(options: IOpenSettingsOptions = {}): Promise<IEditorPane | undefined> {
Expand All @@ -247,53 +248,42 @@ export class PreferencesService extends Disposable implements IPreferencesServic
options = {
...options,
target: ConfigurationTarget.USER_REMOTE,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
};

return options.jsonEditor ?
this.openSettingsJson(this.environmentService.settingsResource, options) :
this.openSettings2(options);
this.open(this.environmentService.settingsResource, options);
}
return undefined;
}

openWorkspaceSettings(options: IOpenSettingsOptions = {}): Promise<IEditorPane | undefined> {
options = {
...options,
target: ConfigurationTarget.WORKSPACE,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
};

if (!this.workspaceSettingsResource) {
this.notificationService.info(nls.localize('openFolderFirst', "Open a folder or workspace first to create workspace or folder settings."));
return Promise.reject(null);
}

return options.jsonEditor ?
this.openSettingsJson(this.workspaceSettingsResource, options) :
this.openSettings2(options);
options = {
...options,
target: ConfigurationTarget.WORKSPACE
};
return this.open(this.workspaceSettingsResource, options);
}

async openFolderSettings(options: IOpenSettingsOptions = {}): Promise<IEditorPane | undefined> {
options = {
...options,
target: ConfigurationTarget.WORKSPACE_FOLDER,
jsonEditor: options.jsonEditor ?? this.shouldOpenJsonByDefault()
target: ConfigurationTarget.WORKSPACE_FOLDER
};

if (!options.folderUri) {
throw new Error(`Missing folder URI`);
}

const folderSettingsUri = await this.getEditableSettingsURI(ConfigurationTarget.WORKSPACE_FOLDER, options.folderUri);
if (options.jsonEditor) {
if (folderSettingsUri) {
return this.openSettingsJson(folderSettingsUri, options);
}
if (!folderSettingsUri) {
throw new Error(`Invalid folder URI - ${options.folderUri.toString()}`);
}

return this.openSettings2(options);
return this.open(folderSettingsUri, options);
}

async openGlobalKeybindingSettings(textual: boolean, options?: IKeybindingsEditorOptions): Promise<void> {
Expand Down

0 comments on commit 0e05663

Please sign in to comment.