Skip to content

Commit

Permalink
[workspace] Store the most recently used workspace root in the user’s…
Browse files Browse the repository at this point in the history
… home directory and use it when the backend is started without an explicit root argument.

Signed-off-by: Sven Efftinge <sven.efftinge@typefox.io>
  • Loading branch information
svenefftinge committed Nov 10, 2017
1 parent 47e45c9 commit a195030
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/markers/src/browser/problem/problem-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { WorkspaceService } from '@theia/workspace/lib/browser';
@injectable()
export class ProblemWidget extends TreeWidget {

protected workspacePath: string;
protected workspacePath: string | undefined;

constructor(
@inject(ProblemManager) protected readonly problemManager: ProblemManager,
Expand All @@ -40,8 +40,8 @@ export class ProblemWidget extends TreeWidget {
this.title.closable = true;
this.addClass('theia-marker-container');

this.workspaceService.tryRoot.then(workspaceFileStat => {
this.workspacePath = workspaceFileStat ? workspaceFileStat.uri : '';
this.workspaceService.root.then(workspaceFileStat => {
this.workspacePath = workspaceFileStat ? workspaceFileStat.uri : undefined;
this.update();
});

Expand Down
55 changes: 55 additions & 0 deletions packages/workspace/src/node/default-workspace-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import * as path from 'path';
import * as yargs from 'yargs';
import * as fs from 'fs-extra';
import * as os from 'os';

import { injectable, inject } from "inversify";
import { FileUri } from '@theia/core/lib/node';
import { CliContribution } from '@theia/core/lib/node/cli';
Expand Down Expand Up @@ -51,6 +54,14 @@ export class DefaultWorkspaceServer implements WorkspaceServer {
@inject(WorkspaceCliContribution) protected readonly cliParams: WorkspaceCliContribution
) {
this.root = this.getRootURIFromCli();
this.root.then(async root => {
if (!root) {
const data = await this.readFromUserHome();
if (data && data.recentRoots) {
this.root = Promise.resolve(data.recentRoots[0]);
}
}
});
}

getRoot(): Promise<string | undefined> {
Expand All @@ -59,6 +70,9 @@ export class DefaultWorkspaceServer implements WorkspaceServer {

setRoot(uri: string): Promise<void> {
this.root = Promise.resolve(uri);
this.writeToUserHome({
recentRoots: [uri]
});
return Promise.resolve();
}

Expand All @@ -67,4 +81,45 @@ export class DefaultWorkspaceServer implements WorkspaceServer {
return arg !== undefined ? FileUri.create(arg).toString() : undefined;
}

/**
* Writes the given uri as the most recently used workspace root to the user's home directory.
* @param uri most recently used uri
*/
private async writeToUserHome(data: WorkspaceData): Promise<void> {
const file = this.getUserStoragePath();
if (!await fs.pathExists(file)) {
await fs.mkdirs(path.resolve(file, '..'));
}
await fs.writeJson(file, data);
}

/**
* Reads the most recently used workspace root from the user's home directory.
*/
private async readFromUserHome(): Promise<WorkspaceData | undefined> {
const file = this.getUserStoragePath();
if (await fs.pathExists(file)) {
const config = await fs.readJson(file);
if (WorkspaceData.is(config)) {
return config;
}
}
return undefined;
}

protected getUserStoragePath(): string {
return path.resolve(os.homedir(), '.theia', 'recentworkspace.json');
}

}

interface WorkspaceData {
recentRoots: string[];
}

namespace WorkspaceData {
// tslint:disable-next-line:no-any
export function is(data: any): data is WorkspaceData {
return data.recentRoots !== undefined;
}
}

0 comments on commit a195030

Please sign in to comment.