Skip to content

fix: debounce UI updates to python panel #549

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export interface EnvironmentManager {
quickCreateConfig?(): QuickCreateConfig | undefined;

/**
* Creates a new Python environment within the specified scope.
* Creates a new Python environment within the specified scope. Create should support adding a .gitignore file if it creates a folder within the workspace.
* @param scope - The scope within which to create the environment.
* @param options - Optional parameters for creating the Python environment.
* @returns A promise that resolves to the created Python environment, or undefined if creation failed.
Expand Down
2 changes: 1 addition & 1 deletion src/common/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface IEventNamePropertyMapping {
};

/* __GDPR__
"package.install": {
"package_management": {
"managerId": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "karthiknadig" },
"result": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "karthiknadig" }
}
Expand Down
57 changes: 33 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { commands, extensions, ExtensionContext, LogOutputChannel, Terminal, Uri, window, workspace } from 'vscode';
import { commands, ExtensionContext, extensions, LogOutputChannel, Terminal, Uri, window, workspace } from 'vscode';
import { PythonEnvironment, PythonEnvironmentApi, PythonProjectCreator } from './api';
import { ensureCorrectVersion } from './common/extVersion';
import { registerLogger, traceError, traceInfo } from './common/logging';
Expand All @@ -8,6 +8,7 @@ import { StopWatch } from './common/stopWatch';
import { EventNames } from './common/telemetry/constants';
import { sendManagerSelectionTelemetry } from './common/telemetry/helpers';
import { sendTelemetryEvent } from './common/telemetry/sender';
import { createSimpleDebounce } from './common/utils/debounce';
import { createDeferred } from './common/utils/deferred';
import {
activeTerminal,
Expand Down Expand Up @@ -75,27 +76,27 @@ import { registerPyenvFeatures } from './managers/pyenv/main';
async function collectEnvironmentInfo(
context: ExtensionContext,
envManagers: EnvironmentManagers,
projectManager: PythonProjectManager
projectManager: PythonProjectManager,
): Promise<string> {
const info: string[] = [];

try {
// Extension version
const extensionVersion = context.extension?.packageJSON?.version || 'unknown';
info.push(`Extension Version: ${extensionVersion}`);

// Python extension version
const pythonExtension = extensions.getExtension('ms-python.python');
const pythonVersion = pythonExtension?.packageJSON?.version || 'not installed';
info.push(`Python Extension Version: ${pythonVersion}`);

// Environment managers
const managers = envManagers.managers;
info.push(`\nRegistered Environment Managers (${managers.length}):`);
managers.forEach(manager => {
managers.forEach((manager) => {
info.push(` - ${manager.id} (${manager.displayName})`);
});

// Available environments
const allEnvironments: PythonEnvironment[] = [];
for (const manager of managers) {
Expand All @@ -106,7 +107,7 @@ async function collectEnvironmentInfo(
info.push(` Error getting environments from ${manager.id}: ${err}`);
}
}

info.push(`\nTotal Available Environments: ${allEnvironments.length}`);
if (allEnvironments.length > 0) {
info.push('Environment Details:');
Expand All @@ -117,8 +118,9 @@ async function collectEnvironmentInfo(
info.push(` ... and ${allEnvironments.length - 10} more environments`);
}
}

// Python projects
console.log('getProjects called from extension.ts activate');
const projects = projectManager.getProjects();
info.push(`\nPython Projects (${projects.length}):`);
for (let index = 0; index < projects.length; index++) {
Expand All @@ -133,22 +135,25 @@ async function collectEnvironmentInfo(
info.push(` Error getting environment: ${err}`);
}
}

// Current settings (non-sensitive)
const config = workspace.getConfiguration('python-envs');
info.push('\nExtension Settings:');
info.push(` Default Environment Manager: ${config.get('defaultEnvManager')}`);
info.push(` Default Package Manager: ${config.get('defaultPackageManager')}`);
info.push(` Terminal Auto Activation: ${config.get('terminal.autoActivationType')}`);

} catch (err) {
info.push(`\nError collecting environment information: ${err}`);
}

return info.join('\n');
}

export async function activate(context: ExtensionContext): Promise<PythonEnvironmentApi> {
// Debounced version of updateViewsAndStatus to avoid excessive UI updates
const debouncedUpdateViewsAndStatus = createSimpleDebounce(200, () => {
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
});
const start = new StopWatch();

// Logging should be set up before anything else.
Expand Down Expand Up @@ -366,11 +371,11 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
commands.registerCommand('python-envs.reportIssue', async () => {
try {
const issueData = await collectEnvironmentInfo(context, envManagers, projectManager);

await commands.executeCommand('workbench.action.openIssueReporter', {
extensionId: 'ms-python.vscode-python-envs',
issueTitle: '[Python Environments] ',
issueBody: `<!-- Please describe the issue you're experiencing -->\n\n<!-- The following information was automatically generated -->\n\n<details>\n<summary>Environment Information</summary>\n\n\`\`\`\n${issueData}\n\`\`\`\n\n</details>`
issueBody: `<!-- Please describe the issue you're experiencing -->\n\n<!-- The following information was automatically generated -->\n\n<details>\n<summary>Environment Information</summary>\n\n\`\`\`\n${issueData}\n\`\`\`\n\n</details>`,
});
} catch (error) {
window.showErrorMessage(`Failed to open issue reporter: ${error}`);
Expand All @@ -387,22 +392,16 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
}
}
}),
window.onDidChangeActiveTextEditor(async () => {
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
}),
envManagers.onDidChangeEnvironment(async () => {
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
window.onDidChangeActiveTextEditor(() => {
debouncedUpdateViewsAndStatus.trigger();
}),
envManagers.onDidChangeEnvironments(async () => {
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
}),
envManagers.onDidChangeEnvironmentFiltered(async (e) => {
envManagers.onDidChangeEnvironmentFiltered((e) => {
managerView.environmentChanged(e);
const location = e.uri?.fsPath ?? 'global';
traceInfo(
`Internal: Changed environment from ${e.old?.displayName} to ${e.new?.displayName} for: ${location}`,
);
updateViewsAndStatus(statusBar, workspaceView, managerView, api);
debouncedUpdateViewsAndStatus.trigger();
}),
onDidChangeTerminalShellIntegration(async (e) => {
const shellEnv = e.shellIntegration?.env;
Expand All @@ -428,6 +427,16 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
}),
);

// Register these listeners last to avoid triggering them during activation/setup
context.subscriptions.push(
envManagers.onDidChangeEnvironment(() => {
debouncedUpdateViewsAndStatus.trigger();
}),
envManagers.onDidChangeEnvironments(() => {
debouncedUpdateViewsAndStatus.trigger();
}),
);

/**
* Below are all the contributed features using the APIs.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/features/creators/newPackageProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class NewPackageProject implements PythonProjectCreator {
uri: Uri.file(projectDestinationFolder),
};
// add package to list of packages
this.projectManager.add(createdPackage);
await this.projectManager.add(createdPackage);

// 4. Create virtual environment if requested
let createdEnv: PythonEnvironment | undefined;
Expand Down
1 change: 0 additions & 1 deletion src/features/projectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export class PythonProjectManagerImpl implements PythonProjectManager {
}

getProjects(uris?: Uri[]): ReadonlyArray<PythonProject> {
console.log('getProjects', uris);
if (uris === undefined) {
return Array.from(this._projects.values());
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/managers/builtin/venvManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs/promises';
import * as path from 'path';
import { EventEmitter, l10n, LogOutputChannel, MarkdownString, ProgressLocation, ThemeIcon, Uri } from 'vscode';
import {
Expand All @@ -20,6 +21,7 @@ import {
} from '../../api';
import { PYTHON_EXTENSION_ID } from '../../common/constants';
import { VenvManagerStrings } from '../../common/localize';
import { traceError } from '../../common/logging';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage, withProgress } from '../../common/window.apis';
import { findParentIfFile } from '../../features/envCommands';
Expand Down Expand Up @@ -162,6 +164,17 @@ export class VenvManager implements EnvironmentManager {
}
if (environment) {
this.addEnvironment(environment, true);

// Add .gitignore to the .venv folder
try {
const venvDir = environment.environmentPath.fsPath;
const gitignorePath = path.join(venvDir, '.gitignore');
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
} catch (err) {
traceError(
`Failed to create .gitignore in venv: ${err instanceof Error ? err.message : String(err)}`,
);
}
}
return environment;
} finally {
Expand Down
16 changes: 16 additions & 0 deletions src/managers/conda/condaEnvManager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { Disposable, EventEmitter, l10n, LogOutputChannel, MarkdownString, ProgressLocation, Uri } from 'vscode';
import {
Expand All @@ -19,6 +20,7 @@ import {
SetEnvironmentScope,
} from '../../api';
import { CondaStrings } from '../../common/localize';
import { traceError } from '../../common/logging';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage, withProgress } from '../../common/window.apis';
import { NativePythonFinder } from '../common/nativePythonFinder';
Expand Down Expand Up @@ -167,6 +169,20 @@ export class CondaEnvManager implements EnvironmentManager, Disposable {
}
if (result) {
this.addEnvironment(result);

// If the environment is inside the workspace, add a .gitignore file
try {
const projectUris = this.api.getPythonProjects().map((p) => p.uri.fsPath);
const envPath = result.environmentPath?.fsPath;
if (envPath && projectUris.some((root) => envPath.startsWith(root))) {
const gitignorePath = path.join(envPath, '.gitignore');
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
}
} catch (err) {
traceError(
`Failed to create .gitignore in conda env: ${err instanceof Error ? err.message : String(err)}`,
);
}
}

return result;
Expand Down