Skip to content

Commit

Permalink
Add createEnvironment.contentButton setting (#21212)
Browse files Browse the repository at this point in the history
Closes #20982

---------

Co-authored-by: Luciana Abud <[email protected]>
  • Loading branch information
karthiknadig and luabud authored May 10, 2023
1 parent 5eef525 commit eb9fde3
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 230 deletions.
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@
"type": "array",
"uniqueItems": true
},
"python.createEnvironment.contentButton": {
"default": "show",
"markdownDescription": "%python.createEnvironment.contentButton.description%",
"scope": "machine-overridable",
"type": "string",
"enum": [
"show",
"hide"
],
"tags": [
"experimental"
]
},
"python.condaPath": {
"default": "",
"description": "%python.condaPath.description%",
Expand Down Expand Up @@ -1707,12 +1720,12 @@
{
"group": "Python",
"command": "python.createEnvironment-button",
"when": "resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
"when": "showCreateEnvButton && resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
},
{
"group": "Python",
"command": "python.createEnvironment-button",
"when": "resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
"when": "showCreateEnvButton && resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor"
}
],
"editor/context": [
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"python.command.python.analysis.restartLanguageServer.title": "Restart Language Server",
"python.command.python.launchTensorBoard.title": "Launch TensorBoard",
"python.command.python.refreshTensorBoard.title": "Refresh TensorBoard",
"python.createEnvironment.contentButton.description": "Show or hide Create Environment button in the editor for `requirements.txt` or other dependency files.",
"python.menu.createNewFile.title": "Python File",
"python.editor.context.submenu.runPython": "Run Python",
"python.editor.context.submenu.runPythonInteractive": "Run in Interactive window",
Expand Down
8 changes: 6 additions & 2 deletions src/client/common/vscodeApis/workspaceApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ export function getOpenTextDocuments(): readonly vscode.TextDocument[] {
return vscode.workspace.textDocuments;
}

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

export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => void): vscode.Disposable {
export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => unknown): vscode.Disposable {
return vscode.workspace.onDidChangeTextDocument(handler);
}

export function onDidChangeConfiguration(handler: (e: vscode.ConfigurationChangeEvent) => unknown): vscode.Disposable {
return vscode.workspace.onDidChangeConfiguration(handler);
}
4 changes: 2 additions & 2 deletions src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import { DynamicPythonDebugConfigurationService } from './debugger/extension/con
import { registerCreateEnvironmentFeatures } from './pythonEnvironments/creation/createEnvApi';
import { IInterpreterQuickPick } from './interpreter/configuration/types';
import { registerInstallFormatterPrompt } from './providers/prompts/installFormatterPrompt';
import { registerPyProjectTomlCreateEnvFeatures } from './pythonEnvironments/creation/pyprojectTomlCreateEnv';
import { registerCreateEnvButtonFeatures } from './pythonEnvironments/creation/createEnvButtonContext';

export async function activateComponents(
// `ext` is passed to any extra activation funcs.
Expand Down Expand Up @@ -98,7 +98,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);
registerCreateEnvButtonFeatures(ext.disposables);
}

/// //////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
onDidOpenTextDocument,
onDidChangeTextDocument,
getOpenTextDocuments,
getConfiguration,
onDidChangeConfiguration,
} from '../../common/vscodeApis/workspaceApis';
import { isPipInstallableToml } from './provider/venvUtils';

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

export function registerPyProjectTomlCreateEnvFeatures(disposables: IDisposableRegistry): void {
async function setShowCreateEnvButtonContextKey(): Promise<void> {
const config = getConfiguration('python');
const showCreateEnvButton = config.get<string>('createEnvironment.contentButton', 'show') === 'show';
await executeCommand('setContext', 'showCreateEnvButton', showCreateEnvButton);
}

export function registerCreateEnvButtonFeatures(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);
const doc = e.document;
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
}),
onDidChangeConfiguration(async () => {
await setShowCreateEnvButtonContextKey();
}),
);

getOpenTextDocuments().forEach(async (doc: TextDocument) => {
if (doc.fileName.endsWith('pyproject.toml')) {
await setPyProjectTomlContextKey(doc);
}
});
setShowCreateEnvButtonContextKey();

const docs = getOpenTextDocuments().filter(
(doc) => doc.fileName.endsWith('pyproject.toml') && isPipInstallableToml(doc.getText()),
);
if (docs.length > 0) {
executeCommand('setContext', 'pipInstallableToml', true);
} else {
executeCommand('setContext', 'pipInstallableToml', false);
}
}
Loading

0 comments on commit eb9fde3

Please sign in to comment.