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

Activation of multiple parts of the extension for multiple workspaces #4052

Merged
merged 11 commits into from
Jan 23, 2019
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/2 Fixes/3501.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Auto-select virtual environment in multi-root workspaces
1 change: 1 addition & 0 deletions news/2 Fixes/3502.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Validate interpreter in multi-root workspaces
111 changes: 111 additions & 0 deletions src/client/activation/activationManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { inject, injectable, multiInject } from 'inversify';
import { TextDocument, workspace } from 'vscode';
import { IApplicationDiagnostics } from '../application/types';
import { IDocumentManager, IWorkspaceService } from '../common/application/types';
import { isTestExecution } from '../common/constants';
import { traceDecorators } from '../common/logger';
import { IDisposable, Resource } from '../common/types';
import { IInterpreterAutoSelectionService } from '../interpreter/autoSelection/types';
import { IInterpreterService } from '../interpreter/contracts';
import { IExtensionActivationManager, IExtensionActivationService } from './types';

@injectable()
export class ExtensionActivationManager implements IExtensionActivationManager {
private readonly disposables: IDisposable[] = [];
private docOpenedHandler?: IDisposable;
private readonly activatedWorkspaces = new Set<string>();
constructor(
@multiInject(IExtensionActivationService) private readonly activationServices: IExtensionActivationService[],
@inject(IDocumentManager) private readonly documentManager: IDocumentManager,
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
@inject(IInterpreterAutoSelectionService) private readonly autoSelection: IInterpreterAutoSelectionService,
@inject(IApplicationDiagnostics) private readonly appDiagnostics: IApplicationDiagnostics,
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService
) { }

public dispose() {
while (this.disposables.length > 0) {
const disposable = this.disposables.shift();
disposable.dispose();
}
karrtikr marked this conversation as resolved.
Show resolved Hide resolved
if (this. docOpenedHandler){
this.docOpenedHandler.dispose();
this.docOpenedHandler = undefined;
}
}
public async activate(): Promise<void> {
await this.initialize();
await this.activateWorkspace(this.getActiveResource());
}
protected async initialize() {
// Get latest interpreter list.
const mainWorkspaceUri = this.getActiveResource();
this.interpreterService.getInterpreters(mainWorkspaceUri).ignoreErrors();
this.addHandlers();
}
protected addHandlers() {
this.disposables.push(this.workspaceService.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged, this));
}
protected addRemoveDocOpenedHandlers() {
if (this.hasMultipleWorkspaces()) {
if (!this.docOpenedHandler) {
this.docOpenedHandler = this.documentManager.onDidOpenTextDocument(this.onDocOpened, this);
}
return;
}
if (this.docOpenedHandler) {
this.docOpenedHandler.dispose();
this.docOpenedHandler = undefined;
}
}
protected onWorkspaceFoldersChanged() {
this.addRemoveDocOpenedHandlers();
}
protected hasMultipleWorkspaces() {
return this.workspaceService.hasWorkspaceFolders && this.workspaceService.workspaceFolders.length > 1;
}
protected onDocOpened(doc: TextDocument) {
const key = this.getWorkspaceKey(doc.uri);
if (this.activatedWorkspaces.has(key)) {
return;
}
const folder = this.workspaceService.getWorkspaceFolder(doc.uri);
this.activateWorkspace(folder ? folder.uri : undefined).ignoreErrors();
}
@traceDecorators.error('Failed to activate a worksapce')
protected async activateWorkspace(resource: Resource) {
const key = this.getWorkspaceKey(resource);
this.activatedWorkspaces.add(key);

await Promise.all(this.activationServices.map(item => item.activate(resource)));

// When testing, do not perform health checks, as modal dialogs can be displayed.
if (!isTestExecution()) {
await this.appDiagnostics.performPreStartupHealthCheck(resource);
}
await this.autoSelection.autoSelectInterpreter(resource);
}
protected getWorkspaceKey(resource: Resource) {
if (!resource) {
return '';
}
const workspaceFolder = this.workspaceService.getWorkspaceFolder(resource);
if (!workspaceFolder) {
return '';
}
return workspaceFolder.uri.fsPath;
}
private getActiveResource(): Resource {
if (this.documentManager.activeTextEditor && !this.documentManager.activeTextEditor.document.isUntitled) {
return this.documentManager.activeTextEditor.document.uri;
}
return Array.isArray(this.workspaceService.workspaceFolders) && workspace.workspaceFolders.length > 0
? workspace.workspaceFolders[0].uri
: undefined;
}
}
62 changes: 32 additions & 30 deletions src/client/activation/activationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,25 @@
'use strict';

