Skip to content

Commit

Permalink
Cache terminal last CWD
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth Marut <[email protected]>
  • Loading branch information
kenneth-marut-work committed Jul 2, 2021
1 parent bc3b5e1 commit d39e50c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/task/src/browser/task-terminal-widget-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 };
}

Expand Down
3 changes: 3 additions & 0 deletions packages/terminal/src/browser/base/terminal-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions packages/terminal/src/browser/terminal-widget-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -292,12 +293,12 @@ export class TerminalWidgetImpl extends TerminalWidget implements StatefulWidget
};
}

get cwd(): Promise<URI> {
get cwd(): MaybePromise<URI> {
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());
Expand Down

0 comments on commit d39e50c

Please sign in to comment.