Skip to content

Add experiments to move interpreter display status bar item to the right #18152

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

Merged
merged 8 commits into from
Jan 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/1 Enhancements/18282.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move pinned interpreter status bar item towards the right behind `pythonInterpreterInfoPinned` experiment.
1 change: 1 addition & 0 deletions news/1 Enhancements/18283.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move interpreter status bar item into the `Python` language status item behind `pythonInterpreterInfoUnpinned` experiment.
6 changes: 6 additions & 0 deletions src/client/common/application/applicationShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
CancellationToken,
CancellationTokenSource,
Disposable,
DocumentSelector,
env,
Event,
InputBox,
InputBoxOptions,
languages,
LanguageStatusItem,
MessageItem,
MessageOptions,
OpenDialogOptions,
Expand Down Expand Up @@ -149,4 +152,7 @@ export class ApplicationShell implements IApplicationShell {
public createOutputChannel(name: string): OutputChannel {
return window.createOutputChannel(name);
}
public createLanguageStatusItem(id: string, selector: DocumentSelector): LanguageStatusItem {
return languages.createLanguageStatusItem(id, selector);
}
}
2 changes: 2 additions & 0 deletions src/client/common/application/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
GlobPattern,
InputBox,
InputBoxOptions,
LanguageStatusItem,
MessageItem,
MessageOptions,
OpenDialogOptions,
Expand Down Expand Up @@ -416,6 +417,7 @@ export interface IApplicationShell {
* @param name Human-readable string which will be used to represent the channel in the UI.
*/
createOutputChannel(name: string): OutputChannel;
createLanguageStatusItem(id: string, selector: DocumentSelector): LanguageStatusItem;
}

export const ICommandManager = Symbol('ICommandManager');
Expand Down
4 changes: 4 additions & 0 deletions src/client/common/experiments/groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
export enum ShowExtensionSurveyPrompt {
experiment = 'pythonSurveyNotification',
}
export enum InterpreterStatusBarPosition {
Pinned = 'pythonInterpreterInfoPinned',
Unpinned = 'pythonInterpreterInfoUnpinned',
}
121 changes: 91 additions & 30 deletions src/client/interpreter/display/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { inject, injectable } from 'inversify';
import { Disposable, StatusBarAlignment, StatusBarItem, Uri } from 'vscode';
import { Disposable, LanguageStatusItem, LanguageStatusSeverity, StatusBarAlignment, StatusBarItem, Uri } from 'vscode';
import { IExtensionSingleActivationService } from '../../activation/types';
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
import { Commands, PYTHON_LANGUAGE } from '../../common/constants';
import { InterpreterStatusBarPosition } from '../../common/experiments/groups';
import '../../common/extensions';
import { IDisposableRegistry, IPathUtils, Resource } from '../../common/types';
import { Interpreters } from '../../common/utils/localize';
import { IDisposableRegistry, IExperimentService, IPathUtils, Resource } from '../../common/types';
import { InterpreterQuickPickList, Interpreters } from '../../common/utils/localize';
import { IServiceContainer } from '../../ioc/types';
import { traceLog } from '../../logging';
import { PythonEnvironment } from '../../pythonEnvironments/info';
Expand All @@ -14,9 +17,19 @@ import {
IInterpreterStatusbarVisibilityFilter,
} from '../contracts';

