Add package.json for publishing API types

This commit is contained in:
Asher 2019-09-03 17:55:58 -05:00
parent da7d8b04a8
commit a26844ea45
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
5 changed files with 84 additions and 50 deletions

View File

@ -1,4 +1,5 @@
import * as vscode from "vscode"; import * as vscode from "vscode";
import { CoderApi, VSCodeApi } from "../typings/api";
import { createCSSRule } from "vs/base/browser/dom"; import { createCSSRule } from "vs/base/browser/dom";
import { Emitter, Event } from "vs/base/common/event"; import { Emitter, Event } from "vs/base/common/event";
import { IDisposable } from "vs/base/common/lifecycle"; 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: Implement menu items for views (for item actions).
* TODO: File system provider doesn't work. * TODO: File system provider doesn't work.
*/ */
export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof vscode> => { export const vscodeApi = (serviceCollection: ServiceCollection): VSCodeApi => {
const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T; const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
const commandService = getService(ICommandService); const commandService = getService(ICommandService);
const notificationService = getService(INotificationService); const notificationService = getService(INotificationService);
@ -51,13 +52,13 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof
// browser's main thread, but I'm not sure how much jank that would require. // browser's main thread, but I'm not sure how much jank that would require.
// We could have a web worker host but we want DOM access. // We could have a web worker host but we want DOM access.
return { return {
EventEmitter: Emitter, EventEmitter: <any>Emitter, // It can take T so T | undefined should work.
TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState,
FileSystemError: extHostTypes.FileSystemError, FileSystemError: extHostTypes.FileSystemError,
FileType, FileType,
StatusBarAlignment: extHostTypes.StatusBarAlignment,
ThemeColor: extHostTypes.ThemeColor,
TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState,
Uri: URI, Uri: URI,
StatusBarAlignment,
ThemeColor,
commands: { commands: {
executeCommand: <T = any>(commandId: string, ...args: any[]): Promise<T | undefined> => { executeCommand: <T = any>(commandId: string, ...args: any[]): Promise<T | undefined> => {
return commandService.executeCommand(commandId, ...args); return commandService.executeCommand(commandId, ...args);
@ -65,10 +66,10 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof
registerCommand: (id: string, command: (...args: any[]) => any): IDisposable => { registerCommand: (id: string, command: (...args: any[]) => any): IDisposable => {
return CommandsRegistry.registerCommand(id, command); return CommandsRegistry.registerCommand(id, command);
}, },
} as Partial<typeof vscode.commands>, },
window: { window: {
createStatusBarItem: (alignment?: vscode.StatusBarAlignment, priority?: number): vscode.StatusBarItem => { createStatusBarItem(alignmentOrOptions?: extHostTypes.StatusBarAlignment | vscode.window.StatusBarItemOptions, priority?: number): StatusBarEntry {
return new StatusBarEntry(statusbarService, alignment, priority); return new StatusBarEntry(statusbarService, alignmentOrOptions, priority);
}, },
registerTreeDataProvider: <T>(id: string, dataProvider: vscode.TreeDataProvider<T>): IDisposable => { registerTreeDataProvider: <T>(id: string, dataProvider: vscode.TreeDataProvider<T>): IDisposable => {
const tree = new TreeViewDataProvider(dataProvider); const tree = new TreeViewDataProvider(dataProvider);
@ -82,20 +83,20 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof
notificationService.error(message); notificationService.error(message);
return undefined; return undefined;
}, },
} as Partial<typeof vscode.window>, },
workspace: { workspace: {
registerFileSystemProvider: (scheme: string, provider: vscode.FileSystemProvider): IDisposable => { registerFileSystemProvider: (scheme: string, provider: vscode.FileSystemProvider): IDisposable => {
return fileService.registerProvider(scheme, new FileSystemProvider(provider)); return fileService.registerProvider(scheme, new FileSystemProvider(provider));
}, },
} as Partial<typeof vscode.workspace>, },
} as Partial<typeof vscode>; // Without this it complains that the type isn't `| undefined`. };
}; };
/** /**
* Coder API. This should only provide functionality that can't be made * Coder API. This should only provide functionality that can't be made
* available through the VS Code API. * available through the VS Code API.
*/ */
export const coderApi = (serviceCollection: ServiceCollection): typeof coder => { export const coderApi = (serviceCollection: ServiceCollection): CoderApi => {
const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T; const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
return { return {
registerView: (viewId, viewName, containerId, containerName, icon): void => { registerView: (viewId, viewName, containerId, containerName, icon): void => {
@ -275,72 +276,71 @@ class TreeViewDataProvider<T> implements ITreeViewDataProvider {
} }
} }
class ThemeColor {
public id: string;
constructor(id: string) {
this.id = id;
}
}
interface IStatusBarEntry extends IStatusbarEntry { interface IStatusBarEntry extends IStatusbarEntry {
alignment: StatusbarAlignment; alignment: StatusbarAlignment;
priority?: number; priority?: number;
} }
enum StatusBarAlignment {
Left = 1,
Right = 2
}
class StatusBarEntry implements vscode.StatusBarItem { class StatusBarEntry implements vscode.StatusBarItem {
private static ID = 0; private static ID = 0;
private _id: number; private _id: number;
private entry: IStatusBarEntry; private entry: IStatusBarEntry;
private _visible: boolean; private visible: boolean;
private disposed: boolean; private disposed: boolean;
private statusId: string; private statusId: string;
private statusName: string; private statusName: string;
private accessor?: IStatusbarEntryAccessor; private accessor?: IStatusbarEntryAccessor;
private timeout: any; 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._id = StatusBarEntry.ID--;
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.statusId = "web-api";
this.statusName = "Web API"; this.statusName = "Web API";
this.entry = { this.entry = {
alignment: alignment && alignment === StatusBarAlignment.Left alignment: alignmentOrOptions === extHostTypes.StatusBarAlignment.Right
? StatusbarAlignment.LEFT : StatusbarAlignment.RIGHT, ? StatusbarAlignment.RIGHT : StatusbarAlignment.LEFT,
text: "",
priority, priority,
text: "",
}; };
} }
}
public get alignment(): vscode.StatusBarAlignment { public get alignment(): extHostTypes.StatusBarAlignment {
return this.entry.alignment === StatusbarAlignment.LEFT return this.entry.alignment === StatusbarAlignment.RIGHT
? StatusBarAlignment.Left : StatusBarAlignment.Right; ? extHostTypes.StatusBarAlignment.Right : extHostTypes.StatusBarAlignment.Left;
} }
public get id(): number { return this._id; } public get id(): number { return this._id; }
public get priority(): number | undefined { return this.entry.priority; } public get priority(): number | undefined { return this.entry.priority; }
public get text(): string { return this.entry.text; } public get text(): string { return this.entry.text; }
public get tooltip(): string | undefined { return this.entry.tooltip; } 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 get command(): string | undefined { return this.entry.command; }
public set text(text: string) { this.update({ text }); } public set text(text: string) { this.update({ text }); }
public set tooltip(tooltip: string | undefined) { this.update({ tooltip }); } 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 set command(command: string | undefined) { this.update({ command }); }
public show(): void { public show(): void {
this._visible = true; this.visible = true;
this.update(); this.update();
} }
public hide(): void { public hide(): void {
clearTimeout(this.timeout); clearTimeout(this.timeout);
this._visible = false; this.visible = false;
if (this.accessor) { if (this.accessor) {
this.accessor.dispose(); this.accessor.dispose();
this.accessor = undefined; this.accessor = undefined;
@ -349,7 +349,7 @@ class StatusBarEntry implements vscode.StatusBarItem {
private update(values?: Partial<IStatusBarEntry>): void { private update(values?: Partial<IStatusBarEntry>): void {
this.entry = { ...this.entry, ...values }; this.entry = { ...this.entry, ...values };
if (this.disposed || !this._visible) { if (this.disposed || !this.visible) {
return; return;
} }
clearTimeout(this.timeout); clearTimeout(this.timeout);

1
typings/.npmignore Normal file
View File

@ -0,0 +1 @@
httpolyglot.d.ts

35
typings/api.d.ts vendored
View File

@ -1,10 +1,35 @@
import * as vscode from "vscode"; 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<void> { export interface IdeReadyEvent extends CustomEvent<void> {
readonly vscode: typeof vscode; readonly vscode: VSCodeApi;
readonly ide: typeof coder; readonly ide: CoderApi;
} }
declare global { declare global {
@ -12,12 +37,12 @@ declare global {
/** /**
* Full VS Code extension API. * Full VS Code extension API.
*/ */
vscode?: typeof vscode; vscode?: VSCodeApi;
/** /**
* Coder API. * Coder API.
*/ */
ide?: typeof coder; ide?: CoderApi;
/** /**
* Listen for when the IDE API has been set and is ready to use. * Listen for when the IDE API has been set and is ready to use.

3
typings/coder.d.ts vendored
View File

@ -1,3 +0,0 @@
declare namespace coder {
export const registerView: (viewId: string, viewName: string, containerId: string, containerName: string, icon: string) => void;
}

11
typings/package.json Normal file
View File

@ -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"
}
}