Merge pull request #167437 from microsoft/tyriar/166031

Lazily attach the linedataeventaddon
This commit is contained in:
Daniel Imms 2022-11-28 13:52:00 -08:00 committed by GitHub
commit f9928a1846
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View file

@ -210,6 +210,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
private _usedShellIntegrationInjection: boolean = false;
get usedShellIntegrationInjection(): boolean { return this._usedShellIntegrationInjection; }
private _quickFixAddon: TerminalQuickFixAddon | undefined;
private _lineDataEventAddon: LineDataEventAddon | undefined;
readonly capabilities = new TerminalCapabilityStoreMultiplexer();
readonly statusList: ITerminalStatusList;
@ -337,7 +338,9 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
readonly onData = this._onData.event;
private readonly _onBinary = this._register(new Emitter<string>());
readonly onBinary = this._onBinary.event;
private readonly _onLineData = this._register(new Emitter<string>());
private readonly _onLineData = this._register(new Emitter<string>({
onDidAddFirstListener: () => this._onLineDataSetup()
}));
readonly onLineData = this._onLineData.event;
private readonly _onRequestExtHostProcess = this._register(new Emitter<ITerminalInstance>());
readonly onRequestExtHostProcess = this._onRequestExtHostProcess.event;
@ -737,8 +740,6 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this.xterm?.raw.loadAddon(this._quickFixAddon);
this.registerQuickFixProvider(gitTwoDashes(), freePort(this), gitSimilar(), gitPushSetUpstream(), gitCreatePr());
this._register(this._quickFixAddon.onDidRequestRerunCommand(async (e) => await this.runCommand(e.command, e.addNewLine || false)));
const lineDataEventAddon = new LineDataEventAddon();
this.xterm.raw.loadAddon(lineDataEventAddon);
this.updateAccessibilitySupport();
this.xterm.onDidRequestRunCommand(e => {
if (e.copyAsHtml) {
@ -749,13 +750,10 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
});
// Write initial text, deferring onLineFeed listener when applicable to avoid firing
// onLineData events containing initialText
if (this._shellLaunchConfig.initialText) {
this._writeInitialText(this.xterm, () => {
lineDataEventAddon.onLineData(e => this._onLineData.fire(e));
});
} else {
lineDataEventAddon.onLineData(e => this._onLineData.fire(e));
}
const initialTextWrittenPromise = this._shellLaunchConfig.initialText ? new Promise<void>(r => this._writeInitialText(xterm, r)) : undefined;
const lineDataEventAddon = new LineDataEventAddon(initialTextWrittenPromise);
lineDataEventAddon.onLineData(e => this._onLineData.fire(e));
this._lineDataEventAddon = lineDataEventAddon;
// Delay the creation of the bell listener to avoid showing the bell when the terminal
// starts up or reconnects
setTimeout(() => {
@ -825,6 +823,11 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
return xterm;
}
private async _onLineDataSetup(): Promise<void> {
const xterm = this.xterm || await this._xtermReadyPromise;
xterm.raw.loadAddon(this._lineDataEventAddon!);
}
async runCommand(commandLine: string, addNewLine: boolean): Promise<void> {
// Determine whether to send ETX (ctrl+c) before running the command. This should always
// happen unless command detection can reliably say that a command is being entered and

View file

@ -19,8 +19,16 @@ export class LineDataEventAddon extends Disposable implements ITerminalAddon {
private readonly _onLineData = this._register(new Emitter<string>());
readonly onLineData = this._onLineData.event;
activate(xterm: XTermTerminal) {
constructor(private readonly _initializationPromise?: Promise<void>) {
super();
}
async activate(xterm: XTermTerminal) {
this._xterm = xterm;
// If there is an initialization promise, wait for it before registering the event
await this._initializationPromise;
// Fire onLineData when a line feed occurs, taking into account wrapped lines
this._register(xterm.onLineFeed(() => {
const buffer = xterm.buffer;