Skip to content

Commit

Permalink
Merge branch 'master' of github.com:theia-ide/theia into theia-4103
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Feb 7, 2019
2 parents f1839df + 8192a13 commit d991632
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
********************************************************************************/

import { AbstractGenerator } from './abstract-generator';
import { existsSync, readFileSync } from 'fs';

export class FrontendGenerator extends AbstractGenerator {

Expand All @@ -27,6 +28,20 @@ export class FrontendGenerator extends AbstractGenerator {
}
}

protected compileIndexPreload(frontendModules: Map<string, string>): string {
const template = this.pck.props.generator.config.preloadTemplate;
if (!template) {
return '';
}

// Support path to html file
if (existsSync(template)) {
return readFileSync(template).toString();
}

return template;
}

protected compileIndexHtml(frontendModules: Map<string, string>): string {
return `<!DOCTYPE html>
<html>
Expand All @@ -36,7 +51,7 @@ export class FrontendGenerator extends AbstractGenerator {
</head>
<body>
<div class="theia-preload"></div>
<div class="theia-preload">${this.compileIndexPreload(frontendModules)}</div>
</body>
</html>`;
Expand Down
21 changes: 21 additions & 0 deletions dev-packages/application-package/src/application-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export interface ApplicationProps extends NpmRegistryProps {
*/
readonly backend: Readonly<{ config: BackendApplicationConfig }>;

/**
* Generator specific properties.
*/
readonly generator: Readonly<{ config: GeneratorConfig }>;
}
export namespace ApplicationProps {

Expand All @@ -72,6 +76,11 @@ export namespace ApplicationProps {
config: {
applicationName: 'Theia'
}
},
generator: {
config: {
preloadTemplate: ''
}
}
};

Expand Down Expand Up @@ -115,3 +124,15 @@ export interface BackendApplicationConfig extends ApplicationConfig {
readonly startupTimeout?: number;

}

/**
* Configuration for the generator.
*/
export interface GeneratorConfig {

/**
* Template to use for extra preload content markup (file path or HTML)
*/
readonly preloadTemplate: string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class CommandRegistryMainImpl implements CommandRegistryMain {
}

$getCommands(): PromiseLike<string[]> {
throw new Error('Method not implemented.');
return Promise.resolve(this.delegate.commandIds);
}

}
7 changes: 4 additions & 3 deletions packages/plugin-ext/src/main/browser/documents-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { EditorModelService } from './text-editor-model-service';
import { createUntitledResource } from './editor/untitled-resource';
import { EditorManager } from '@theia/editor/lib/browser';
import URI from '@theia/core/lib/common/uri';
import CodeURI from 'vscode-uri';
import { ApplicationShell, OpenerOptions, Saveable } from '@theia/core/lib/browser';
import { TextDocumentShowOptions } from '../../api/model';
import { Range } from 'vscode-languageserver-types';
Expand Down Expand Up @@ -154,16 +155,16 @@ export class DocumentsMainImpl implements DocumentsMain {
widgetOptions
};
}
const uriArg = new URI(uri.external!);
const uriArg = new URI(CodeURI.revive(uri));
const opener = await this.openerService.getOpener(uriArg, openerOptions);
opener.open(uriArg, openerOptions);
await opener.open(uriArg, openerOptions);
} catch (err) {
throw new Error(err);
}
}

