Skip to content

Add option to reuse existing Python terminals for code execution #25158

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 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 10, 2025

This PR implements a new feature to reuse existing active Python interpreter terminals instead of always creating new ones when running code selections or lines. This addresses workflows where users work with pre-configured terminals (e.g., SLURM allocated resources) and want to avoid creating multiple terminal instances.

Changes

New Configuration Setting

  • Added python.terminal.reuseActiveTerminal setting (default: true)
  • Controls whether code execution should try to reuse existing Python terminals
  • Fully configurable per workspace/user preferences

Smart Terminal Detection

The implementation intelligently identifies existing Python terminals by:

  • Scanning window.terminals for active terminals
  • Matching terminals with names containing "python" or "repl"
  • Detecting terminals where terminal.state?.shell === 'python'
  • Skipping closed or exited terminals

Modified Execution Flow

  • initializeRepl() now tries to find existing Python terminals first
  • Falls back to creating new terminals when none are found
  • Stores references to reused terminals for direct communication
  • Handles terminal cleanup when terminals are closed

Seamless Integration

  • Uses terminal.sendText() for existing terminals
  • Maintains existing terminal.executeCommand() for new terminals
  • Preserves all existing functionality and backwards compatibility

Example Usage

With the setting enabled (default), when you:

  1. Have an existing Python terminal open
  2. Run "Selection/Line in Python Terminal"
  3. Code is sent to the existing terminal instead of creating a new one

When disabled, behavior reverts to the original implementation of always creating new terminals.

Benefits

  • Solves SLURM workflow issues: Reuses terminals with allocated resources
  • Reduces terminal proliferation: Fewer unnecessary terminal instances
  • Resource efficient: Better memory and process management
  • User configurable: Can be disabled if the old behavior is preferred
  • Backwards compatible: Graceful fallback when no suitable terminals exist

Testing

  • Added configuration tests to validate the new setting
  • Verified existing terminal tests continue to pass
  • Confirmed build stability and backwards compatibility

Fixes #24960.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 10, 2025 04:38
Co-authored-by: anthonykim1 <62267334+anthonykim1@users.noreply.github.com>
Co-authored-by: anthonykim1 <62267334+anthonykim1@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Run Selection/Line in already active python interpreter terminal rather than opening a new terminal Add option to reuse existing Python terminals for code execution Jun 10, 2025
@Copilot Copilot AI requested a review from anthonykim1 June 10, 2025 04:43
Copilot finished work on behalf of anthonykim1 June 10, 2025 04:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run Selection/Line in already active python interpreter terminal rather than opening a new terminal
2 participants