|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { Emitter, type Event } from 'vs/base/common/event'; |
| 7 | +import { Disposable } from 'vs/base/common/lifecycle'; |
| 8 | +import { ILogService, LogLevel } from 'vs/platform/log/common/log'; |
| 9 | +import type { ITerminalCommand } from 'vs/platform/terminal/common/capabilities/capabilities'; |
| 10 | +import { debounce } from 'vs/base/common/decorators'; |
| 11 | + |
| 12 | +// Importing types is safe in any layer |
| 13 | +// eslint-disable-next-line local/code-import-patterns |
| 14 | +import type { Terminal, IMarker, IBufferLine, IBuffer } from '@xterm/headless'; |
| 15 | + |
| 16 | +const enum PromptInputState { |
| 17 | + Unknown, |
| 18 | + Input, |
| 19 | + Execute, |
| 20 | +} |
| 21 | + |
| 22 | +export interface IPromptInputModel { |
| 23 | + readonly onDidStartInput: Event<void>; |
| 24 | + readonly onDidChangeInput: Event<void>; |
| 25 | + readonly onDidFinishInput: Event<void>; |
| 26 | + |
| 27 | + readonly value: string; |
| 28 | + readonly cursorIndex: number; |
| 29 | +} |
| 30 | + |
| 31 | +export class PromptInputModel extends Disposable implements IPromptInputModel { |
| 32 | + private _state: PromptInputState = PromptInputState.Unknown; |
| 33 | + |
| 34 | + private _commandStartMarker: IMarker | undefined; |
| 35 | + private _commandStartX: number = 0; |
| 36 | + private _continuationPrompt: string | undefined; |
| 37 | + |
| 38 | + private _value: string = ''; |
| 39 | + get value() { return this._value; } |
| 40 | + |
| 41 | + private _cursorIndex: number = 0; |
| 42 | + get cursorIndex() { return this._cursorIndex; } |
| 43 | + |
| 44 | + private readonly _onDidStartInput = this._register(new Emitter<void>()); |
| 45 | + readonly onDidStartInput = this._onDidStartInput.event; |
| 46 | + private readonly _onDidChangeInput = this._register(new Emitter<void>()); |
| 47 | + readonly onDidChangeInput = this._onDidChangeInput.event; |
| 48 | + private readonly _onDidFinishInput = this._register(new Emitter<void>()); |
| 49 | + readonly onDidFinishInput = this._onDidFinishInput.event; |
| 50 | + |
| 51 | + constructor( |
| 52 | + private readonly _xterm: Terminal, |
| 53 | + onCommandStart: Event<ITerminalCommand>, |
| 54 | + onCommandExecuted: Event<ITerminalCommand>, |
| 55 | + @ILogService private readonly _logService: ILogService |
| 56 | + ) { |
| 57 | + super(); |
| 58 | + |
| 59 | + this._register(this._xterm.onData(e => this._handleInput(e))); |
| 60 | + this._register(this._xterm.onCursorMove(() => this._sync())); |
| 61 | + |
| 62 | + this._register(onCommandStart(e => this._handleCommandStart(e as { marker: IMarker }))); |
| 63 | + this._register(onCommandExecuted(() => this._handleCommandExecuted())); |
| 64 | + } |
| 65 | + |
| 66 | + setContinuationPrompt(value: string): void { |
| 67 | + this._continuationPrompt = value; |
| 68 | + } |
| 69 | + |
| 70 | + private _handleCommandStart(command: { marker: IMarker }) { |
| 71 | + if (this._state === PromptInputState.Input) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + this._state = PromptInputState.Input; |
| 76 | + this._commandStartMarker = command.marker; |
| 77 | + this._commandStartX = this._xterm.buffer.active.cursorX; |
| 78 | + this._value = ''; |
| 79 | + this._cursorIndex = 0; |
| 80 | + this._onDidStartInput.fire(); |
| 81 | + } |
| 82 | + |
| 83 | + private _handleCommandExecuted() { |
| 84 | + if (this._state === PromptInputState.Execute) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + this._state = PromptInputState.Execute; |
| 89 | + this._onDidFinishInput.fire(); |
| 90 | + } |
| 91 | + |
| 92 | + private _handleInput(data: string) { |
| 93 | + this._sync(); |
| 94 | + } |
| 95 | + |
| 96 | + @debounce(50) |
| 97 | + private _sync() { |
| 98 | + this._syncNow(); |
| 99 | + } |
| 100 | + |
| 101 | + protected _syncNow() { |
| 102 | + if (this._state !== PromptInputState.Input) { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const commandStartY = this._commandStartMarker?.line; |
| 107 | + if (commandStartY === undefined) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const buffer = this._xterm.buffer.active; |
| 112 | + let line = buffer.getLine(commandStartY); |
| 113 | + const commandLine = line?.translateToString(true, this._commandStartX); |
| 114 | + if (!commandLine || !line) { |
| 115 | + this._logService.trace(`PromptInputModel#_sync: no line`); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Command start line |
| 120 | + this._value = commandLine; |
| 121 | + |
| 122 | + // Get cursor index |
| 123 | + const absoluteCursorY = buffer.baseY + buffer.cursorY; |
| 124 | + this._cursorIndex = absoluteCursorY === commandStartY ? this._getRelativeCursorIndex(this._commandStartX, buffer, line) : commandLine.length + 1; |
| 125 | + |
| 126 | + // IDEA: Detect ghost text based on SGR and cursor. This might work by checking for italic |
| 127 | + // or dim only to avoid false positives from shells that do immediate coloring. |
| 128 | + // IDEA: Detect line continuation if it's not set |
| 129 | + |
| 130 | + // From command start line to cursor line |
| 131 | + for (let y = commandStartY + 1; y <= absoluteCursorY; y++) { |
| 132 | + line = buffer.getLine(y); |
| 133 | + let lineText = line?.translateToString(true); |
| 134 | + if (lineText && line) { |
| 135 | + // Verify continuation prompt if we have it, if this line doesn't have it then the |
| 136 | + // user likely just pressed enter |
| 137 | + if (this._continuationPrompt === undefined || this._lineContainsContinuationPrompt(lineText)) { |
| 138 | + lineText = this._trimContinuationPrompt(lineText); |
| 139 | + this._value += `\n${lineText}`; |
| 140 | + this._cursorIndex += (absoluteCursorY === y |
| 141 | + ? this._getRelativeCursorIndex(this._getContinuationPromptCellWidth(line, lineText), buffer, line) |
| 142 | + : lineText.length + 1); |
| 143 | + } else { |
| 144 | + break; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + // Below cursor line |
| 150 | + for (let y = absoluteCursorY + 1; y < buffer.baseY + this._xterm.rows; y++) { |
| 151 | + line = buffer.getLine(y); |
| 152 | + const lineText = line?.translateToString(true); |
| 153 | + if (lineText && line) { |
| 154 | + if (this._continuationPrompt === undefined || this._lineContainsContinuationPrompt(lineText)) { |
| 155 | + this._value += `\n${this._trimContinuationPrompt(lineText)}`; |
| 156 | + } else { |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if (this._logService.getLevel() === LogLevel.Trace) { |
| 163 | + this._logService.trace(`PromptInputModel#_sync: Input="${this._value.substring(0, this._cursorIndex)}|${this.value.substring(this._cursorIndex)}"`); |
| 164 | + } |
| 165 | + |
| 166 | + this._onDidChangeInput.fire(); |
| 167 | + } |
| 168 | + |
| 169 | + private _trimContinuationPrompt(lineText: string): string { |
| 170 | + if (this._lineContainsContinuationPrompt(lineText)) { |
| 171 | + lineText = lineText.substring(this._continuationPrompt!.length); |
| 172 | + } |
| 173 | + return lineText; |
| 174 | + } |
| 175 | + |
| 176 | + private _lineContainsContinuationPrompt(lineText: string): boolean { |
| 177 | + return !!(this._continuationPrompt && lineText.startsWith(this._continuationPrompt)); |
| 178 | + } |
| 179 | + |
| 180 | + private _getContinuationPromptCellWidth(line: IBufferLine, lineText: string): number { |
| 181 | + if (!this._continuationPrompt || !lineText.startsWith(this._continuationPrompt)) { |
| 182 | + return 0; |
| 183 | + } |
| 184 | + let buffer = ''; |
| 185 | + let x = 0; |
| 186 | + while (buffer !== this._continuationPrompt) { |
| 187 | + buffer += line.getCell(x++)!.getChars(); |
| 188 | + } |
| 189 | + return x; |
| 190 | + } |
| 191 | + |
| 192 | + private _getRelativeCursorIndex(startCellX: number, buffer: IBuffer, line: IBufferLine): number { |
| 193 | + return line?.translateToString(true, startCellX, buffer.cursorX).length ?? 0; |
| 194 | + } |
| 195 | +} |
0 commit comments