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

Workspace folders API #2141

Merged
merged 1 commit into from
Jun 22, 2018
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
5 changes: 5 additions & 0 deletions packages/plugin-ext/src/api/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export interface WindowStateExt {
$onWindowStateChanged(focus: boolean): void;
}

export interface WorkspaceExt {
$onWorkspaceFoldersChanged(event: theia.WorkspaceFoldersChangeEvent): void;
}

export enum EditorPosition {
ONE = 0,
TWO = 1,
Expand Down Expand Up @@ -423,6 +427,7 @@ export const MAIN_RPC_CONTEXT = {
COMMAND_REGISTRY_EXT: createProxyIdentifier<CommandRegistryExt>('CommandRegistryExt'),
QUICK_OPEN_EXT: createProxyIdentifier<QuickOpenExt>('QuickOpenExt'),
WINDOW_STATE_EXT: createProxyIdentifier<WindowStateExt>('WindowStateExt'),
WORKSPACE_EXT: createProxyIdentifier<WorkspaceExt>('WorkspaceExt'),
TEXT_EDITORS_EXT: createProxyIdentifier<TextEditorsExt>('TextEditorsExt'),
EDITORS_AND_DOCUMENTS_EXT: createProxyIdentifier<EditorsAndDocumentsExt>('EditorsAndDocumentsExt'),
DOCUMENTS_EXT: createProxyIdentifier<DocumentsExt>('DocumentsExt'),
Expand Down
11 changes: 6 additions & 5 deletions packages/plugin-ext/src/main/browser/main-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { interfaces } from 'inversify';
import { CommandRegistryMainImpl } from './command-registry-main';
import { PreferenceRegistryMainImpl } from './preference-registry-main';
Expand All @@ -12,10 +13,12 @@ import { RPCProtocol } from '../../api/rpc-protocol';
import { PLUGIN_RPC_CONTEXT } from '../../api/plugin-api';
import { MessageRegistryMainImpl } from './message-registry-main';
import { WindowStateMain } from './window-state-main';
import { WorkspaceMain } from './workspace-main';
import { StatusBarMessageRegistryMainImpl } from './status-bar-message-registry-main';
import { EnvMainImpl } from './env-main';
import { EditorsAndDocumentsMain } from './editors-and-documents-main';
import {OutputChannelRegistryMainImpl} from "./output-channel-registry-main";
import { OutputChannelRegistryMainImpl } from "./output-channel-registry-main";
import { WorkspaceService } from '@theia/workspace/lib/browser';

export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container): void {
const commandRegistryMain = new CommandRegistryMainImpl(rpc, container);
Expand All @@ -30,11 +33,9 @@ export function setUpPluginApi(rpc: RPCProtocol, container: interfaces.Container
const preferenceRegistryMain = new PreferenceRegistryMainImpl(rpc, container);
rpc.set(PLUGIN_RPC_CONTEXT.PREFERENCE_REGISTRY_MAIN, preferenceRegistryMain);

// tslint:disable-next-line:no-unused-variable
// @ts-ignore
const windowStateMain = new WindowStateMain(rpc);

/* tslint:disable */
new WindowStateMain(rpc);
new WorkspaceMain(rpc, container.get(WorkspaceService));
new EditorsAndDocumentsMain(rpc, container);
/* tslint:enable */

Expand Down
48 changes: 48 additions & 0 deletions packages/plugin-ext/src/main/browser/workspace-main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { WorkspaceExt, MAIN_RPC_CONTEXT } from "../../api/plugin-api";
import { RPCProtocol } from "../../api/rpc-protocol";
import { WorkspaceService } from "@theia/workspace/lib/browser";
import Uri from 'vscode-uri';
import { WorkspaceFoldersChangeEvent, WorkspaceFolder } from "@theia/plugin";
import { Path } from "@theia/core/lib/common/path";

export class WorkspaceMain {

private proxy: WorkspaceExt;

private workspaceRoot: Uri | undefined;

constructor(rpc: RPCProtocol, workspaceService: WorkspaceService) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.WORKSPACE_EXT);

workspaceService.root.then((root) => {
if (root) {
this.workspaceRoot = Uri.parse(root.uri);
const workspacePath = new Path(this.workspaceRoot.path);

const folder: WorkspaceFolder = {
uri: this.workspaceRoot,
name: workspacePath.base,
index: 0
} as WorkspaceFolder;

this.proxy.$onWorkspaceFoldersChanged({
added: [folder],
removed: []
} as WorkspaceFoldersChangeEvent);
} else {
this.proxy.$onWorkspaceFoldersChanged({
added: [],
removed: []
} as WorkspaceFoldersChangeEvent);
}
});
}

}
14 changes: 12 additions & 2 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getPluginId } from '../common/plugin-protocol';
import { MessageRegistryExt } from './message-registry';
import { StatusBarMessageRegistryExt } from './status-bar-message-registry';
import { WindowStateExtImpl } from './window-state';
import { WorkspaceExtImpl } from './workspace';
import { EnvExtImpl } from './env';
import { QueryParameters } from '../common/env';
import {
Expand Down Expand Up @@ -52,6 +53,7 @@ export function createAPI(rpc: RPCProtocol): typeof theia {
const editorsAndDocuments = rpc.set(MAIN_RPC_CONTEXT.EDITORS_AND_DOCUMENTS_EXT, new EditorsAndDocumentsExtImpl(rpc));
const editors = rpc.set(MAIN_RPC_CONTEXT.TEXT_EDITORS_EXT, new TextEditorsExtImpl(rpc, editorsAndDocuments));
const documents = rpc.set(MAIN_RPC_CONTEXT.DOCUMENTS_EXT, new DocumentsExtImpl(rpc, editorsAndDocuments));
const workspaceExt = rpc.set(MAIN_RPC_CONTEXT.WORKSPACE_EXT, new WorkspaceExtImpl());
const statusBarMessageRegistryExt = new StatusBarMessageRegistryExt(rpc);
const envExt = rpc.set(MAIN_RPC_CONTEXT.ENV_EXT, new EnvExtImpl(rpc));
const preferenceRegistryExt = rpc.set(MAIN_RPC_CONTEXT.PREFERENCE_REGISTRY_EXT, new PreferenceRegistryExtImpl(rpc));
Expand Down Expand Up @@ -137,7 +139,7 @@ export function createAPI(rpc: RPCProtocol): typeof theia {
return statusBarMessageRegistryExt.createStatusBarItem(alignment, priority);
},
createOutputChannel(name: string): theia.OutputChannel {
return outputChannelRegistryExt.createOutputChannel(name);
return outputChannelRegistryExt.createOutputChannel(name);
},

get state(): theia.WindowState {
Expand All @@ -152,6 +154,15 @@ export function createAPI(rpc: RPCProtocol): typeof theia {
};

const workspace: typeof theia.workspace = {
get workspaceFolders(): theia.WorkspaceFolder[] | undefined {
return workspaceExt.workspaceFolders;
},
get name(): string | undefined {
return workspaceExt.name;
},
onDidChangeWorkspaceFolders(listener, thisArg?, disposables?): theia.Disposable {
return workspaceExt.onDidChangeWorkspaceFolders(listener, thisArg, disposables);
},
get textDocuments() {
return documents.getAllDocumentData().map(data => data.document);
},
Expand Down Expand Up @@ -231,7 +242,6 @@ export function createAPI(rpc: RPCProtocol): typeof theia {
OverviewRulerLane,
ConfigurationTarget,
};

}

// tslint:disable-next-line:no-any
Expand Down
40 changes: 40 additions & 0 deletions packages/plugin-ext/src/plugin/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

import { WorkspaceFolder, WorkspaceFoldersChangeEvent } from "@theia/plugin";
import { Event, Emitter } from "@theia/core/lib/common/event";
import { WorkspaceExt } from "../api/plugin-api";
import { Path } from "@theia/core/lib/common/path";

export class WorkspaceExtImpl implements WorkspaceExt {

private workspaceFoldersChangedEmitter = new Emitter<WorkspaceFoldersChangeEvent>();
public readonly onDidChangeWorkspaceFolders: Event<WorkspaceFoldersChangeEvent> = this.workspaceFoldersChangedEmitter.event;

private folders: WorkspaceFolder[] | undefined;

constructor() {
}

get workspaceFolders(): WorkspaceFolder[] | undefined {
return this.folders;
}

get name(): string | undefined {
if (this.workspaceFolders) {
return new Path(this.workspaceFolders[0].uri.path).base;
}

return undefined;
}

$onWorkspaceFoldersChanged(event: WorkspaceFoldersChangeEvent): void {
this.folders = event.added;
this.workspaceFoldersChangedEmitter.fire(event);
}

}
61 changes: 61 additions & 0 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,46 @@ declare module '@theia/plugin' {
affectsConfiguration(section: string, resource?: Uri): boolean;
}

/**
* An event describing a change to the set of [workspace folders](#workspace.workspaceFolders).
*/
export interface WorkspaceFoldersChangeEvent {
/**
* Added workspace folders.
*/
readonly added: WorkspaceFolder[];

/**
* Removed workspace folders.
*/
readonly removed: WorkspaceFolder[];
}

/**
* A workspace folder is one of potentially many roots opened by the editor. All workspace folders
* are equal which means there is no notion of an active or master workspace folder.
*/
export interface WorkspaceFolder {
/**
* The associated uri for this workspace folder.
*
* *Note:* The [Uri](#Uri)-type was intentionally chosen such that future releases of the editor can support
* workspace folders that are not stored on the local disk, e.g. `ftp://server/workspaces/foo`.
*/
readonly uri: Uri;

/**
* The name of this workspace folder. Defaults to
* the basename of its [uri-path](#Uri.path)
*/
readonly name: string;

/**
* The ordinal number of this workspace folder.
*/
readonly index: number;
}

/**
* Namespace for dealing with the current workspace. A workspace is the representation
* of the folder that has been opened. There is no workspace when just a file but not a
Expand All @@ -2101,6 +2141,27 @@ declare module '@theia/plugin' {
* the editor-process so that they should be always used instead of nodejs-equivalents.
*/
export namespace workspace {
/**
* List of workspace folders or `undefined` when no folder is open.
* *Note* that the first entry corresponds to the value of `rootPath`.
*
* @readonly
*/
export let workspaceFolders: WorkspaceFolder[] | undefined;

/**
* The name of the workspace. `undefined` when no folder
* has been opened.
*
* @readonly
*/
export let name: string | undefined;

/**
* An event that is emitted when a workspace folder is added or removed.
*/
export const onDidChangeWorkspaceFolders: Event<WorkspaceFoldersChangeEvent>;

/**
* All text documents currently known to the system.
*
Expand Down