Skip to content

Commit

Permalink
feat: DBGp stream redirect support
Browse files Browse the repository at this point in the history
  • Loading branch information
zobo committed Jun 30, 2024
1 parent 5c37f12 commit 654ed71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/phpDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ class PhpDebugSession extends vscode.DebugSession {
throw new Error(`Error applying xdebugSettings: ${String(error instanceof Error ? error.message : error)}`)
}

if (this._args.externalConsole) {
await connection.sendStdout('1')
connection.on('stream', (stream: xdebug.Stream) =>
this.sendEvent(new vscode.OutputEvent(stream.value, 'stdout'))
)
}

this.sendEvent(new vscode.ThreadEvent('started', connection.id))

// wait for all breakpoints
Expand Down
32 changes: 32 additions & 0 deletions src/xdebugConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ export class UserNotify extends Notify {
}
}

export class Stream {
/** Type of stream */
type: string
/** Data of the stream */
value: string

/** Constructs a stream object from an XML node from a Xdebug response */
constructor(document: XMLDocument) {
this.type = document.documentElement.getAttribute('type')!
const encoding = document.documentElement.getAttribute('encoding')
if (encoding) {
this.value = iconv.encode(document.documentElement.textContent!, encoding).toString()
} else {
this.value = document.documentElement.textContent!
}
}
}

export type BreakpointType = 'line' | 'call' | 'return' | 'exception' | 'conditional' | 'watch'
export type BreakpointState = 'enabled' | 'disabled'
export type BreakpointResolved = 'resolved' | 'unresolved'
Expand Down Expand Up @@ -745,6 +763,7 @@ export declare interface Connection extends DbgpConnection {
on(event: 'log', listener: (text: string) => void): this
on(event: 'notify_user', listener: (notify: UserNotify) => void): this
on(event: 'notify_breakpoint_resolved', listener: (notify: BreakpointResolvedNotify) => void): this
on(event: 'stream', listener: (stream: Stream) => void): this
}

/**
Expand Down Expand Up @@ -809,6 +828,9 @@ export class Connection extends DbgpConnection {
} else if (response.documentElement.nodeName === 'notify') {
const n = Notify.fromXml(response, this)
this.emit('notify_' + n.name, n)
} else if (response.documentElement.nodeName === 'stream') {
const s = new Stream(response)
this.emit('stream', s)
} else {
const transactionId = parseInt(response.documentElement.getAttribute('transaction_id')!)
if (this._pendingCommands.has(transactionId)) {
Expand Down Expand Up @@ -1109,4 +1131,14 @@ export class Connection extends DbgpConnection {
public async sendEvalCommand(expression: string): Promise<EvalResponse> {
return new EvalResponse(await this._enqueueCommand('eval', undefined, expression), this)
}

// ------------------------------ stream ----------------------------------------

public async sendStdout(mode: '0' | '1' | '2'): Promise<Response> {
return new Response(await this._enqueueCommand('stdout', `-c ${mode}`), this)
}

public async sendStderr(mode: '0' | '1' | '2'): Promise<Response> {
return new Response(await this._enqueueCommand('stdout', `-c ${mode}`), this)
}
}

0 comments on commit 654ed71

Please sign in to comment.