Skip to content

Commit

Permalink
Convert and pass URI to shellIntegration
Browse files Browse the repository at this point in the history
Fixes #208368
  • Loading branch information
Tyriar committed Mar 28, 2024
1 parent 38c1ddc commit 275cb2f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
Expand Up @@ -9,6 +9,7 @@ import { ITerminalOutputMatcher, ITerminalOutputMatch } from 'vs/platform/termin
// Importing types is safe in any layer
// eslint-disable-next-line local/code-import-patterns
import type { IBuffer, IBufferLine, Terminal } from '@xterm/headless';
import { URI } from 'vs/base/common/uri';

export interface ITerminalCommandProperties {
command: string;
Expand Down Expand Up @@ -201,6 +202,13 @@ export class TerminalCommand implements ITerminalCommand {
getCommandRowCount(): number {
return getCommandRowCount(this);
}

getCwdResource(): URI | undefined {
if (!this.cwd) {
return undefined;
}
return URI.file(this.cwd);
}
}

export interface ICurrentPartialCommand {
Expand Down Expand Up @@ -263,6 +271,7 @@ export class PartialTerminalCommand implements ICurrentPartialCommand {
currentContinuationMarker?: IMarker;
continuations?: { marker: IMarker; end: number }[];

cwd?: string;
command?: string;

isTrusted?: boolean;
Expand Down
Expand Up @@ -157,6 +157,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe
}

setCwd(value: string) {
console.log('setCwd', value);
this._cwd = value;
}

Expand Down Expand Up @@ -281,6 +282,7 @@ export class CommandDetectionCapability extends Disposable implements ICommandDe

handleCommandStart(options?: IHandleCommandOptions): void {
this._handleCommandStartOptions = options;
this._currentCommand.cwd = this._cwd;
// Only update the column if the line has already been set
this._currentCommand.commandStartMarker = options?.marker || this._currentCommand.commandStartMarker;
if (this._currentCommand.commandStartMarker?.line === this._terminal.buffer.active.cursorY) {
Expand Down
16 changes: 10 additions & 6 deletions src/vs/workbench/api/browser/mainThreadTerminalShellIntegration.ts
Expand Up @@ -5,6 +5,7 @@

import { Event } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { TerminalCapability, type ITerminalCommand } from 'vs/platform/terminal/common/capabilities/capabilities';
import { ExtHostContext, MainContext, type ExtHostTerminalShellIntegrationShape, type MainThreadTerminalShellIntegrationShape } from 'vs/workbench/api/common/extHost.protocol';
import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
Expand Down Expand Up @@ -38,17 +39,14 @@ export class MainThreadTerminalShellIntegration extends Disposable implements Ma
const commandDetectionStartEvent = this._store.add(this._terminalService.createOnInstanceCapabilityEvent(TerminalCapability.CommandDetection, e => e.onCommandExecuted));
let currentCommand: ITerminalCommand | undefined;
this._store.add(commandDetectionStartEvent.event(e => {
// String paths are not exposed in the extension API
if (typeof e.data.cwd === 'string') {
return;
}
// Prevent duplicate events from being sent in case command detection double fires the
// event
if (e.data === currentCommand) {
return;
}
// String paths are not exposed in the extension API
currentCommand = e.data;
this._proxy.$shellExecutionStart(e.instance.instanceId, e.data.command, e.data.cwd);
this._proxy.$shellExecutionStart(e.instance.instanceId, e.data.command, this._convertCwdToUri(e.data.cwd));
}));

// onDidEndTerminalShellExecution
Expand All @@ -60,7 +58,9 @@ export class MainThreadTerminalShellIntegration extends Disposable implements Ma

// onDidChangeTerminalShellIntegration via cwd
const cwdChangeEvent = this._store.add(this._terminalService.createOnInstanceCapabilityEvent(TerminalCapability.CwdDetection, e => e.onDidChangeCwd));
this._store.add(cwdChangeEvent.event(e => this._proxy.$cwdChange(e.instance.instanceId, e.data)));
this._store.add(cwdChangeEvent.event(e => {
this._proxy.$cwdChange(e.instance.instanceId, this._convertCwdToUri(e.data));
}));

// Clean up after dispose
this._store.add(this._terminalService.onDidDisposeInstance(e => this._proxy.$closeTerminal(e.instanceId)));
Expand All @@ -75,4 +75,8 @@ export class MainThreadTerminalShellIntegration extends Disposable implements Ma
$executeCommand(terminalId: number, commandLine: string): void {
this._terminalService.getInstanceFromId(terminalId)?.runCommand(commandLine, true);
}

private _convertCwdToUri(cwd: string | undefined): URI | undefined {
return cwd ? URI.file(cwd) : undefined;
}
}
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Expand Up @@ -2271,7 +2271,7 @@ export interface ExtHostTerminalShellIntegrationShape {
$shellExecutionStart(instanceId: number, commandLine: string | undefined, cwd: UriComponents | undefined): void;
$shellExecutionEnd(instanceId: number, commandLine: string | undefined, exitCode: number | undefined): void;
$shellExecutionData(instanceId: number, data: string): void;
$cwdChange(instanceId: number, cwd: UriComponents | string): void;
$cwdChange(instanceId: number, cwd: UriComponents | undefined): void;
$closeTerminal(instanceId: number): void;
}

Expand Down
Expand Up @@ -120,7 +120,7 @@ export class ExtHostTerminalShellIntegration extends Disposable implements IExtH
this._activeShellIntegrations.get(instanceId)?.emitData(data);
}

public $cwdChange(instanceId: number, cwd: URI): void {
public $cwdChange(instanceId: number, cwd: URI | undefined): void {
this._activeShellIntegrations.get(instanceId)?.setCwd(cwd);
}

Expand Down Expand Up @@ -230,13 +230,13 @@ class InternalTerminalShellExecution {
const that = this;
this.value = {
get terminal(): vscode.Terminal {
return terminal;
return that.terminal;
},
get commandLine(): string | undefined {
return that._commandLine;
},
get cwd(): URI | undefined {
return cwd;
return that.cwd;
},
createDataStream(): AsyncIterable<string> {
return that._createDataStream();
Expand Down
Expand Up @@ -69,6 +69,7 @@ declare module 'vscode' {
}

export interface TerminalShellIntegration {
// TODO: Should this share TerminalShellIntegrationChangeEvent or have it's own TerminalShellIntegrationCwdChangeEvent?
/**
* The current working directory of the terminal. This will be a {@link Uri} if the path
* reported by the shell can reliably be mapped to the connected machine.
Expand Down

0 comments on commit 275cb2f

Please sign in to comment.