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

[ls] implement multiple workspace folders in MonacoWorkspace #7182

Merged
merged 1 commit into from
Feb 21, 2020
Merged
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
67 changes: 56 additions & 11 deletions packages/monaco/src/browser/monaco-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { injectable, inject, postConstruct } from 'inversify';
import { ProtocolToMonacoConverter, MonacoToProtocolConverter, testGlob } from 'monaco-languageclient';
import URI from '@theia/core/lib/common/uri';
import { DisposableCollection } from '@theia/core/lib/common';
import { FileSystem, } from '@theia/filesystem/lib/common';
import { FileSystem, FileStat, } from '@theia/filesystem/lib/common';
import { FileChangeType, FileSystemWatcher } from '@theia/filesystem/lib/browser';
import { WorkspaceService } from '@theia/workspace/lib/browser';
import { EditorManager, EditorOpenerOptions } from '@theia/editor/lib/browser';
Expand Down Expand Up @@ -118,6 +118,17 @@ export namespace EditsByEditor {

export type Edit = TextEdits | ResourceEdit;

export interface WorkspaceFoldersChangeEvent {
readonly added: WorkspaceFolder[];
readonly removed: WorkspaceFolder[];
}

export interface WorkspaceFolder {
readonly uri: Uri;
readonly name: string;
readonly index: number;
}

@injectable()
export class MonacoWorkspace implements lang.Workspace {

Expand Down Expand Up @@ -148,6 +159,9 @@ export class MonacoWorkspace implements lang.Workspace {
protected readonly onDidSaveTextDocumentEmitter = new Emitter<MonacoEditorModel>();
readonly onDidSaveTextDocument = this.onDidSaveTextDocumentEmitter.event;

protected readonly onDidChangeWorkspaceFoldersEmitter = new Emitter<WorkspaceFoldersChangeEvent>();
readonly onDidChangeWorkspaceFolders = this.onDidChangeWorkspaceFoldersEmitter.event;

@inject(FileSystem)
protected readonly fileSystem: FileSystem;

Expand Down Expand Up @@ -175,14 +189,19 @@ export class MonacoWorkspace implements lang.Workspace {
@inject(ProblemManager)
protected readonly problems: ProblemManager;

protected _workspaceFolders: WorkspaceFolder[] = [];
get workspaceFolders(): WorkspaceFolder[] {
return this._workspaceFolders;
}

@postConstruct()
protected init(): void {
this.workspaceService.roots.then(roots => {
const rootStat = roots[0];
if (rootStat) {
this._rootUri = rootStat.uri;
this.resolveReady();
}
protected async init(): Promise<void> {
const roots = await this.workspaceService.roots;
this.updateWorkspaceFolders(roots);
this.resolveReady();

JanKoehnlein marked this conversation as resolved.
Show resolved Hide resolved
this.workspaceService.onWorkspaceChanged(async newRootDirs => {
JanKoehnlein marked this conversation as resolved.
Show resolved Hide resolved
JanKoehnlein marked this conversation as resolved.
Show resolved Hide resolved
this.updateWorkspaceFolders(newRootDirs);
});

for (const model of this.textModelService.models) {
Expand All @@ -191,13 +210,39 @@ export class MonacoWorkspace implements lang.Workspace {
this.textModelService.onDidCreate(model => this.fireDidOpen(model));
}

protected _rootUri: string | null = null;
protected updateWorkspaceFolders(newRootDirs: FileStat[]): void {
const oldWorkspaceUris = this.workspaceFolders.map(folder => folder.uri.toString());
const newWorkspaceUris = newRootDirs.map(folder => folder.uri);
const added = newWorkspaceUris.filter(uri => oldWorkspaceUris.indexOf(uri) < 0).map((dir, index) => this.toWorkspaceFolder(dir, index));
const removed = oldWorkspaceUris.filter(uri => newWorkspaceUris.indexOf(uri) < 0).map((dir, index) => this.toWorkspaceFolder(dir, index));
this._workspaceFolders = newWorkspaceUris.map(this.toWorkspaceFolder);
this.onDidChangeWorkspaceFoldersEmitter.fire({ added, removed });
}

protected toWorkspaceFolder(uriString: string, index: number): WorkspaceFolder {
const uri = Uri.parse(uriString);
const path = uri.path;
return {
uri,
name: path.substring(path.lastIndexOf('/') + 1),
index
};
}

get rootUri(): string | null {
return this._rootUri;
if (this._workspaceFolders.length > 0) {
return this._workspaceFolders[0].uri.toString();
} else {
return null;
}
}

get rootPath(): string | null {
return this._rootUri && new URI(this._rootUri).path.toString();
if (this._workspaceFolders.length > 0) {
return new URI(this._workspaceFolders[0].uri).path.toString();
} else {
return null;
}
}

get textDocuments(): MonacoEditorModel[] {
Expand Down