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

Fix line normalization in Terminal.sendText() for IPython shell on Windows #238190 #238235

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 35 additions & 14 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to figure it out

Original file line number Diff line number Diff line change
Expand Up @@ -1262,26 +1262,47 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
}

async sendText(text: string, shouldExecute: boolean, bracketedPasteMode?: boolean): Promise<void> {
// Apply bracketed paste sequences if the terminal has the mode enabled, this will prevent
// the text from triggering keybindings and ensure new lines are handled properly
// Log incoming text for debugging
this._logService.debug('Original text received for processing:', text);

// Apply bracketed paste sequences if the terminal has the mode enabled,
// this will prevent the text from triggering keybindings and ensure new lines are handled properly
if (bracketedPasteMode && this.xterm?.raw.modes.bracketedPasteMode) {
text = `\x1b[200~${text}\x1b[201~`;
this._logService.debug('Bracketed paste mode enabled, applying paste sequences');
text = `\x1b[200~${text}\x1b[201~`; // Wrap text in bracketed paste
}

// Normalize line endings to 'enter' press.
text = text.replace(/\r?\n/g, '\r');
if (shouldExecute && !text.endsWith('\r')) {
text += '\r';
// Determine the appropriate line ending based on the platform
const lineEnding = process.platform === 'win32' ? '\r\n' : '\n'; // Use \r\n for Windows, \n for other platforms

// Normalize line endings to the platform-specific line ending
const normalizedText = text.replace(/\r?\n/g, lineEnding);
this._logService.debug('Text after line ending normalization:', normalizedText);

// Ensure the text ends with the correct line ending if it needs to be executed
if (shouldExecute && !normalizedText.endsWith(lineEnding)) {
this._logService.debug('Appending platform-specific line ending to text for execution');
text = normalizedText + lineEnding; // Ensure it ends with the appropriate line ending
} else {
text = normalizedText;
}

// Send it to the process
this._logService.debug('sending data (vscode)', text);
await this._processManager.write(text);
this._onDidInputData.fire(text);
this._onDidSendText.fire(text);
this.xterm?.scrollToBottom();
if (shouldExecute) {
this._onDidExecuteText.fire();
try {
this._logService.debug('Sending data to process:', text);
await this._processManager.write(text);
this._onDidInputData.fire(text); // Fire input event after writing text
this._onDidSendText.fire(text); // Fire send event
this.xterm?.scrollToBottom(); // Ensure terminal scrolls to the bottom

// Trigger execution if shouldExecute is true
if (shouldExecute) {
this._logService.debug('Executing text after sending:', text);
this._onDidExecuteText.fire();
}
} catch (error) {
// Log any errors encountered during processing
this._logService.error('Error while processing text in sendText:', error);
}
}

Expand Down