import { inject, injectable } from 'inversify';
import {
ConfigurationChangeEvent,
Disposable, OutputChannel, Uri
} from 'vscode';
import { ConfigurationChangeEvent, Disposable, OutputChannel, Uri } from 'vscode';
import { LSNotSupportedDiagnosticServiceId } from '../application/diagnostics/checks/lsNotSupported';
import { IDiagnosticsService } from '../application/diagnostics/types';
import {
IApplicationShell, ICommandManager,
IWorkspaceService
} from '../common/application/types';
import { IApplicationShell, ICommandManager, IWorkspaceService } from '../common/application/types';
import { STANDARD_OUTPUT_CHANNEL } from '../common/constants';
import '../common/extensions';
import {
IConfigurationService, IDisposableRegistry,
IOutputChannel, IPythonSettings
} from '../common/types';
import { IConfigurationService, IDisposableRegistry, IOutputChannel, IPythonSettings, Resource } from '../common/types';
import { IServiceContainer } from '../ioc/types';
import { sendTelemetryEvent } from '../telemetry';
import { EventName } from '../telemetry/constants';
import {
ExtensionActivators, IExtensionActivationService,
IExtensionActivator
} from './types';
import { IExtensionActivationService, ILanguageServerActivator, LanguageServerActivator } from './types';

const jediEnabledSetting: keyof IPythonSettings = 'jediEnabled';
type ActivatorInfo = { jedi: boolean; activator: IExtensionActivator };
type ActivatorInfo = { jedi: boolean; activator: ILanguageServerActivator };

@injectable()
export class ExtensionActivationService implements IExtensionActivationService, Disposable {
export class LanguageServerExtensionActivationService implements IExtensionActivationService, Disposable {
private currentActivator?: ActivatorInfo;
private activatedOnce: boolean;
private readonly workspaceService: IWorkspaceService;
private readonly output: OutputChannel;
private readonly appShell: IApplicationShell;
Expand All @@ -43,31 +32,35 @@ export class ExtensionActivationService implements IExtensionActivationService,
this.workspaceService = this.serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.output = this.serviceContainer.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
this.appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
this.lsNotSupportedDiagnosticService = this.serviceContainer.get<IDiagnosticsService>(IDiagnosticsService, LSNotSupportedDiagnosticServiceId);
this.lsNotSupportedDiagnosticService = this.serviceContainer.get<IDiagnosticsService>(
IDiagnosticsService,
LSNotSupportedDiagnosticServiceId
);
const disposables = serviceContainer.get<IDisposableRegistry>(IDisposableRegistry);
disposables.push(this);
disposables.push(this.workspaceService.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this)));
}