/**
* Based on https://github.com/microsoft/vscode-python/issues/18040#issuecomment-992567670.
* This is to ensure the item appears right after the Python language status item.
*/
const STATUS_BAR_ITEM_PRIORITY = 100.09999;
@injectable()
export class InterpreterDisplay implements IInterpreterDisplay {
private readonly statusBar: StatusBarItem;
export class InterpreterDisplay implements IInterpreterDisplay, IExtensionSingleActivationService {
public supportedWorkspaceTypes: { untrustedWorkspace: boolean; virtualWorkspace: boolean } = {
untrustedWorkspace: false,
virtualWorkspace: true,
};
private statusBar: StatusBarItem | undefined;
private languageStatus: LanguageStatusItem | undefined;
private readonly helper: IInterpreterHelper;
private readonly workspaceService: IWorkspaceService;
private readonly pathUtils: IPathUtils;
Expand All @@ -26,26 +39,48 @@ export class InterpreterDisplay implements IInterpreterDisplay {
private interpreterPath: string | undefined;
private statusBarCanBeDisplayed?: boolean;
private visibilityFilters: IInterpreterStatusbarVisibilityFilter[] = [];
private disposableRegistry: Disposable[];
private experiments: IExperimentService;

constructor(@inject(IServiceContainer) private readonly serviceContainer: IServiceContainer) {
this.helper = serviceContainer.get<IInterpreterHelper>(IInterpreterHelper);
this.workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.pathUtils = serviceContainer.get<IPathUtils>(IPathUtils);
this.interpreterService = serviceContainer.get<IInterpreterService>(IInterpreterService);

const application = serviceContainer.get<IApplicationShell>(IApplicationShell);
const disposableRegistry = serviceContainer.get<Disposable[]>(IDisposableRegistry);

this.statusBar = application.createStatusBarItem(StatusBarAlignment.Left, 100);
this.statusBar.command = 'python.setInterpreter';
disposableRegistry.push(this.statusBar);
this.disposableRegistry = serviceContainer.get<Disposable[]>(IDisposableRegistry);

this.interpreterService.onDidChangeInterpreterInformation(
this.onDidChangeInterpreterInformation,
this,
disposableRegistry,
this.disposableRegistry,
);
this.experiments = this.serviceContainer.get<IExperimentService>(IExperimentService);
}

public async activate(): Promise<void> {
const application = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
if (this.experiments.inExperimentSync(InterpreterStatusBarPosition.Unpinned)) {
this.languageStatus = application.createLanguageStatusItem('python.selectedInterpreter', {
language: PYTHON_LANGUAGE,
});
this.languageStatus.severity = LanguageStatusSeverity.Information;
this.languageStatus.command = {
title: InterpreterQuickPickList.browsePath.openButtonLabel(),
command: Commands.Set_Interpreter,
};
this.disposableRegistry.push(this.languageStatus);
} else {
let [alignment, priority] = [StatusBarAlignment.Left, 100];
if (this.experiments.inExperimentSync(InterpreterStatusBarPosition.Pinned)) {
[alignment, priority] = [StatusBarAlignment.Right, STATUS_BAR_ITEM_PRIORITY];
}
this.statusBar = application.createStatusBarItem(alignment, priority);
this.statusBar.command = Commands.Set_Interpreter;
this.disposableRegistry.push(this.statusBar);
}
}

public async refresh(resource?: Uri) {
// Use the workspace Uri if available
if (resource && this.workspaceService.getWorkspaceFolder(resource)) {
Expand All @@ -72,30 +107,56 @@ export class InterpreterDisplay implements IInterpreterDisplay {
private async updateDisplay(workspaceFolder?: Uri) {
const interpreter = await this.interpreterService.getActiveInterpreter(workspaceFolder);
this.currentlySelectedWorkspaceFolder = workspaceFolder;
if (interpreter) {
this.statusBar.color = '';
this.statusBar.tooltip = this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath);
if (this.interpreterPath !== interpreter.path) {
traceLog(
Interpreters.pythonInterpreterPath().format(
this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath),
),
);
this.interpreterPath = interpreter.path;
if (this.statusBar) {
if (interpreter) {
this.statusBar.color = '';
this.statusBar.tooltip = this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath);
if (this.interpreterPath !== interpreter.path) {
traceLog(
Interpreters.pythonInterpreterPath().format(
this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath),
),
);
this.interpreterPath = interpreter.path;
}
let text = interpreter.displayName!;
if (this.experiments.inExperimentSync(InterpreterStatusBarPosition.Pinned)) {
text = text.startsWith('Python') ? text.substring('Python'.length).trim() : text;
}
this.statusBar.text = text;
this.currentlySelectedInterpreterPath = interpreter.path;
} else {
this.statusBar.tooltip = '';
this.statusBar.color = '';
this.statusBar.text = '$(alert) Select Python Interpreter';
this.currentlySelectedInterpreterPath = undefined;
}
} else if (this.languageStatus) {
if (interpreter) {
this.languageStatus.detail = this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath);
if (this.interpreterPath !== interpreter.path) {
traceLog(
Interpreters.pythonInterpreterPath().format(
this.pathUtils.getDisplayName(interpreter.path, workspaceFolder?.fsPath),
),
);
this.interpreterPath = interpreter.path;
}
let text = interpreter.displayName!;
text = text.startsWith('Python') ? text.substring('Python'.length).trim() : text;
this.languageStatus.text = text;
this.currentlySelectedInterpreterPath = interpreter.path;
} else {
this.languageStatus.text = '$(alert) No Interpreter Selected';
this.languageStatus.detail = undefined;
this.currentlySelectedInterpreterPath = undefined;
}
this.statusBar.text = interpreter.displayName!;
this.currentlySelectedInterpreterPath = interpreter.path;
} else {
this.statusBar.tooltip = '';
this.statusBar.color = '';
this.statusBar.text = '$(alert) Select Python Interpreter';
this.currentlySelectedInterpreterPath = undefined;
}
this.statusBarCanBeDisplayed = true;
this.updateVisibility();
}
private updateVisibility() {
if (!this.statusBarCanBeDisplayed) {
if (!this.statusBar || !this.statusBarCanBeDisplayed) {
return;
}
if (this.visibilityFilters.length === 0 || this.visibilityFilters.every((filter) => !filter.hidden)) {
Expand Down
40 changes: 36 additions & 4 deletions src/client/interpreter/interpreterService.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
// eslint-disable-next-line max-classes-per-file
import { inject, injectable } from 'inversify';
import { Disposable, Event, EventEmitter, Uri } from 'vscode';
import '../common/extensions';
import { IDocumentManager } from '../common/application/types';
import { IPythonExecutionFactory } from '../common/process/types';
import { IConfigurationService, IDisposableRegistry, IInterpreterPathService } from '../common/types';
import {
IConfigurationService,
IDisposableRegistry,
IExperimentService,
IInterpreterPathService,
} from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { PythonEnvironment } from '../pythonEnvironments/info';
import {
IComponentAdapter,
IInterpreterDisplay,
IInterpreterService,
IInterpreterStatusbarVisibilityFilter,
PythonEnvironmentsChangedEvent,
} from './contracts';
import { PythonLocatorQuery } from '../pythonEnvironments/base/locator';
import { traceError } from '../logging';
import { PYTHON_LANGUAGE } from '../common/constants';
import { InterpreterStatusBarPosition } from '../common/experiments/groups';

type StoredPythonEnvironment = PythonEnvironment & { store?: boolean };

Expand Down Expand Up @@ -80,6 +89,22 @@ export class InterpreterService implements Disposable, IInterpreterService {
public initialize(): void {
const disposables = this.serviceContainer.get<Disposable[]>(IDisposableRegistry);
const documentManager = this.serviceContainer.get<IDocumentManager>(IDocumentManager);
const interpreterDisplay = this.serviceContainer.get<IInterpreterDisplay>(IInterpreterDisplay);
const filter = new (class implements IInterpreterStatusbarVisibilityFilter {
constructor(private readonly docManager: IDocumentManager) {}

public readonly interpreterVisibilityEmitter = new EventEmitter<void>();

public readonly changed = this.interpreterVisibilityEmitter.event;

get hidden() {
return this.docManager.activeTextEditor?.document.languageId !== PYTHON_LANGUAGE;
}
})(documentManager);
const experiments = this.serviceContainer.get<IExperimentService>(IExperimentService);
if (experiments.inExperimentSync(InterpreterStatusBarPosition.Pinned)) {
interpreterDisplay.registerVisibilityFilter(filter);
}
disposables.push(
this.onDidChangeInterpreters((e) => {
const interpreter = e.old ?? e.new;
Expand All @@ -89,9 +114,16 @@ export class InterpreterService implements Disposable, IInterpreterService {
}),
);
disposables.push(
documentManager.onDidChangeActiveTextEditor((e) =>
e && e.document ? this.refresh(e.document.uri) : undefined,
),
documentManager.onDidOpenTextDocument(() => {
// To handle scenario when language mode is set to "python"
filter.interpreterVisibilityEmitter.fire();
}),
documentManager.onDidChangeActiveTextEditor((e) => {
filter.interpreterVisibilityEmitter.fire();
if (e && e.document) {
this.refresh(e.document.uri);
}
}),
);
const pySettings = this.configService.getSettings();
this._pythonPathSetting = pySettings.pythonPath;
Expand Down
1 change: 1 addition & 0 deletions src/client/interpreter/serviceRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function registerInterpreterTypes(serviceManager: IServiceManager): void

serviceManager.addSingleton<IInterpreterService>(IInterpreterService, InterpreterService);
serviceManager.addSingleton<IInterpreterDisplay>(IInterpreterDisplay, InterpreterDisplay);
serviceManager.addBinding(IInterpreterDisplay, IExtensionSingleActivationService);

serviceManager.addSingleton<IPythonPathUpdaterServiceFactory>(
IPythonPathUpdaterServiceFactory,
Expand Down
Loading