Skip to content

Commit

Permalink
adopt new terminal API
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Sep 16, 2019
1 parent 44eb607 commit 1961739
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions extensions/debug-server-ready/src/extension.ts
Expand Up @@ -21,7 +21,9 @@ interface ServerReadyAction {
}

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 shellPid?: number;
Expand Down Expand Up @@ -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) {
super(() => this.internalDispose());

Expand All @@ -66,35 +91,17 @@ class ServerReadyDetector extends vscode.Disposable {
this.disposables = [];
}

async trackTerminals() {

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 {

detectPattern(s: string): boolean {
if (!this.hasFired) {
const matches = this.regexp.exec(s);
if (matches && matches.length >= 1) {
this.openExternalWithString(this.session, matches.length > 1 ? matches[1] : '');
this.hasFired = true;
this.internalDispose();
return true;
}
}
return false;
}

private openExternalWithString(session: vscode.DebugSession, captureString: string) {
Expand Down Expand Up @@ -132,11 +139,12 @@ class ServerReadyDetector extends vscode.Disposable {

const args: ServerReadyAction = session.configuration.serverReadyAction;
switch (args.action || 'openExternally') {

case 'openExternally':
vscode.env.openExternal(vscode.Uri.parse(uri));
break;
case 'debugWithChrome':

case 'debugWithChrome':
if (vscode.env.remoteName === 'wsl' || !!vscode.extensions.getExtension('msjsdiag.debugger-for-chrome')) {
vscode.debug.startDebugging(session.workspaceFolder, {
type: 'chrome',
Expand All @@ -150,6 +158,7 @@ class ServerReadyDetector extends vscode.Disposable {
vscode.window.showErrorMessage(errMsg, { modal: true }).then(_ => undefined);
}
break;

default:
// not supported
break;
Expand All @@ -163,7 +172,7 @@ export function activate(context: vscode.ExtensionContext) {
if (session && session.configuration.serverReadyAction) {
const detector = ServerReadyDetector.start(session);
if (detector) {
detector.trackTerminals();
ServerReadyDetector.startListeningTerminalData();
}
}
}));
Expand Down

0 comments on commit 1961739

Please sign in to comment.