public async activate(): Promise<void> {
if (this.currentActivator) {
public async activate(_resource: Resource): Promise<void> {
if (this.currentActivator || this.activatedOnce) {
return;
}
this.activatedOnce = true;

let jedi = this.useJedi();
if (!jedi) {
const diagnostic = await this.lsNotSupportedDiagnosticService.diagnose(undefined);
this.lsNotSupportedDiagnosticService.handle(diagnostic).ignoreErrors();
if (diagnostic.length){
if (diagnostic.length) {
sendTelemetryEvent(EventName.PYTHON_LANGUAGE_SERVER_PLATFORM_NOT_SUPPORTED);
jedi = true;
}
}

await this.logStartup(jedi);

let activatorName = jedi ? ExtensionActivators.Jedi : ExtensionActivators.DotNet;
let activator = this.serviceContainer.get<IExtensionActivator>(IExtensionActivator, activatorName);
let activatorName = jedi ? LanguageServerActivator.Jedi : LanguageServerActivator.DotNet;
let activator = this.serviceContainer.get<ILanguageServerActivator>(ILanguageServerActivator, activatorName);
this.currentActivator = { jedi, activator };

try {
Expand All @@ -80,8 +73,8 @@ export class ExtensionActivationService implements IExtensionActivationService,
//Language server fails, reverting to jedi
jedi = true;
await this.logStartup(jedi);
activatorName = ExtensionActivators.Jedi;
activator = this.serviceContainer.get<IExtensionActivator>(IExtensionActivator, activatorName);
activatorName = LanguageServerActivator.Jedi;
activator = this.serviceContainer.get<ILanguageServerActivator>(ILanguageServerActivator, activatorName);
this.currentActivator = { jedi, activator };
await activator.activate();
}
Expand All @@ -94,12 +87,16 @@ export class ExtensionActivationService implements IExtensionActivationService,
}

private async logStartup(isJedi: boolean): Promise<void> {
const outputLine = isJedi ? 'Starting Jedi Python language engine.' : 'Starting Microsoft Python language server.';
const outputLine = isJedi
? 'Starting Jedi Python language engine.'
: 'Starting Microsoft Python language server.';
this.output.appendLine(outputLine);
}

private async onDidChangeConfiguration(event: ConfigurationChangeEvent) {
const workspacesUris: (Uri | undefined)[] = this.workspaceService.hasWorkspaceFolders ? this.workspaceService.workspaceFolders!.map(workspace => workspace.uri) : [undefined];
const workspacesUris: (Uri | undefined)[] = this.workspaceService.hasWorkspaceFolders
? this.workspaceService.workspaceFolders!.map(workspace => workspace.uri)
: [undefined];
if (workspacesUris.findIndex(uri => event.affectsConfiguration(`python.${jediEnabledSetting}`, uri)) === -1) {
return;
}
Expand All @@ -108,13 +105,18 @@ export class ExtensionActivationService implements IExtensionActivationService,
return;
}

const item = await this.appShell.showInformationMessage('Please reload the window switching between language engines.', 'Reload');
const item = await this.appShell.showInformationMessage(
'Please reload the window switching between language engines.',
'Reload'
);
if (item === 'Reload') {
this.serviceContainer.get<ICommandManager>(ICommandManager).executeCommand('workbench.action.reloadWindow');
}
}
private useJedi(): boolean {
const workspacesUris: (Uri | undefined)[] = this.workspaceService.hasWorkspaceFolders ? this.workspaceService.workspaceFolders!.map(item => item.uri) : [undefined];
const workspacesUris: (Uri | undefined)[] = this.workspaceService.hasWorkspaceFolders
? this.workspaceService.workspaceFolders!.map(item => item.uri)
: [undefined];
const configuraionService = this.serviceContainer.get<IConfigurationService>(IConfigurationService);
return workspacesUris.filter(uri => configuraionService.getSettings(uri).jediEnabled).length > 0;
}
Expand Down
58 changes: 46 additions & 12 deletions src/client/activation/jedi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import { OnTypeFormattingDispatcher } from '../typeFormatters/dispatcher';
import { OnEnterFormatter } from '../typeFormatters/onEnterFormatter';
import { IUnitTestManagementService } from '../unittests/types';
import { WorkspaceSymbols } from '../workspaceSymbols/main';
import { IExtensionActivator } from './types';
import { ILanguageServerActivator } from './types';

@injectable()
export class JediExtensionActivator implements IExtensionActivator {
export class JediExtensionActivator implements ILanguageServerActivator {
private readonly context: IExtensionContext;
private jediFactory?: JediFactory;
private readonly documentSelector: DocumentFilter[];
Expand All @@ -36,27 +36,51 @@ export class JediExtensionActivator implements IExtensionActivator {
public async activate(): Promise<void> {
const context = this.context;

const jediFactory = this.jediFactory = new JediFactory(context.asAbsolutePath('.'), this.serviceManager);
const jediFactory = (this.jediFactory = new JediFactory(context.asAbsolutePath('.'), this.serviceManager));
context.subscriptions.push(jediFactory);
context.subscriptions.push(...activateGoToObjectDefinitionProvider(jediFactory));

context.subscriptions.push(jediFactory);
context.subscriptions.push(languages.registerRenameProvider(this.documentSelector, new PythonRenameProvider(this.serviceManager)));
context.subscriptions.push(
languages.registerRenameProvider(this.documentSelector, new PythonRenameProvider(this.serviceManager))
);
const definitionProvider = new PythonDefinitionProvider(jediFactory);

context.subscriptions.push(languages.registerDefinitionProvider(this.documentSelector, definitionProvider));
context.subscriptions.push(languages.registerHoverProvider(this.documentSelector, new PythonHoverProvider(jediFactory)));
context.subscriptions.push(languages.registerReferenceProvider(this.documentSelector, new PythonReferenceProvider(jediFactory)));
context.subscriptions.push(languages.registerCompletionItemProvider(this.documentSelector, new PythonCompletionItemProvider(jediFactory, this.serviceManager), '.'));
context.subscriptions.push(languages.registerCodeLensProvider(this.documentSelector, this.serviceManager.get<IShebangCodeLensProvider>(IShebangCodeLensProvider)));
context.subscriptions.push(
languages.registerHoverProvider(this.documentSelector, new PythonHoverProvider(jediFactory))
);
context.subscriptions.push(
languages.registerReferenceProvider(this.documentSelector, new PythonReferenceProvider(jediFactory))
);
context.subscriptions.push(
languages.registerCompletionItemProvider(
this.documentSelector,
new PythonCompletionItemProvider(jediFactory, this.serviceManager),
'.'
)
);
context.subscriptions.push(
languages.registerCodeLensProvider(
this.documentSelector,
this.serviceManager.get<IShebangCodeLensProvider>(IShebangCodeLensProvider)
)
);

const onTypeDispatcher = new OnTypeFormattingDispatcher({
'\n': new OnEnterFormatter(),
':': new BlockFormatProviders()
});
const onTypeTriggers = onTypeDispatcher.getTriggerCharacters();
if (onTypeTriggers) {
context.subscriptions.push(languages.registerOnTypeFormattingEditProvider(PYTHON, onTypeDispatcher, onTypeTriggers.first, ...onTypeTriggers.more));
context.subscriptions.push(
languages.registerOnTypeFormattingEditProvider(
PYTHON,
onTypeDispatcher,
onTypeTriggers.first,
...onTypeTriggers.more
)
);
}

const serviceContainer = this.serviceManager.get<IServiceContainer>(IServiceContainer);
Expand All @@ -67,13 +91,23 @@ export class JediExtensionActivator implements IExtensionActivator {

const pythonSettings = this.serviceManager.get<IConfigurationService>(IConfigurationService).getSettings();
if (pythonSettings.devOptions.indexOf('DISABLE_SIGNATURE') === -1) {
context.subscriptions.push(languages.registerSignatureHelpProvider(this.documentSelector, new PythonSignatureProvider(jediFactory), '(', ','));
context.subscriptions.push(
languages.registerSignatureHelpProvider(
this.documentSelector,
new PythonSignatureProvider(jediFactory),
'(',
','
)
);
}

context.subscriptions.push(languages.registerRenameProvider(PYTHON, new PythonRenameProvider(serviceContainer)));
context.subscriptions.push(
languages.registerRenameProvider(PYTHON, new PythonRenameProvider(serviceContainer))
);

const testManagementService = this.serviceManager.get<IUnitTestManagementService>(IUnitTestManagementService);
testManagementService.activate()
testManagementService
.activate()
.then(() => testManagementService.activateCodeLenses(symbolProvider))
.catch(ex => this.serviceManager.get<ILogger>(ILogger).logError('Failed to activate Unit Tests', ex));
}
Expand Down
Loading