Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vscode] Support TerminalExitReason #12293

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ import {
InputBoxValidationSeverity,
TerminalLink,
TerminalLocation,
TerminalExitReason,
TerminalProfile,
InlayHint,
InlayHintKind,
Expand Down Expand Up @@ -1306,7 +1307,8 @@ export function createAPIFactory(
TabInputNotebookDiff: NotebookDiffEditorTabInput,
TabInputWebview: WebviewEditorTabInput,
TabInputTerminal: TerminalEditorTabInput,
TerminalLocation
TerminalLocation,
TerminalExitReason
};
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-ext/src/plugin/terminal-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { RPCProtocol } from '../common/rpc-protocol';
import { Event, Emitter } from '@theia/core/lib/common/event';
import { Deferred } from '@theia/core/lib/common/promise-util';
import * as theia from '@theia/plugin';
import { Disposable, EnvironmentVariableMutatorType, ThemeIcon } from './types-impl';
import { Disposable, EnvironmentVariableMutatorType, TerminalExitReason, ThemeIcon } from './types-impl';
import { SerializableEnvironmentVariableCollection } from '@theia/terminal/lib/common/base-terminal-protocol';
import { ProvidedTerminalLink } from '../common/plugin-api-rpc-model';
import { ThemeIcon as MonacoThemeIcon } from '@theia/monaco-editor-core/esm/vs/platform/theme/common/themeService';
Expand Down Expand Up @@ -198,7 +198,7 @@ export class TerminalServiceExtImpl implements TerminalServiceExt {
$terminalClosed(id: string, exitStatus: theia.TerminalExitStatus | undefined): void {
const terminal = this._terminals.get(id);
if (terminal) {
terminal.exitStatus = exitStatus ?? { code: undefined };
terminal.exitStatus = exitStatus ?? { code: undefined, reason: TerminalExitReason.Unknown };
this.onDidCloseTerminalEmitter.fire(terminal);
this._terminals.delete(id);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-ext/src/plugin/types-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,14 @@ export class TerminalProfile {
}
}

export enum TerminalExitReason {
Unknown = 0,
Shutdown = 1,
Process = 2,
User = 3,
Extension = 4,
}

@es5ClassCompat
export class FileDecoration {

Expand Down
35 changes: 35 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3058,6 +3058,41 @@ export module '@theia/plugin' {
* without providing an exit code.
*/
readonly code: number | undefined;

/**
* The reason that triggered the exit of a terminal.
*/
readonly reason: TerminalExitReason;
}

/**
* Terminal exit reason kind.
*/
export enum TerminalExitReason {
/**
* Unknown reason.
*/
Unknown = 0,

/**
* The window closed/reloaded.
*/
Shutdown = 1,

/**
* The shell process exited.
*/
Process = 2,

/**
* The user closed the terminal.
*/
User = 3,

/**
* An extension disposed the terminal.
*/
Extension = 4
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/terminal/src/browser/base/terminal-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { Event, ViewColumn } from '@theia/core';
import { BaseWidget } from '@theia/core/lib/browser';
import { CommandLineOptions } from '@theia/process/lib/common/shell-command-builder';
import { TerminalSearchWidget } from '../search/terminal-search-widget';
import { TerminalProcessInfo } from '../../common/base-terminal-protocol';
import { TerminalProcessInfo, TerminalExitReason } from '../../common/base-terminal-protocol';
import URI from '@theia/core/lib/common/uri';

export interface TerminalDimensions {
Expand All @@ -28,6 +28,7 @@ export interface TerminalDimensions {

export interface TerminalExitStatus {
readonly code: number | undefined;
readonly reason: TerminalExitReason;
}

export type TerminalLocationOptions = TerminalLocation | TerminalEditorLocation | TerminalSplitLocation;
Expand Down
25 changes: 19 additions & 6 deletions packages/terminal/src/browser/terminal-widget-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ import { isOSX } from '@theia/core/lib/common';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { ShellTerminalServerProxy, IShellTerminalPreferences } from '../common/shell-terminal-protocol';
import { terminalsPath } from '../common/terminal-protocol';
import { IBaseTerminalServer, TerminalProcessInfo } from '../common/base-terminal-protocol';
import { IBaseTerminalServer, TerminalProcessInfo, TerminalExitReason } from '../common/base-terminal-protocol';
import { TerminalWatcher } from '../common/terminal-watcher';
import { TerminalWidgetOptions, TerminalWidget, TerminalDimensions, TerminalExitStatus, TerminalLocationOptions, TerminalLocation } from './base/terminal-widget';
import {
TerminalWidgetOptions, TerminalWidget, TerminalDimensions, TerminalExitStatus, TerminalLocationOptions,
TerminalLocation
} from './base/terminal-widget';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { TerminalPreferences, TerminalRendererType, isTerminalRendererType, DEFAULT_TERMINAL_RENDERER_TYPE, CursorStyle } from './terminal-preferences';
import URI from '@theia/core/lib/common/uri';
Expand Down Expand Up @@ -200,14 +203,18 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget

this.toDispose.push(this.terminalWatcher.onTerminalError(({ terminalId, error }) => {
if (terminalId === this.terminalId) {
this.exitStatus = { code: undefined };
this.exitStatus = { code: undefined, reason: TerminalExitReason.Process };
this.dispose();
this.logger.error(`The terminal process terminated. Cause: ${error}`);
}
}));
this.toDispose.push(this.terminalWatcher.onTerminalExit(({ terminalId, code }) => {
this.toDispose.push(this.terminalWatcher.onTerminalExit(({ terminalId, code, reason }) => {
if (terminalId === this.terminalId) {
this.exitStatus = { code };
if (reason) {
this.exitStatus = { code, reason };
} else {
this.exitStatus = { code, reason: TerminalExitReason.Process };
}
this.dispose();
}
}));
Expand Down Expand Up @@ -362,6 +369,11 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
return this.searchBox;
}

protected override onCloseRequest(msg: Message): void {
this.exitStatus = { code: undefined, reason: TerminalExitReason.User };
super.onCloseRequest(msg);
}

get dimensions(): TerminalDimensions {
return {
cols: this.term.cols,
Expand Down Expand Up @@ -721,7 +733,8 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
// Close the backend terminal only when explicitly closing the terminal
// a refresh for example won't close it.
this.shellTerminalServer.close(this.terminalId);
this.exitStatus = { code: undefined };
// Exit status is set when terminal is closed by user or by process, so most likely an extension closed it.
this.exitStatus = { code: undefined, reason: TerminalExitReason.Extension };
}
if (this.exitStatus) {
this.onTermDidClose.fire(this);
Expand Down
11 changes: 10 additions & 1 deletion packages/terminal/src/common/base-terminal-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ export namespace IBaseTerminalServer {
export interface IBaseTerminalExitEvent {
terminalId: number;

// Exactly one of code and signal will be set.
// Either code and reason will be set or signal.
code?: number;
reason?: TerminalExitReason;
signal?: string;
}

export enum TerminalExitReason {
Unknown = 0,
Shutdown = 1,
Process = 2,
User = 3,
Extension = 4,
}

export interface IBaseTerminalErrorEvent {
terminalId: number;
error: Error
Expand Down
4 changes: 3 additions & 1 deletion packages/terminal/src/node/base-terminal-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
ExtensionOwnedEnvironmentVariableMutator,
EnvironmentVariableMutatorType,
EnvironmentVariableCollectionWithPersistence,
SerializableExtensionEnvironmentVariableCollection
SerializableExtensionEnvironmentVariableCollection,
TerminalExitReason
} from '../common/base-terminal-protocol';
import { TerminalProcess, ProcessManager, TaskTerminalProcess } from '@theia/process/lib/node';
import { ShellProcess } from './shell-process';
Expand Down Expand Up @@ -160,6 +161,7 @@ export abstract class BaseTerminalServer implements IBaseTerminalServer {
this.client.onTerminalExitChanged({
'terminalId': term.id,
'code': event.code,
'reason': TerminalExitReason.Process,
'signal': event.signal
});
}
Expand Down