diff --git a/src/api.ts b/src/api.ts index f3b483759..26880cc21 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,5 @@ import * as vscode from "vscode"; +import { CoderApi, VSCodeApi } from "../typings/api"; import { createCSSRule } from "vs/base/browser/dom"; import { Emitter, Event } from "vs/base/common/event"; import { IDisposable } from "vs/base/common/lifecycle"; @@ -37,7 +38,7 @@ import { IViewletService } from "vs/workbench/services/viewlet/browser/viewlet"; * TODO: Implement menu items for views (for item actions). * TODO: File system provider doesn't work. */ -export const vscodeApi = (serviceCollection: ServiceCollection): Partial => { +export const vscodeApi = (serviceCollection: ServiceCollection): VSCodeApi => { const getService = (id: ServiceIdentifier): T => serviceCollection.get(id) as T; const commandService = getService(ICommandService); const notificationService = getService(INotificationService); @@ -51,13 +52,13 @@ export const vscodeApi = (serviceCollection: ServiceCollection): PartialEmitter, // It can take T so T | undefined should work. FileSystemError: extHostTypes.FileSystemError, FileType, + StatusBarAlignment: extHostTypes.StatusBarAlignment, + ThemeColor: extHostTypes.ThemeColor, + TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState, Uri: URI, - StatusBarAlignment, - ThemeColor, commands: { executeCommand: (commandId: string, ...args: any[]): Promise => { return commandService.executeCommand(commandId, ...args); @@ -65,10 +66,10 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial any): IDisposable => { return CommandsRegistry.registerCommand(id, command); }, - } as Partial, + }, window: { - createStatusBarItem: (alignment?: vscode.StatusBarAlignment, priority?: number): vscode.StatusBarItem => { - return new StatusBarEntry(statusbarService, alignment, priority); + createStatusBarItem(alignmentOrOptions?: extHostTypes.StatusBarAlignment | vscode.window.StatusBarItemOptions, priority?: number): StatusBarEntry { + return new StatusBarEntry(statusbarService, alignmentOrOptions, priority); }, registerTreeDataProvider: (id: string, dataProvider: vscode.TreeDataProvider): IDisposable => { const tree = new TreeViewDataProvider(dataProvider); @@ -82,20 +83,20 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial, + }, workspace: { registerFileSystemProvider: (scheme: string, provider: vscode.FileSystemProvider): IDisposable => { return fileService.registerProvider(scheme, new FileSystemProvider(provider)); }, - } as Partial, - } as Partial; // Without this it complains that the type isn't `| undefined`. + }, + }; }; /** * Coder API. This should only provide functionality that can't be made * available through the VS Code API. */ -export const coderApi = (serviceCollection: ServiceCollection): typeof coder => { +export const coderApi = (serviceCollection: ServiceCollection): CoderApi => { const getService = (id: ServiceIdentifier): T => serviceCollection.get(id) as T; return { registerView: (viewId, viewName, containerId, containerName, icon): void => { @@ -275,72 +276,71 @@ class TreeViewDataProvider implements ITreeViewDataProvider { } } -class ThemeColor { - public id: string; - constructor(id: string) { - this.id = id; - } -} - interface IStatusBarEntry extends IStatusbarEntry { alignment: StatusbarAlignment; priority?: number; } -enum StatusBarAlignment { - Left = 1, - Right = 2 -} - class StatusBarEntry implements vscode.StatusBarItem { private static ID = 0; private _id: number; private entry: IStatusBarEntry; - private _visible: boolean; + private visible: boolean; private disposed: boolean; private statusId: string; private statusName: string; private accessor?: IStatusbarEntryAccessor; private timeout: any; - constructor(private readonly statusbarService: IStatusbarService, alignment?: vscode.StatusBarAlignment, priority?: number) { + constructor(private readonly statusbarService: IStatusbarService, alignmentOrOptions?: extHostTypes.StatusBarAlignment | vscode.window.StatusBarItemOptions, priority?: number) { this._id = StatusBarEntry.ID--; - this.statusId = "web-api"; - this.statusName = "Web API"; - this.entry = { - alignment: alignment && alignment === StatusBarAlignment.Left - ? StatusbarAlignment.LEFT : StatusbarAlignment.RIGHT, - text: "", - priority, - }; + if (alignmentOrOptions && typeof alignmentOrOptions !== "number") { + this.statusId = alignmentOrOptions.id; + this.statusName = alignmentOrOptions.name; + this.entry = { + alignment: alignmentOrOptions.alignment === extHostTypes.StatusBarAlignment.Right + ? StatusbarAlignment.RIGHT : StatusbarAlignment.LEFT, + priority, + text: "", + }; + } else { + this.statusId = "web-api"; + this.statusName = "Web API"; + this.entry = { + alignment: alignmentOrOptions === extHostTypes.StatusBarAlignment.Right + ? StatusbarAlignment.RIGHT : StatusbarAlignment.LEFT, + priority, + text: "", + }; + } } - public get alignment(): vscode.StatusBarAlignment { - return this.entry.alignment === StatusbarAlignment.LEFT - ? StatusBarAlignment.Left : StatusBarAlignment.Right; + public get alignment(): extHostTypes.StatusBarAlignment { + return this.entry.alignment === StatusbarAlignment.RIGHT + ? extHostTypes.StatusBarAlignment.Right : extHostTypes.StatusBarAlignment.Left; } public get id(): number { return this._id; } public get priority(): number | undefined { return this.entry.priority; } public get text(): string { return this.entry.text; } public get tooltip(): string | undefined { return this.entry.tooltip; } - public get color(): string | ThemeColor | undefined { return this.entry.color; } + public get color(): string | extHostTypes.ThemeColor | undefined { return this.entry.color; } public get command(): string | undefined { return this.entry.command; } public set text(text: string) { this.update({ text }); } public set tooltip(tooltip: string | undefined) { this.update({ tooltip }); } - public set color(color: string | ThemeColor | undefined) { this.update({ color }); } + public set color(color: string | extHostTypes.ThemeColor | undefined) { this.update({ color }); } public set command(command: string | undefined) { this.update({ command }); } public show(): void { - this._visible = true; + this.visible = true; this.update(); } public hide(): void { clearTimeout(this.timeout); - this._visible = false; + this.visible = false; if (this.accessor) { this.accessor.dispose(); this.accessor = undefined; @@ -349,7 +349,7 @@ class StatusBarEntry implements vscode.StatusBarItem { private update(values?: Partial): void { this.entry = { ...this.entry, ...values }; - if (this.disposed || !this._visible) { + if (this.disposed || !this.visible) { return; } clearTimeout(this.timeout); diff --git a/typings/.npmignore b/typings/.npmignore new file mode 100644 index 000000000..228e70f90 --- /dev/null +++ b/typings/.npmignore @@ -0,0 +1 @@ +httpolyglot.d.ts \ No newline at end of file diff --git a/typings/api.d.ts b/typings/api.d.ts index e85eddc9b..6657f6eb5 100644 --- a/typings/api.d.ts +++ b/typings/api.d.ts @@ -1,10 +1,35 @@ import * as vscode from "vscode"; -export { vscode }; +// Only export the subset of VS Code we have implemented. +export interface VSCodeApi { + EventEmitter: typeof vscode.EventEmitter; + FileSystemError: typeof vscode.FileSystemError; + FileType: typeof vscode.FileType; + StatusBarAlignment: typeof vscode.StatusBarAlignment; + ThemeColor: typeof vscode.ThemeColor; + TreeItemCollapsibleState: typeof vscode.TreeItemCollapsibleState; + Uri: typeof vscode.Uri; + commands: { + executeCommand: typeof vscode.commands.executeCommand; + registerCommand: typeof vscode.commands.registerCommand; + }; + window: { + createStatusBarItem: typeof vscode.window.createStatusBarItem; + registerTreeDataProvider: typeof vscode.window.registerTreeDataProvider; + showErrorMessage: typeof vscode.window.showErrorMessage; + }; + workspace: { + registerFileSystemProvider: typeof vscode.workspace.registerFileSystemProvider; + }; +} + +export interface CoderApi { + registerView: (viewId: string, viewName: string, containerId: string, containerName: string, icon: string) => void; +} export interface IdeReadyEvent extends CustomEvent { - readonly vscode: typeof vscode; - readonly ide: typeof coder; + readonly vscode: VSCodeApi; + readonly ide: CoderApi; } declare global { @@ -12,12 +37,12 @@ declare global { /** * Full VS Code extension API. */ - vscode?: typeof vscode; + vscode?: VSCodeApi; /** * Coder API. */ - ide?: typeof coder; + ide?: CoderApi; /** * Listen for when the IDE API has been set and is ready to use. diff --git a/typings/coder.d.ts b/typings/coder.d.ts deleted file mode 100644 index e550513e1..000000000 --- a/typings/coder.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare namespace coder { - export const registerView: (viewId: string, viewName: string, containerId: string, containerName: string, icon: string) => void; -} diff --git a/typings/package.json b/typings/package.json new file mode 100644 index 000000000..11c1652e4 --- /dev/null +++ b/typings/package.json @@ -0,0 +1,11 @@ +{ + "name": "@coder/ide-api", + "version": "2.0.3", + "typings": "api.d.ts", + "license": "MIT", + "author": "Coder", + "description": "API for interfacing with the API created for content-scripts.", + "dependencies": { + "@types/vscode": "^1.37.0" + } +}