Skip to content

Commit eb9fde3

Browse files
karthiknadigluabud
andauthored
Add createEnvironment.contentButton setting (#21212)
Closes #20982 --------- Co-authored-by: Luciana Abud <45497113+luabud@users.noreply.github.com>
1 parent 5eef525 commit eb9fde3

File tree

7 files changed

+395
-230
lines changed

7 files changed

+395
-230
lines changed

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@
404404
"type": "array",
405405
"uniqueItems": true
406406
},
407+
"python.createEnvironment.contentButton": {
408+
"default": "show",
409+
"markdownDescription": "%python.createEnvironment.contentButton.description%",
410+
"scope": "machine-overridable",
411+
"type": "string",
412+
"enum": [
413+
"show",
414+
"hide"
415+
],
416+
"tags": [
417+
"experimental"
418+
]
419+
},
407420
"python.condaPath": {
408421
"default": "",
409422
"description": "%python.condaPath.description%",
@@ -1707,12 +1720,12 @@
17071720
{
17081721
"group": "Python",
17091722
"command": "python.createEnvironment-button",
1710-
"when": "resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
1723+
"when": "showCreateEnvButton && resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
17111724
},
17121725
{
17131726
"group": "Python",
17141727
"command": "python.createEnvironment-button",
1715-
"when": "resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
1728+
"when": "showCreateEnvButton && resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
17161729
}
17171730
],
17181731
"editor/context": [

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"python.command.python.analysis.restartLanguageServer.title": "Restart Language Server",
2626
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
2727
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
28+
"python.createEnvironment.contentButton.description": "Show or hide Create Environment button in the editor for `requirements.txt` or other dependency files.",
2829
"python.menu.createNewFile.title": "Python File",
2930
"python.editor.context.submenu.runPython": "Run Python",
3031
"python.editor.context.submenu.runPythonInteractive": "Run in Interactive window",

src/client/common/vscodeApis/workspaceApis.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ export function getOpenTextDocuments(): readonly vscode.TextDocument[] {
4848
return vscode.workspace.textDocuments;
4949
}
5050

51-
export function onDidOpenTextDocument(handler: (doc: vscode.TextDocument) => void): vscode.Disposable {
51+
export function onDidOpenTextDocument(handler: (doc: vscode.TextDocument) => unknown): vscode.Disposable {
5252
return vscode.workspace.onDidOpenTextDocument(handler);
5353
}
5454

55-
export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => void): vscode.Disposable {
55+
export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => unknown): vscode.Disposable {
5656
return vscode.workspace.onDidChangeTextDocument(handler);
5757
}
58+
59+
export function onDidChangeConfiguration(handler: (e: vscode.ConfigurationChangeEvent) => unknown): vscode.Disposable {
60+
return vscode.workspace.onDidChangeConfiguration(handler);
61+
}

src/client/extensionActivation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import { DynamicPythonDebugConfigurationService } from './debugger/extension/con
5454
import { registerCreateEnvironmentFeatures } from './pythonEnvironments/creation/createEnvApi';
5555
import { IInterpreterQuickPick } from './interpreter/configuration/types';
5656
import { registerInstallFormatterPrompt } from './providers/prompts/installFormatterPrompt';
57-
import { registerPyProjectTomlCreateEnvFeatures } from './pythonEnvironments/creation/pyprojectTomlCreateEnv';
57+
import { registerCreateEnvButtonFeatures } from './pythonEnvironments/creation/createEnvButtonContext';
5858

5959
export async function activateComponents(
6060
// `ext` is passed to any extra activation funcs.
@@ -98,7 +98,7 @@ export function activateFeatures(ext: ExtensionState, _components: Components):
9898
);
9999
const pathUtils = ext.legacyIOC.serviceContainer.get<IPathUtils>(IPathUtils);
100100
registerCreateEnvironmentFeatures(ext.disposables, interpreterQuickPick, interpreterPathService, pathUtils);
101-
registerPyProjectTomlCreateEnvFeatures(ext.disposables);
101+
registerCreateEnvButtonFeatures(ext.disposables);
102102
}
103103

104104
/// //////////////////////////

src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts renamed to src/client/pythonEnvironments/creation/createEnvButtonContext.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
onDidOpenTextDocument,
99
onDidChangeTextDocument,
1010
getOpenTextDocuments,
11+
getConfiguration,
12+
onDidChangeConfiguration,
1113
} from '../../common/vscodeApis/workspaceApis';
1214
import { isPipInstallableToml } from './provider/venvUtils';
1315

@@ -19,23 +21,38 @@ async function setPyProjectTomlContextKey(doc: TextDocument): Promise<void> {
1921
}
2022
}
2123

22-
export function registerPyProjectTomlCreateEnvFeatures(disposables: IDisposableRegistry): void {
24+
async function setShowCreateEnvButtonContextKey(): Promise<void> {
25+
const config = getConfiguration('python');
26+
const showCreateEnvButton = config.get<string>('createEnvironment.contentButton', 'show') === 'show';
27+
await executeCommand('setContext', 'showCreateEnvButton', showCreateEnvButton);
28+
}
29+
30+
export function registerCreateEnvButtonFeatures(disposables: IDisposableRegistry): void {
2331
disposables.push(
2432
onDidOpenTextDocument(async (doc: TextDocument) => {
2533
if (doc.fileName.endsWith('pyproject.toml')) {
2634
await setPyProjectTomlContextKey(doc);
2735
}
2836
}),
2937
onDidChangeTextDocument(async (e: TextDocumentChangeEvent) => {
30-
if (e.document.fileName.endsWith('pyproject.toml')) {
31-
await setPyProjectTomlContextKey(e.document);
38+
const doc = e.document;
39+
if (doc.fileName.endsWith('pyproject.toml')) {
40+
await setPyProjectTomlContextKey(doc);
3241
}
3342
}),
43+
onDidChangeConfiguration(async () => {
44+
await setShowCreateEnvButtonContextKey();
45+
}),
3446
);
3547

36-
getOpenTextDocuments().forEach(async (doc: TextDocument) => {
37-
if (doc.fileName.endsWith('pyproject.toml')) {
38-
await setPyProjectTomlContextKey(doc);
39-
}
40-
});
48+
setShowCreateEnvButtonContextKey();
49+
50+
const docs = getOpenTextDocuments().filter(
51+
(doc) => doc.fileName.endsWith('pyproject.toml') && isPipInstallableToml(doc.getText()),
52+
);
53+
if (docs.length > 0) {
54+
executeCommand('setContext', 'pipInstallableToml', true);
55+
} else {
56+
executeCommand('setContext', 'pipInstallableToml', false);
57+
}
4158
}

0 commit comments

Comments
 (0)