Skip to content

Commit 8dc1d20

Browse files
authored
Drop workspaceService.hasWorkspaceFolders (#18812)
* Drop `workspaceService.hasWorkspaceFolders` * Fix tests * || instead of ??
1 parent 391dc9e commit 8dc1d20

File tree

20 files changed

+74
-190
lines changed

20 files changed

+74
-190
lines changed

src/client/activation/activationManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
104104
return;
105105
}
106106
const key = this.getWorkspaceKey(doc.uri);
107+
const hasWorkspaceFolders = (this.workspaceService.workspaceFolders?.length || 0) > 0;
107108
// If we have opened a doc that does not belong to workspace, then do nothing.
108-
if (key === '' && this.workspaceService.hasWorkspaceFolders) {
109+
if (key === '' && hasWorkspaceFolders) {
109110
return;
110111
}
111112
if (this.activatedWorkspaces.has(key)) {
@@ -148,7 +149,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
148149
}
149150

150151
protected hasMultipleWorkspaces(): boolean {
151-
return this.workspaceService.hasWorkspaceFolders && this.workspaceService.workspaceFolders!.length > 1;
152+
return (this.workspaceService.workspaceFolders?.length || 0) > 1;
152153
}
153154

154155
protected getWorkspaceKey(resource: Resource): string {

src/client/activation/activationService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ export class LanguageServerExtensionActivationService
286286
}
287287

288288
private async onDidChangeConfiguration(event: ConfigurationChangeEvent): Promise<void> {
289-
const workspacesUris: (Uri | undefined)[] = this.workspaceService.hasWorkspaceFolders
290-
? this.workspaceService.workspaceFolders!.map((workspace) => workspace.uri)
291-
: [undefined];
289+
const workspacesUris: (Uri | undefined)[] = this.workspaceService.workspaceFolders?.map(
290+
(workspace) => workspace.uri,
291+
) ?? [undefined];
292292
if (
293293
workspacesUris.findIndex((uri) => event.affectsConfiguration(`python.${languageServerSetting}`, uri)) === -1
294294
) {

src/client/activation/common/activatorBase.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ export abstract class LanguageServerActivatorBase implements ILanguageServerActi
5050
@traceDecoratorError('Failed to activate language server')
5151
public async start(resource: Resource, interpreter?: PythonEnvironment): Promise<void> {
5252
if (!resource) {
53-
resource = this.workspace.hasWorkspaceFolders ? this.workspace.workspaceFolders![0].uri : undefined;
53+
resource =
54+
this.workspace.workspaceFolders && this.workspace.workspaceFolders.length > 0
55+
? this.workspace.workspaceFolders[0].uri
56+
: undefined;
5457
}
5558
this.resource = resource;
5659
await this.ensureLanguageServerIsAvailable(resource);

src/client/application/diagnostics/checks/invalidLaunchJsonDebugger.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export class InvalidLaunchJsonDebuggerService extends BaseDiagnosticsService {
7272
}
7373

7474
public async diagnose(resource: Resource): Promise<IDiagnostic[]> {
75-
if (!this.workspaceService.hasWorkspaceFolders) {
75+
const hasWorkspaceFolders = (this.workspaceService.workspaceFolders?.length || 0) > 0;
76+
if (!hasWorkspaceFolders) {
7677
return [];
7778
}
7879
const workspaceFolder = resource
@@ -86,12 +87,13 @@ export class InvalidLaunchJsonDebuggerService extends BaseDiagnosticsService {
8687
}
8788

8889
protected async fixLaunchJson(code: DiagnosticCodes): Promise<void> {
89-
if (!this.workspaceService.hasWorkspaceFolders) {
90+
const hasWorkspaceFolders = (this.workspaceService.workspaceFolders?.length || 0) > 0;
91+
if (!hasWorkspaceFolders) {
9092
return;
9193
}
9294

9395
await Promise.all(
94-
this.workspaceService.workspaceFolders!.map((workspaceFolder) =>
96+
(this.workspaceService.workspaceFolders ?? []).map((workspaceFolder) =>
9597
this.fixLaunchJsonInWorkspace(code, workspaceFolder),
9698
),
9799
);

src/client/common/application/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,12 +747,6 @@ export interface IWorkspaceService {
747747
* An event that is emitted when the [configuration](#WorkspaceConfiguration) changed.
748748
*/
749749
readonly onDidChangeConfiguration: Event<ConfigurationChangeEvent>;
750-
/**
751-
* Whether a workspace folder exists
752-
* @type {boolean}
753-
* @memberof IWorkspaceService
754-
*/
755-
readonly hasWorkspaceFolders: boolean;
756750
/**
757751
* Returns if we're running in a virtual workspace.
758752
*/

src/client/common/application/workspace.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ export class WorkspaceService implements IWorkspaceService {
3636
public get onDidChangeWorkspaceFolders(): Event<WorkspaceFoldersChangeEvent> {
3737
return workspace.onDidChangeWorkspaceFolders;
3838
}
39-
public get hasWorkspaceFolders() {
40-
return Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0;
41-
}
4239
public get workspaceFile() {
4340
return workspace.workspaceFile;
4441
}

src/client/debugger/extension/hooks/childProcessAttachService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ export class ChildProcessAttachService implements IChildProcessAttachService {
4242
config: AttachRequestArguments & DebugConfiguration,
4343
): WorkspaceFolder | undefined {
4444
const workspaceFolder = config.workspaceFolder;
45-
if (!this.workspaceService.hasWorkspaceFolders || !workspaceFolder) {
45+
const hasWorkspaceFolders = (this.workspaceService.workspaceFolders?.length || 0) > 0;
46+
if (!hasWorkspaceFolders || !workspaceFolder) {
4647
return;
4748
}
4849
return this.workspaceService.workspaceFolders!.find((ws) => ws.uri.fsPath === workspaceFolder);

src/client/interpreter/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export class InterpreterHelper implements IInterpreterHelper {
4040

4141
public getActiveWorkspaceUri(resource: Resource): WorkspacePythonPath | undefined {
4242
const workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
43-
if (!workspaceService.hasWorkspaceFolders) {
43+
const hasWorkspaceFolders = (workspaceService.workspaceFolders?.length || 0) > 0;
44+
if (!hasWorkspaceFolders) {
4445
return;
4546
}
4647
if (Array.isArray(workspaceService.workspaceFolders) && workspaceService.workspaceFolders.length === 1) {

src/client/startupTelemetry.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,15 @@ async function getActivationTelemetryProps(serviceContainer: IServiceContainer):
8282
// be able to partially populate as much as possible instead
8383
// (through granular try-catch statements).
8484
const workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
85-
const workspaceFolderCount = workspaceService.hasWorkspaceFolders ? workspaceService.workspaceFolders!.length : 0;
85+
const workspaceFolderCount = workspaceService.workspaceFolders?.length || 0;
8686
const terminalHelper = serviceContainer.get<ITerminalHelper>(ITerminalHelper);
8787
const terminalShellType = terminalHelper.identifyTerminalShell();
8888
if (!workspaceService.isTrusted) {
8989
return { workspaceFolderCount, terminal: terminalShellType };
9090
}
9191
const condaLocator = serviceContainer.get<ICondaService>(ICondaService);
9292
const interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);
93-
const mainWorkspaceUri = workspaceService.hasWorkspaceFolders
94-
? workspaceService.workspaceFolders![0].uri
95-
: undefined;
93+
const mainWorkspaceUri = workspaceService.workspaceFolders ? workspaceService.workspaceFolders[0].uri : undefined;
9694
const [condaVersion, hasPythonThree] = await Promise.all([
9795
condaLocator
9896
.getCondaVersion()

src/client/testing/common/configSettingService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export class TestConfigSettingsService implements ITestConfigSettingsService {
5555
private async updateSetting(testDirectory: string | Uri, setting: string, value: unknown) {
5656
let pythonConfig: WorkspaceConfiguration;
5757
const resource = typeof testDirectory === 'string' ? Uri.file(testDirectory) : testDirectory;
58-
if (!this.workspaceService.hasWorkspaceFolders) {
58+
const hasWorkspaceFolders = (this.workspaceService.workspaceFolders?.length || 0) > 0;
59+
if (!hasWorkspaceFolders) {
5960
pythonConfig = this.workspaceService.getConfiguration('python');
6061
} else if (this.workspaceService.workspaceFolders!.length === 1) {
6162
pythonConfig = this.workspaceService.getConfiguration(

0 commit comments

Comments
 (0)