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

Unsplit terminals #124039

Merged
merged 3 commits into from
May 17, 2021
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
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface ITerminalGroup {
resizePanes(relativeSizes: number[]): void;
setActiveInstanceByIndex(index: number): void;
attachToElement(element: HTMLElement): void;
removeInstance(instance: ITerminalInstance): void;
setVisible(visible: boolean): void;
layout(width: number, height: number): void;
addDisposable(disposable: IDisposable): void;
Expand Down Expand Up @@ -143,6 +144,7 @@ export interface ITerminalService {
getActiveOrCreateInstance(): ITerminalInstance;
splitInstance(instance: ITerminalInstance, shell?: IShellLaunchConfig, cwd?: string | URI): ITerminalInstance | null;
splitInstance(instance: ITerminalInstance, profile: ITerminalProfile): ITerminalInstance | null;
unsplitInstance(instance: ITerminalInstance): void;

/**
* Perform an action with the active terminal instance, if the terminal does
Expand Down
15 changes: 15 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,21 @@ export function registerTerminalActions() {
return undefined;
}
});
registerAction2(class extends Action2 {
constructor() {
super({
id: TerminalCommandId.Unsplit,
title: { value: localize('workbench.action.terminal.unsplit', "Unsplit Terminal"), original: 'Unsplit Terminal' },
f1: true,
category,
precondition: KEYBINDING_CONTEXT_TERMINAL_PROCESS_SUPPORTED
});
}
async run(accessor: ServicesAccessor) {
const terminalService = accessor.get(ITerminalService);
await terminalService.doWithActiveInstance(async t => terminalService.unsplitInstance(t));
}
});
registerAction2(class extends Action2 {
constructor() {
super({
Expand Down
40 changes: 28 additions & 12 deletions src/vs/workbench/contrib/terminal/browser/terminalGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { TERMINAL_VIEW_ID } from 'vs/workbench/contrib/terminal/common/terminal';
import { Event, Emitter } from 'vs/base/common/event';
import { IDisposable, Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IDisposable, Disposable, DisposableStore, dispose } from 'vs/base/common/lifecycle';
import { SplitView, Orientation, IView, Sizing } from 'vs/base/browser/ui/splitview/splitview';
import { IWorkbenchLayoutService, Parts, Position } from 'vs/workbench/services/layout/browser/layoutService';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand Down Expand Up @@ -233,6 +233,7 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
private _groupElement: HTMLElement | undefined;
private _panelPosition: Position = Position.BOTTOM;
private _terminalLocation: ViewContainerLocation = ViewContainerLocation.Panel;
private _instanceDisposables: Map<number, IDisposable[]> = new Map();

private _activeInstanceIndex: number;
private _isVisible: boolean = false;
Expand Down Expand Up @@ -317,18 +318,36 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
}

private _initInstanceListeners(instance: ITerminalInstance): void {
instance.addDisposable(instance.onDisposed(instance => this._onInstanceDisposed(instance)));
instance.addDisposable(instance.onFocused(instance => this._setActiveInstance(instance)));
this._instanceDisposables.set(instance.instanceId, [
instance.onDisposed(instance => this._onInstanceDisposed(instance)),
instance.onFocused(instance => this._setActiveInstance(instance))
]);
}

private _onInstanceDisposed(instance: ITerminalInstance): void {
// Get the index of the instance and remove it from the list
private _onInstanceDisposed(instance: ITerminalInstance) {
this._removeInstance(instance);
}

removeInstance(instance: ITerminalInstance): void {
this._removeInstance(instance);

// Dispose instance event listeners
const disposables = this._instanceDisposables.get(instance.instanceId);
if (disposables) {
dispose(disposables);
this._instanceDisposables.delete(instance.instanceId);
}
}

private _removeInstance(instance: ITerminalInstance) {
const index = this._terminalInstances.indexOf(instance);
const wasActiveInstance = instance === this.activeInstance;
if (index !== -1) {
this._terminalInstances.splice(index, 1);
if (index === -1) {
return;
}

const wasActiveInstance = instance === this.activeInstance;
this._terminalInstances.splice(index, 1);

// Adjust focus if the instance was active
if (wasActiveInstance && this._terminalInstances.length > 0) {
const newIndex = index < this._terminalInstances.length ? index : this._terminalInstances.length - 1;
Expand All @@ -342,10 +361,7 @@ export class TerminalGroup extends Disposable implements ITerminalGroup {
this._activeInstanceIndex--;
}

// Remove the instance from the split pane if it has been created
if (this._splitPaneContainer) {
this._splitPaneContainer.remove(instance);
}
this._splitPaneContainer?.remove(instance);

// Fire events and dispose group if it was the last instance
if (this._terminalInstances.length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,6 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {

override dispose(immediate?: boolean): void {
this._logService.trace(`terminalInstance#dispose (instanceId: ${this.instanceId})`);

dispose(this._linkManager);
this._linkManager = undefined;
dispose(this._commandTrackerAddon);
Expand Down
18 changes: 18 additions & 0 deletions src/vs/workbench/contrib/terminal/browser/terminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,23 @@ export class TerminalService implements ITerminalService {
return instance;
}

unsplitInstance(instance: ITerminalInstance): void {
const oldGroup = this.getGroupForInstance(instance);
if (!oldGroup || oldGroup.terminalInstances.length < 2) {
return;
}

oldGroup.removeInstance(instance);

const newGroup = this._instantiationService.createInstance(TerminalGroup, this._terminalContainer, instance);
newGroup.onPanelOrientationChanged((orientation) => this._onPanelOrientationChanged.fire(orientation));
this._terminalGroups.push(newGroup);

newGroup.addDisposable(newGroup.onDisposed(this._onGroupDisposed.fire, this._onGroupDisposed));
newGroup.addDisposable(newGroup.onInstancesChanged(this._onInstancesChanged.fire, this._onInstancesChanged));
this._onInstancesChanged.fire();
}

protected _initInstanceListeners(instance: ITerminalInstance): void {
instance.addDisposable(instance.onDisposed(this._onInstanceDisposed.fire, this._onInstanceDisposed));
instance.addDisposable(instance.onTitleChanged(this._onInstanceTitleChanged.fire, this._onInstanceTitleChanged));
Expand Down Expand Up @@ -969,6 +986,7 @@ export class TerminalService implements ITerminalService {
instance.shellLaunchConfig.hideFromUser = false;
const terminalGroup = this._instantiationService.createInstance(TerminalGroup, this._terminalContainer, instance);
this._terminalGroups.push(terminalGroup);
terminalGroup.onPanelOrientationChanged((orientation) => this._onPanelOrientationChanged.fire(orientation));
terminalGroup.addDisposable(terminalGroup.onDisposed(this._onGroupDisposed.fire, this._onGroupDisposed));
terminalGroup.addDisposable(terminalGroup.onInstancesChanged(this._onInstancesChanged.fire, this._onInstancesChanged));
if (this.terminalInstances.length === 1) {
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export const enum TerminalCommandId {
Split = 'workbench.action.terminal.split',
SplitInstance = 'workbench.action.terminal.splitInstance',
SplitInActiveWorkspace = 'workbench.action.terminal.splitInActiveWorkspace',
Unsplit = 'workbench.action.terminal.unsplit',
Relaunch = 'workbench.action.terminal.relaunch',
FocusPreviousPane = 'workbench.action.terminal.focusPreviousPane',
ShowTabs = 'workbench.action.terminal.showTabs',
Expand Down