adopt new terminal API

This commit is contained in:
Andre Weinand 2019-09-16 23:16:22 +02:00
parent 44eb607fc6
commit 1961739dfc

View file

@ -21,7 +21,9 @@ interface ServerReadyAction {
} }
class ServerReadyDetector extends vscode.Disposable { class ServerReadyDetector extends vscode.Disposable {
static detectors = new Map<vscode.DebugSession, ServerReadyDetector>();
private static detectors = new Map<vscode.DebugSession, ServerReadyDetector>();
private static terminalDataListener: vscode.Disposable | undefined;
private hasFired = false; private hasFired = false;
private shellPid?: number; private shellPid?: number;
@ -55,6 +57,29 @@ class ServerReadyDetector extends vscode.Disposable {
} }
} }
static async startListeningTerminalData() {
if (!this.terminalDataListener) {
this.terminalDataListener = vscode.window.onDidWriteTerminalData(async e => {
// first find the detector with a matching pid
const pid = await e.terminal.processId;
for (let [, detector] of this.detectors) {
if (detector.shellPid === pid) {
detector.detectPattern(e.data);
return;
}
}
// if none found, try all detectors until one matches
for (let [, detector] of this.detectors) {
if (detector.detectPattern(e.data)) {
return;
}
}
});
}
}
private constructor(private session: vscode.DebugSession) { private constructor(private session: vscode.DebugSession) {
super(() => this.internalDispose()); super(() => this.internalDispose());
@ -66,35 +91,17 @@ class ServerReadyDetector extends vscode.Disposable {
this.disposables = []; this.disposables = [];
} }
async trackTerminals() { detectPattern(s: string): boolean {
let terminals: vscode.Terminal[] = [];
// either find the terminal where the debug is started with "runInTerminal" or use all terminals
for (let terminal of vscode.window.terminals) {
if (!this.shellPid || await terminal.processId === this.shellPid) {
terminals.push(terminal);
}
}
this.shellPid = undefined;
this.disposables.push(vscode.window.onDidWriteTerminalData(e => {
if (terminals.indexOf(e.terminal) !== -1) {
this.detectPattern(e.data);
}
}));
}
detectPattern(s: string): void {
if (!this.hasFired) { if (!this.hasFired) {
const matches = this.regexp.exec(s); const matches = this.regexp.exec(s);
if (matches && matches.length >= 1) { if (matches && matches.length >= 1) {
this.openExternalWithString(this.session, matches.length > 1 ? matches[1] : ''); this.openExternalWithString(this.session, matches.length > 1 ? matches[1] : '');
this.hasFired = true; this.hasFired = true;
this.internalDispose(); this.internalDispose();
return true;
} }
} }
return false;
} }
private openExternalWithString(session: vscode.DebugSession, captureString: string) { private openExternalWithString(session: vscode.DebugSession, captureString: string) {
@ -132,11 +139,12 @@ class ServerReadyDetector extends vscode.Disposable {
const args: ServerReadyAction = session.configuration.serverReadyAction; const args: ServerReadyAction = session.configuration.serverReadyAction;
switch (args.action || 'openExternally') { switch (args.action || 'openExternally') {
case 'openExternally': case 'openExternally':
vscode.env.openExternal(vscode.Uri.parse(uri)); vscode.env.openExternal(vscode.Uri.parse(uri));
break; break;
case 'debugWithChrome':
case 'debugWithChrome':
if (vscode.env.remoteName === 'wsl' || !!vscode.extensions.getExtension('msjsdiag.debugger-for-chrome')) { if (vscode.env.remoteName === 'wsl' || !!vscode.extensions.getExtension('msjsdiag.debugger-for-chrome')) {
vscode.debug.startDebugging(session.workspaceFolder, { vscode.debug.startDebugging(session.workspaceFolder, {
type: 'chrome', type: 'chrome',
@ -150,6 +158,7 @@ class ServerReadyDetector extends vscode.Disposable {
vscode.window.showErrorMessage(errMsg, { modal: true }).then(_ => undefined); vscode.window.showErrorMessage(errMsg, { modal: true }).then(_ => undefined);
} }
break; break;
default: default:
// not supported // not supported
break; break;
@ -163,7 +172,7 @@ export function activate(context: vscode.ExtensionContext) {
if (session && session.configuration.serverReadyAction) { if (session && session.configuration.serverReadyAction) {
const detector = ServerReadyDetector.start(session); const detector = ServerReadyDetector.start(session);
if (detector) { if (detector) {
detector.trackTerminals(); ServerReadyDetector.startListeningTerminalData();
} }
} }
})); }));