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

Add create environment button to requirements.txt and pyproject.toml files #20813

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"publisher": "ms-python",
"enabledApiProposals": [
"contribEditorContentMenu",
"quickPickSortByLabel",
"envShellEvent",
"testObserver"
Expand All @@ -41,7 +42,7 @@
"theme": "dark"
},
"engines": {
"vscode": "^1.76.0"
"vscode": "^1.77.0-20230309"
},
"keywords": [
"python",
Expand Down Expand Up @@ -1688,6 +1689,18 @@
"when": "!virtualWorkspace && shellExecutionSupported"
}
],
"editor/content": [
{
"group": "Python",
"command": "python.createEnvironment",
"when": "resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported"
},
{
"group": "Python",
"command": "python.createEnvironment",
"when": "resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported"
}
],
"editor/context": [
{
"command": "python.execInTerminal",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"python.command.python.sortImports.title": "Sort Imports",
"python.command.python.startREPL.title": "Start REPL",
"python.command.python.createEnvironment.title": "Create Environment",
"python.command.python.createEnvironment.title": "Create Environment...",
"python.command.python.createNewFile.title": "New Python File",
"python.command.python.createTerminal.title": "Create Terminal",
"python.command.python.execInTerminal.title": "Run Python File in Terminal",
Expand Down
15 changes: 15 additions & 0 deletions src/client/common/vscodeApis/workspaceApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import {
CancellationToken,
ConfigurationScope,
Disposable,
GlobPattern,
TextDocument,
TextDocumentChangeEvent,
Uri,
workspace,
WorkspaceConfiguration,
Expand Down Expand Up @@ -41,3 +44,15 @@ export function findFiles(
): Thenable<Uri[]> {
return workspace.findFiles(include, exclude, maxResults, token);
}

export function getOpenTextDocuments(): readonly TextDocument[] {
return workspace.textDocuments;
}

export function onDidOpenTextDocument(handler: (doc: TextDocument) => void): Disposable {
return workspace.onDidOpenTextDocument(handler);
}

export function onDidChangeTextDocument(handler: (e: TextDocumentChangeEvent) => void): Disposable {
return workspace.onDidChangeTextDocument(handler);
}
2 changes: 2 additions & 0 deletions src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { WorkspaceService } from './common/application/workspace';
import { DynamicPythonDebugConfigurationService } from './debugger/extension/configuration/dynamicdebugConfigurationService';
import { registerCreateEnvironmentFeatures } from './pythonEnvironments/creation/createEnvApi';
import { IInterpreterQuickPick } from './interpreter/configuration/types';
import { registerPyProjectTomlCreateEnvFeatures } from './pythonEnvironments/creation/pyprojectTomlCreateEnv';

export async function activateComponents(
// `ext` is passed to any extra activation funcs.
Expand Down Expand Up @@ -105,6 +106,7 @@ export function activateFeatures(ext: ExtensionState, _components: Components):
);
const pathUtils = ext.legacyIOC.serviceContainer.get<IPathUtils>(IPathUtils);
registerCreateEnvironmentFeatures(ext.disposables, interpreterQuickPick, interpreterPathService, pathUtils);
registerPyProjectTomlCreateEnvFeatures(ext.disposables);
}

/// //////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions src/client/pythonEnvironments/creation/provider/venvUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ async function pickRequirementsFiles(files: string[], token?: CancellationToken)
return undefined;
}

export function isPipInstallableToml(tomlContent: string): boolean {
const toml = tomlParse(tomlContent);
return tomlHasBuildSystem(toml);
}

export interface IPackageInstallSelection {
installType: 'toml' | 'requirements' | 'none';
installItem?: string;
Expand Down
41 changes: 41 additions & 0 deletions src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { TextDocument, TextDocumentChangeEvent } from 'vscode';
import { IDisposableRegistry } from '../../common/types';
import { executeCommand } from '../../common/vscodeApis/commandApis';
import {
onDidOpenTextDocument,
onDidChangeTextDocument,
getOpenTextDocuments,
} from '../../common/vscodeApis/workspaceApis';
import { isPipInstallableToml } from './provider/venvUtils';

async function setPyProjectTomlContextKey(doc: TextDocument): Promise<void> {
if (isPipInstallableToml(doc.getText())) {
await executeCommand('setContext', 'pipInstallableToml', true);
} else {
await executeCommand('setContext', 'pipInstallableToml', false);
}
}

export function registerPyProjectTomlCreateEnvFeatures(disposables: IDisposableRegistry): void {
disposables.push(
onDidOpenTextDocument(async (doc: TextDocument) => {
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
}),
onDidChangeTextDocument(async (e: TextDocumentChangeEvent) => {
if (e.document.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(e.document);
}
}),
);

getOpenTextDocuments().forEach(async (doc: TextDocument) => {
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/no-explicit-any */

import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import * as typemoq from 'typemoq';
import { assert, use as chaiUse } from 'chai';
import { TextDocument, TextDocumentChangeEvent } from 'vscode';
import * as cmdApis from '../../../client/common/vscodeApis/commandApis';
import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis';
import { IDisposableRegistry } from '../../../client/common/types';
import { registerPyProjectTomlCreateEnvFeatures } from '../../../client/pythonEnvironments/creation/pyprojectTomlCreateEnv';

chaiUse(chaiAsPromised);

class FakeDisposable {
public dispose() {
// Do nothing
}
}

function getInstallableToml(): typemoq.IMock<TextDocument> {
const pyprojectTomlPath = 'pyproject.toml';
const pyprojectToml = typemoq.Mock.ofType<TextDocument>();
pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath);
pyprojectToml
.setup((p) => p.getText(typemoq.It.isAny()))
.returns(
() =>
'[project]\nname = "spam"\nversion = "2020.0.0"\n[build-system]\nrequires = ["setuptools ~= 58.0", "cython ~= 0.29.0"]\n[project.optional-dependencies]\ntest = ["pytest"]\ndoc = ["sphinx", "furo"]',
);
return pyprojectToml;
}

function getNonInstallableToml(): typemoq.IMock<TextDocument> {
const pyprojectTomlPath = 'pyproject.toml';
const pyprojectToml = typemoq.Mock.ofType<TextDocument>();
pyprojectToml.setup((p) => p.fileName).returns(() => pyprojectTomlPath);
pyprojectToml
.setup((p) => p.getText(typemoq.It.isAny()))
.returns(() => '[project]\nname = "spam"\nversion = "2020.0.0"\n');
return pyprojectToml;
}

function getSomeFile(): typemoq.IMock<TextDocument> {
const someFilePath = 'something.py';
const someFile = typemoq.Mock.ofType<TextDocument>();
someFile.setup((p) => p.fileName).returns(() => someFilePath);
someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'print("Hello World")');
return someFile;
}

suite('PyProject.toml Create Env Features', () => {
let executeCommandStub: sinon.SinonStub;
const disposables: IDisposableRegistry = [];
let getOpenTextDocumentsStub: sinon.SinonStub;
let onDidOpenTextDocumentStub: sinon.SinonStub;
let onDidChangeTextDocumentStub: sinon.SinonStub;

setup(() => {
executeCommandStub = sinon.stub(cmdApis, 'executeCommand');
getOpenTextDocumentsStub = sinon.stub(workspaceApis, 'getOpenTextDocuments');
onDidOpenTextDocumentStub = sinon.stub(workspaceApis, 'onDidOpenTextDocument');
onDidChangeTextDocumentStub = sinon.stub(workspaceApis, 'onDidChangeTextDocument');

onDidOpenTextDocumentStub.returns(new FakeDisposable());
onDidChangeTextDocumentStub.returns(new FakeDisposable());
});

teardown(() => {
sinon.restore();
disposables.forEach((d) => d.dispose());
});

test('Installable pyproject.toml is already open in the editor on extension activate', async () => {
const pyprojectToml = getInstallableToml();
getOpenTextDocumentsStub.returns([pyprojectToml.object]);

registerPyProjectTomlCreateEnvFeatures(disposables);

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true));
});

test('Non installable pyproject.toml is already open in the editor on extension activate', async () => {
const pyprojectToml = getNonInstallableToml();
getOpenTextDocumentsStub.returns([pyprojectToml.object]);

registerPyProjectTomlCreateEnvFeatures(disposables);

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false));
});

test('Some random file open in the editor on extension activate', async () => {
const someFile = getSomeFile();
getOpenTextDocumentsStub.returns([someFile.object]);

registerPyProjectTomlCreateEnvFeatures(disposables);

assert.ok(executeCommandStub.notCalled);
});

test('Installable pyproject.toml is opened in the editor', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (doc: TextDocument) => void = () => {
/* do nothing */
};
onDidOpenTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const pyprojectToml = getInstallableToml();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler(pyprojectToml.object);

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true));
});

