Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debt - make toWorkspaceIdentifier return empty window workspace too #169612

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/snippet/browser/snippetVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Text, Variable, VariableResolver } from 'vs/editor/contrib/snippet/brow
import { OvertypingCapturer } from 'vs/editor/contrib/suggest/browser/suggestOvertypingCapturer';
import * as nls from 'vs/nls';
import { ILabelService } from 'vs/platform/label/common/label';
import { WORKSPACE_EXTENSION, isSingleFolderWorkspaceIdentifier, toWorkspaceIdentifier, IWorkspaceContextService, ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
import { WORKSPACE_EXTENSION, isSingleFolderWorkspaceIdentifier, toWorkspaceIdentifier, IWorkspaceContextService, ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier, isEmptyWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';

export const KnownSnippetVariableNames = Object.freeze<{ [key: string]: true }>({
'CURRENT_YEAR': true,
Expand Down Expand Up @@ -311,7 +311,7 @@ export class WorkspaceBasedVariableResolver implements VariableResolver {
}

const workspaceIdentifier = toWorkspaceIdentifier(this._workspaceService.getWorkspace());
if (!workspaceIdentifier) {
if (isEmptyWorkspaceIdentifier(workspaceIdentifier)) {
return undefined;
}

Expand Down
12 changes: 8 additions & 4 deletions src/vs/platform/workspace/common/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ export function isEmptyWorkspaceIdentifier(obj: unknown): obj is IEmptyWorkspace
}

export const EXTENSION_DEVELOPMENT_EMPTY_WINDOW_WORKSPACE: IEmptyWorkspaceIdentifier = { id: 'ext-dev' };
export const UNKNOWN_EMPTY_WINDOW_WORKSPACE: IEmptyWorkspaceIdentifier = { id: 'empty-window' };

export function toWorkspaceIdentifier(workspace: IWorkspace): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined;
export function toWorkspaceIdentifier(workspace: IWorkspace): IAnyWorkspaceIdentifier;
export function toWorkspaceIdentifier(backupPath: string | undefined, isExtensionDevelopment: boolean): IEmptyWorkspaceIdentifier;
export function toWorkspaceIdentifier(arg0: IWorkspace | string | undefined, isExtensionDevelopment?: boolean): IAnyWorkspaceIdentifier | undefined {
export function toWorkspaceIdentifier(arg0: IWorkspace | string | undefined, isExtensionDevelopment?: boolean): IAnyWorkspaceIdentifier {

// Empty workspace
if (typeof arg0 === 'string' || typeof arg0 === 'undefined') {
Expand All @@ -170,7 +171,7 @@ export function toWorkspaceIdentifier(arg0: IWorkspace | string | undefined, isE
return EXTENSION_DEVELOPMENT_EMPTY_WINDOW_WORKSPACE;
}

return undefined;
return UNKNOWN_EMPTY_WINDOW_WORKSPACE;
}

// Multi root
Expand All @@ -190,7 +191,10 @@ export function toWorkspaceIdentifier(arg0: IWorkspace | string | undefined, isE
};
}

return undefined;
// Empty window
return {
id: workspace.id
};
}

export function isWorkspaceIdentifier(obj: unknown): obj is IWorkspaceIdentifier {
Expand Down
7 changes: 4 additions & 3 deletions src/vs/platform/workspaces/test/common/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ISerializedSingleFolderWorkspaceIdentifier, ISerializedWorkspaceIdentifier, reviveIdentifier, hasWorkspaceFileExtension, isWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IEmptyWorkspaceIdentifier, toWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
import { ISerializedSingleFolderWorkspaceIdentifier, ISerializedWorkspaceIdentifier, reviveIdentifier, hasWorkspaceFileExtension, isWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IEmptyWorkspaceIdentifier, toWorkspaceIdentifier, isEmptyWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';

suite('Workspaces', () => {

Expand All @@ -31,8 +31,9 @@ suite('Workspaces', () => {

test('toWorkspaceIdentifier', () => {
let identifier = toWorkspaceIdentifier({ id: 'id', folders: [] });
assert.ok(!identifier);
assert.ok(!isSingleFolderWorkspaceIdentifier(identifier));
assert.ok(identifier);
assert.ok(isEmptyWorkspaceIdentifier(identifier));
assert.ok(!isWorkspaceIdentifier(identifier));
assert.ok(!isWorkspaceIdentifier(identifier));

identifier = toWorkspaceIdentifier({ id: 'id', folders: [{ index: 0, name: 'test', toResource: () => URI.file('test'), uri: URI.file('test') }] });
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/browser/web.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA
import { IWorkbenchFileService } from 'vs/workbench/services/files/common/files';
import { FileService } from 'vs/platform/files/common/fileService';
import { Schemas, connectionTokenCookieName } from 'vs/base/common/network';
import { IAnyWorkspaceIdentifier, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IAnyWorkspaceIdentifier, IWorkspaceContextService, UNKNOWN_EMPTY_WINDOW_WORKSPACE } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchConfigurationService } from 'vs/workbench/services/configuration/common/configuration';
import { onUnexpectedError } from 'vs/base/common/errors';
import { setFullscreen } from 'vs/base/browser/browser';
Expand Down Expand Up @@ -557,6 +557,6 @@ export class BrowserMain extends Disposable {
}

// Empty window workspace
return { id: 'empty-window' };
return UNKNOWN_EMPTY_WINDOW_WORKSPACE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { WORKSPACE_TRUST_BANNER, WORKSPACE_TRUST_EMPTY_WINDOW, WORKSPACE_TRUST_E
import { IEditorSerializer, IEditorFactoryRegistry, EditorExtensions } from 'vs/workbench/common/editor';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IWorkspaceContextService, IWorkspaceFoldersWillChangeEvent, toWorkspaceIdentifier, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { isEmptyWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IWorkspaceContextService, IWorkspaceFoldersWillChangeEvent, toWorkspaceIdentifier, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { dirname, resolve } from 'vs/base/common/path';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
Expand Down Expand Up @@ -299,8 +299,9 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
localize('folderTrust', "Do you trust the authors of the files in this folder?");

let checkboxText: string | undefined;
const workspaceIdentifier = toWorkspaceIdentifier(this.workspaceContextService.getWorkspace())!;
const workspaceIdentifier = toWorkspaceIdentifier(this.workspaceContextService.getWorkspace());
const isSingleFolderWorkspace = isSingleFolderWorkspaceIdentifier(workspaceIdentifier);
const isEmptyWindow = isEmptyWorkspaceIdentifier(workspaceIdentifier);
if (this.workspaceTrustManagementService.canSetParentFolderTrust()) {
const { name } = splitName(splitName((workspaceIdentifier as ISingleFolderWorkspaceIdentifier).uri.fsPath).parentPath);
checkboxText = localize('checkboxString', "Trust the authors of all files in the parent folder '{0}'", name);
Expand All @@ -316,7 +317,8 @@ export class WorkspaceTrustUXHandler extends Disposable implements IWorkbenchCon
localize('workspaceStartupTrustDetails', "{0} provides features that may automatically execute files in this workspace.", this.productService.nameShort) :
localize('folderStartupTrustDetails', "{0} provides features that may automatically execute files in this folder.", this.productService.nameShort),
localize('startupTrustRequestLearnMore', "If you don't trust the authors of these files, we recommend to continue in restricted mode as the files may be malicious. See [our docs](https://aka.ms/vscode-workspace-trust) to learn more."),
`\`${this.labelService.getWorkspaceLabel(workspaceIdentifier, { verbose: Verbosity.LONG })}\``,
!isEmptyWindow ?
`\`${this.labelService.getWorkspaceLabel(workspaceIdentifier, { verbose: Verbosity.LONG })}\`` : '',
],
checkboxText
);
Expand Down
7 changes: 1 addition & 6 deletions src/vs/workbench/electron-sandbox/desktop.main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,7 @@ export class DesktopMain extends Disposable {
}

// Otherwise, workspace is empty, so we derive an identifier
const emptyWorkspaceIdentifier = toWorkspaceIdentifier(this.configuration.backupPath, environmentService.isExtensionDevelopment);
if (!emptyWorkspaceIdentifier) {
throw new Error('Unable to resolve an empty workspace identifier from the environment');
}

return emptyWorkspaceIdentifier;
return toWorkspaceIdentifier(this.configuration.backupPath, environmentService.isExtensionDevelopment);
}

private async createWorkspaceService(
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/services/label/common/labelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export class LabelService extends Disposable implements ILabelService {
getWorkspaceLabel(workspace: IWorkspace | IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | URI, options?: { verbose: Verbosity }): string {
if (isWorkspace(workspace)) {
const identifier = toWorkspaceIdentifier(workspace);
if (identifier) {
if (isSingleFolderWorkspaceIdentifier(identifier) || isWorkspaceIdentifier(identifier)) {
return this.getWorkspaceLabel(identifier, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { DidChangeProfilesEvent, IUserDataProfile, IUserDataProfileOptions, IUserDataProfilesService, IUserDataProfileUpdateOptions } from 'vs/platform/userDataProfile/common/userDataProfile';
import { IAnyWorkspaceIdentifier, IWorkspaceContextService, WorkbenchState } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceContextService, toWorkspaceIdentifier } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IHostService } from 'vs/workbench/services/host/browser/host';
Expand Down Expand Up @@ -65,14 +65,14 @@ export class UserDataProfileManagementService extends Disposable implements IUse
}

async createAndEnterProfile(name: string, options?: IUserDataProfileOptions, fromExisting?: boolean): Promise<IUserDataProfile> {
const profile = await this.userDataProfilesService.createNamedProfile(name, options, this.getWorkspaceIdentifier());
const profile = await this.userDataProfilesService.createNamedProfile(name, options, toWorkspaceIdentifier(this.workspaceContextService.getWorkspace()));
await this.enterProfile(profile, !!fromExisting);
this.telemetryService.publicLog2<ProfileManagementActionExecutedEvent, ProfileManagementActionExecutedClassification>('profileManagementActionExecuted', { id: 'createAndEnterProfile' });
return profile;
}

async createAndEnterTransientProfile(): Promise<IUserDataProfile> {
const profile = await this.userDataProfilesService.createTransientProfile(this.getWorkspaceIdentifier());
const profile = await this.userDataProfilesService.createTransientProfile(toWorkspaceIdentifier(this.workspaceContextService.getWorkspace()));
await this.enterProfile(profile, false);
this.telemetryService.publicLog2<ProfileManagementActionExecutedEvent, ProfileManagementActionExecutedClassification>('profileManagementActionExecuted', { id: 'createAndEnterTransientProfile' });
return profile;
Expand Down Expand Up @@ -101,7 +101,7 @@ export class UserDataProfileManagementService extends Disposable implements IUse
}

async switchProfile(profile: IUserDataProfile): Promise<void> {
const workspaceIdentifier = this.getWorkspaceIdentifier();
const workspaceIdentifier = toWorkspaceIdentifier(this.workspaceContextService.getWorkspace());
if (!this.userDataProfilesService.profiles.some(p => p.id === profile.id)) {
throw new Error(`Profile ${profile.name} does not exist`);
}
Expand All @@ -113,17 +113,6 @@ export class UserDataProfileManagementService extends Disposable implements IUse
this.telemetryService.publicLog2<ProfileManagementActionExecutedEvent, ProfileManagementActionExecutedClassification>('profileManagementActionExecuted', { id: 'switchProfile' });
}

private getWorkspaceIdentifier(): IAnyWorkspaceIdentifier {
const workspace = this.workspaceContextService.getWorkspace();
switch (this.workspaceContextService.getWorkbenchState()) {
case WorkbenchState.FOLDER:
return { uri: workspace.folders[0].uri, id: workspace.id };
case WorkbenchState.WORKSPACE:
return { configPath: workspace.configuration!, id: workspace.id };
}
return { id: workspace.id };
}

private async enterProfile(profile: IUserDataProfile, preserveData: boolean, reloadMessage?: string): Promise<void> {
const isRemoteWindow = !!this.environmentService.remoteAuthority;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { IWorkspaceEditingService } from 'vs/workbench/services/workspaces/common/workspaceEditing';
import { URI } from 'vs/base/common/uri';
import { localize } from 'vs/nls';
import { hasWorkspaceFileExtension, isSavedWorkspace, isUntitledWorkspace, IWorkspaceContextService, IWorkspaceIdentifier, WorkbenchState, WORKSPACE_EXTENSION, WORKSPACE_FILTER } from 'vs/platform/workspace/common/workspace';
import { hasWorkspaceFileExtension, isSavedWorkspace, isUntitledWorkspace, isWorkspaceIdentifier, IWorkspaceContextService, IWorkspaceIdentifier, toWorkspaceIdentifier, WorkbenchState, WORKSPACE_EXTENSION, WORKSPACE_FILTER } from 'vs/platform/workspace/common/workspace';
import { IJSONEditingService, JSONEditingError, JSONEditingErrorCode } from 'vs/workbench/services/configuration/common/jsonEditing';
import { IWorkspaceFolderCreationData, IWorkspacesService, rewriteWorkspaceFileForNewLocation, IEnterWorkspaceResult, IStoredWorkspace } from 'vs/platform/workspaces/common/workspaces';
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService';
Expand Down Expand Up @@ -400,9 +400,9 @@ export abstract class AbstractWorkspaceEditingService implements IWorkspaceEditi
}

protected getCurrentWorkspaceIdentifier(): IWorkspaceIdentifier | undefined {
const workspace = this.contextService.getWorkspace();
if (workspace?.configuration) {
return { id: workspace.id, configPath: workspace.configuration };
const identifier = toWorkspaceIdentifier(this.contextService.getWorkspace());
if (isWorkspaceIdentifier(identifier)) {
return identifier;
}

return undefined;
Expand Down