async $trySaveDocument(uri: UriComponents): Promise<boolean> {
const widget = await this.editorManger.getByUri(new URI(uri.external!));
const widget = await this.editorManger.getByUri(new URI(CodeURI.revive(uri)));
if (widget) {
await Saveable.save(widget);
return true;
Expand Down
19 changes: 15 additions & 4 deletions packages/plugin-ext/src/plugin/command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export class CommandRegistryImpl implements CommandRegistryExt {
}
}

registerCommand(command: theia.Command, handler?: Handler): Disposable {
// tslint:disable-next-line:no-any
registerCommand(command: theia.Command, handler?: Handler, thisArg?: any): Disposable {
if (this.commands.has(command.id)) {
throw new Error(`Command ${command.id} already exist`);
}
Expand All @@ -66,7 +67,7 @@ export class CommandRegistryImpl implements CommandRegistryExt {

const toDispose: Disposable[] = [];
if (handler) {
toDispose.push(this.registerHandler(command.id, handler));
toDispose.push(this.registerHandler(command.id, handler, thisArg));
}
toDispose.push(Disposable.create(() => {
this.commands.delete(command.id);
Expand All @@ -75,12 +76,14 @@ export class CommandRegistryImpl implements CommandRegistryExt {
return Disposable.from(...toDispose);
}

registerHandler(commandId: string, handler: Handler): Disposable {
// tslint:disable-next-line:no-any
registerHandler(commandId: string, handler: Handler, thisArg?: any): Disposable {
if (this.handlers.has(commandId)) {
throw new Error(`Command "${commandId}" already has handler`);
}
this.proxy.$registerHandler(commandId);
this.handlers.set(commandId, handler);
// tslint:disable-next-line:no-any
this.handlers.set(commandId, (...args: any[]) => handler.apply(thisArg, args));
return Disposable.create(() => {
this.handlers.delete(commandId);
this.proxy.$unregisterHandler(commandId);
Expand Down Expand Up @@ -134,6 +137,14 @@ export class CommandRegistryImpl implements CommandRegistryExt {
}
return commands.executeCommand(actualCmd.command ? actualCmd.command : actualCmd.id, ...(actualCmd.arguments || []));
}

async getCommands(filterUnderscoreCommands: boolean = false): Promise<string[]> {
const result = await this.proxy.$getCommands();
if (filterUnderscoreCommands) {
return result.filter(command => command[0] !== '_');
}
return result;
}
}

/** Converter between internal and api commands. */
Expand Down
39 changes: 32 additions & 7 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/

// tslint:disable:no-any

import * as theia from '@theia/plugin';
import { CommandRegistryImpl } from './command-registry';
import { Emitter } from '@theia/core/lib/common/event';
Expand Down Expand Up @@ -153,23 +156,42 @@ export function createAPIFactory(
return function (plugin: InternalPlugin): typeof theia {
const commands: typeof theia.commands = {
// tslint:disable-next-line:no-any
registerCommand(command: theia.Command, handler?: <T>(...args: any[]) => T | Thenable<T>): Disposable {
return commandRegistry.registerCommand(command, handler);
registerCommand(command: theia.Command, handler?: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any): Disposable {
return commandRegistry.registerCommand(command, handler, thisArg);
},
// tslint:disable-next-line:no-any
executeCommand<T>(commandId: string, ...args: any[]): PromiseLike<T | undefined> {
return commandRegistry.executeCommand<T>(commandId, ...args);
},
// tslint:disable-next-line:no-any
registerTextEditorCommand(command: theia.Command, callback: (textEditor: theia.TextEditor, edit: theia.TextEditorEdit, ...arg: any[]) => void): Disposable {
throw new Error('Function registerTextEditorCommand is not implemented');
registerTextEditorCommand(command: string, handler: (textEditor: theia.TextEditor, edit: theia.TextEditorEdit, ...arg: any[]) => void, thisArg?: any): Disposable {
return commandRegistry.registerCommand({ id: command }, (...args: any[]): any => {
const activeTextEditor = editors.getActiveEditor();
if (!activeTextEditor) {
console.warn('Cannot execute ' + command + ' because there is no active text editor.');
return undefined;
}

return activeTextEditor.edit((edit: theia.TextEditorEdit) => {
args.unshift(activeTextEditor, edit);
handler.apply(thisArg, args);
}).then(result => {
if (!result) {
console.warn('Edits from command ' + command + ' were not applied.');
}
}, err => {
console.warn('An error occurred while running command ' + command, err);
});
});
},
// tslint:disable-next-line:no-any
registerHandler(commandId: string, handler: (...args: any[]) => any): Disposable {
return commandRegistry.registerHandler(commandId, handler);
registerHandler(commandId: string, handler: (...args: any[]) => any, thisArg?: any): Disposable {
return commandRegistry.registerHandler(commandId, handler, thisArg);
},
getKeyBinding(commandId: string): PromiseLike<theia.CommandKeyBinding[] | undefined> {
return commandRegistry.getKeyBinding(commandId);
},
getCommands(filterInternal: boolean = false): PromiseLike<string[]> {
return commandRegistry.getCommands(filterInternal);
}
};

Expand Down Expand Up @@ -334,6 +356,9 @@ export function createAPIFactory(
};

const workspace: typeof theia.workspace = {
get rootPath(): string | undefined {
return workspaceExt.rootPath;
},
get workspaceFolders(): theia.WorkspaceFolder[] | undefined {
return workspaceExt.workspaceFolders;
},
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-ext/src/plugin/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ export class WorkspaceExtImpl implements WorkspaceExt {
this.fileSystemWatcherManager = new InPluginFileSystemWatcherProxy(this.proxy);
}

get rootPath(): string | undefined {
const folder = this.folders && this.folders[0];
return folder && folder.uri.fsPath;
}

get workspaceFolders(): theia.WorkspaceFolder[] | undefined {
return this.folders;
}
Expand Down
40 changes: 34 additions & 6 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1996,7 +1996,7 @@ declare module '@theia/plugin' {
*
* Throw if a command is already registered for the given command identifier.
*/
export function registerCommand(command: Command, handler?: (...args: any[]) => any): Disposable;
export function registerCommand(command: Command, handler?: (...args: any[]) => any, thisArg?: any): Disposable;

/**
* Register the given handler for the given command identifier.
Expand All @@ -2006,22 +2006,39 @@ declare module '@theia/plugin' {
*
* Throw if a handler for the given command identifier is already registered.
*/
export function registerHandler(commandId: string, handler: (...args: any[]) => any): Disposable;
export function registerHandler(commandId: string, handler: (...args: any[]) => any, thisArg?: any): Disposable;

/**
* Register a text editor command which can execute only if active editor present and command has access to the active editor
* Registers a text editor command that can be invoked via a keyboard shortcut,
* a menu item, an action, or directly.
*
* @param command a command description
* @param handler a command handler with access to text editor
* Text editor commands are different from ordinary [commands](#commands.registerCommand) as
* they only execute when there is an active editor when the command is called. Also, the
* command handler of an editor command has access to the active editor and to an
* [edit](#TextEditorEdit)-builder.
*
* @param command A unique identifier for the command.
* @param callback A command handler function with access to an [editor](#TextEditor) and an [edit](#TextEditorEdit).
* @param thisArg The `this` context used when invoking the handler function.
* @return Disposable which unregisters this command on disposal.
*/
export function registerTextEditorCommand(command: Command, handler: (textEditor: TextEditor, edit: TextEditorEdit, ...arg: any[]) => void): Disposable;
export function registerTextEditorCommand(command: string, handler: (textEditor: TextEditor, edit: TextEditorEdit, ...arg: any[]) => void, thisArg?: any): Disposable;

/**
* Execute the active handler for the given command and arguments.
*
* Reject if a command cannot be executed.
*/
export function executeCommand<T>(commandId: string, ...args: any[]): PromiseLike<T | undefined>;

/**
* Retrieve the list of all available commands. Commands starting an underscore are
* treated as internal commands.
*
* @param filterInternal Set `true` to not see internal commands (starting with an underscore)
* @return Thenable that resolves to a list of command ids.
*/
export function getCommands(filterInternal?: boolean): PromiseLike<string[]>;
}

/**
Expand Down Expand Up @@ -3750,6 +3767,17 @@ declare module '@theia/plugin' {
* the editor-process so that they should be always used instead of nodejs-equivalents.
*/
export namespace workspace {

/**
* ~~The folder that is open in the editor. `undefined` when no folder
* has been opened.~~
*
* @deprecated Use [`workspaceFolders`](#workspace.workspaceFolders) instead.
*
* @readonly
*/
export let rootPath: string | undefined;

/**
* List of workspace folders or `undefined` when no folder is open.
* *Note* that the first entry corresponds to the value of `rootPath`.
Expand Down

0 comments on commit d991632

Please sign in to comment.