From d39e50c4b2b4503aa9b8ac676067e60cecde853c Mon Sep 17 00:00:00 2001 From: Kenneth Marut Date: Fri, 2 Jul 2021 16:54:31 +0200 Subject: [PATCH] Cache terminal last CWD Signed-off-by: Kenneth Marut --- packages/task/src/browser/task-terminal-widget-manager.ts | 4 ++++ packages/terminal/src/browser/base/terminal-widget.ts | 3 +++ packages/terminal/src/browser/terminal-widget-impl.ts | 7 ++++--- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/task/src/browser/task-terminal-widget-manager.ts b/packages/task/src/browser/task-terminal-widget-manager.ts index b7439eb3e372e..72d381e49e121 100644 --- a/packages/task/src/browser/task-terminal-widget-manager.ts +++ b/packages/task/src/browser/task-terminal-widget-manager.ts @@ -23,6 +23,7 @@ import { PanelKind, TaskConfiguration, TaskWatcher, TaskExitedEvent, TaskServer, import { ProcessTaskInfo } from '../common/process/task-protocol'; import { TaskDefinitionRegistry } from './task-definition-registry'; import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service'; +import URI from '@theia/core/lib/common/uri'; export interface TaskTerminalWidget extends TerminalWidget { readonly kind: 'task'; @@ -195,10 +196,13 @@ export class TaskTerminalWidgetManager { } // we are unable to find a terminal widget to run the task, or `taskPresentation === 'new'` + const lastCwd = new URI(taskConfig?.options.cwd); if (!reusableTerminalWidget) { const widget = await this.newTaskTerminal(factoryOptions); + widget.lastCwd = lastCwd; return { isNew: true, widget }; } + reusableTerminalWidget.lastCwd = lastCwd; return { isNew: false, widget: reusableTerminalWidget }; } diff --git a/packages/terminal/src/browser/base/terminal-widget.ts b/packages/terminal/src/browser/base/terminal-widget.ts index a0f95911bcc1c..3f4d50bde9e96 100644 --- a/packages/terminal/src/browser/base/terminal-widget.ts +++ b/packages/terminal/src/browser/base/terminal-widget.ts @@ -45,6 +45,9 @@ export abstract class TerminalWidget extends BaseWidget { abstract readonly dimensions: TerminalDimensions; + /** The last CWD assigned to the terminal, useful when attempting getCwdURI on a task terminal fails */ + lastCwd: URI | undefined; + /** * Start terminal and return terminal id. * @param id - terminal id. diff --git a/packages/terminal/src/browser/terminal-widget-impl.ts b/packages/terminal/src/browser/terminal-widget-impl.ts index 00227599b3864..d65cad33b3bef 100644 --- a/packages/terminal/src/browser/terminal-widget-impl.ts +++ b/packages/terminal/src/browser/terminal-widget-impl.ts @@ -17,7 +17,7 @@ import { Terminal, RendererType } from 'xterm'; import { FitAddon } from 'xterm-addon-fit'; import { inject, injectable, named, postConstruct } from '@theia/core/shared/inversify'; -import { ContributionProvider, Disposable, Event, Emitter, ILogger, DisposableCollection } from '@theia/core'; +import { ContributionProvider, Disposable, Event, Emitter, ILogger, DisposableCollection, MaybePromise } from '@theia/core'; import { Widget, Message, WebSocketConnectionProvider, StatefulWidget, isFirefox, MessageLoop, KeyCode } from '@theia/core/lib/browser'; import { isOSX } from '@theia/core/lib/common'; import { WorkspaceService } from '@theia/workspace/lib/browser'; @@ -61,6 +61,7 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget protected hoverMessage: HTMLDivElement; protected lastTouchEnd: TouchEvent | undefined; protected isAttachedCloseListener: boolean = false; + lastCwd: URI; @inject(WorkspaceService) protected readonly workspaceService: WorkspaceService; @inject(WebSocketConnectionProvider) protected readonly webSocketConnectionProvider: WebSocketConnectionProvider; @@ -292,12 +293,12 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget }; } - get cwd(): Promise { + get cwd(): MaybePromise { if (!IBaseTerminalServer.validateId(this.terminalId)) { return Promise.reject(new Error('terminal is not started')); } if (this.terminalService.getById(this.id)) { - return this.shellTerminalServer.getCwdURI(this.terminalId) + return this.lastCwd || this.shellTerminalServer.getCwdURI(this.terminalId) .then(cwdUrl => new URI(cwdUrl)); } return Promise.resolve(new URI());