test('Non Installable pyproject.toml is opened in the editor', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (doc: TextDocument) => void = () => {
/* do nothing */
};
onDidOpenTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const pyprojectToml = getNonInstallableToml();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler(pyprojectToml.object);

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false));
});

test('Some random file is opened in the editor', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (doc: TextDocument) => void = () => {
/* do nothing */
};
onDidOpenTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const someFile = getSomeFile();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler(someFile.object);

assert.ok(executeCommandStub.notCalled);
});

test('Installable pyproject.toml is changed', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (d: TextDocumentChangeEvent) => void = () => {
/* do nothing */
};
onDidChangeTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const pyprojectToml = getInstallableToml();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined });

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true));
});

test('Non Installable pyproject.toml is changed', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (d: TextDocumentChangeEvent) => void = () => {
/* do nothing */
};
onDidChangeTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const pyprojectToml = getNonInstallableToml();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined });

assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false));
});

test('Some random file is changed', async () => {
getOpenTextDocumentsStub.returns([]);

let handler: (d: TextDocumentChangeEvent) => void = () => {
/* do nothing */
};
onDidChangeTextDocumentStub.callsFake((callback) => {
handler = callback;
return new FakeDisposable();
});

const someFile = getSomeFile();

registerPyProjectTomlCreateEnvFeatures(disposables);
handler({ contentChanges: [], document: someFile.object, reason: undefined });

assert.ok(executeCommandStub.notCalled);
});
});