Skip to content

Commit a26844e

Browse files
committed
Add package.json for publishing API types
1 parent da7d8b0 commit a26844e

File tree

5 files changed

+84
-50
lines changed

5 files changed

+84
-50
lines changed

src/api.ts

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode";
2+
import { CoderApi, VSCodeApi } from "../typings/api";
23
import { createCSSRule } from "vs/base/browser/dom";
34
import { Emitter, Event } from "vs/base/common/event";
45
import { IDisposable } from "vs/base/common/lifecycle";
@@ -37,7 +38,7 @@ import { IViewletService } from "vs/workbench/services/viewlet/browser/viewlet";
3738
* TODO: Implement menu items for views (for item actions).
3839
* TODO: File system provider doesn't work.
3940
*/
40-
export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof vscode> => {
41+
export const vscodeApi = (serviceCollection: ServiceCollection): VSCodeApi => {
4142
const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
4243
const commandService = getService(ICommandService);
4344
const notificationService = getService(INotificationService);
@@ -51,24 +52,24 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof
5152
// browser's main thread, but I'm not sure how much jank that would require.
5253
// We could have a web worker host but we want DOM access.
5354
return {
54-
EventEmitter: Emitter,
55-
TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState,
55+
EventEmitter: <any>Emitter, // It can take T so T | undefined should work.
5656
FileSystemError: extHostTypes.FileSystemError,
5757
FileType,
58+
StatusBarAlignment: extHostTypes.StatusBarAlignment,
59+
ThemeColor: extHostTypes.ThemeColor,
60+
TreeItemCollapsibleState: extHostTypes.TreeItemCollapsibleState,
5861
Uri: URI,
59-
StatusBarAlignment,
60-
ThemeColor,
6162
commands: {
6263
executeCommand: <T = any>(commandId: string, ...args: any[]): Promise<T | undefined> => {
6364
return commandService.executeCommand(commandId, ...args);
6465
},
6566
registerCommand: (id: string, command: (...args: any[]) => any): IDisposable => {
6667
return CommandsRegistry.registerCommand(id, command);
6768
},
68-
} as Partial<typeof vscode.commands>,
69+
},
6970
window: {
70-
createStatusBarItem: (alignment?: vscode.StatusBarAlignment, priority?: number): vscode.StatusBarItem => {
71-
return new StatusBarEntry(statusbarService, alignment, priority);
71+
createStatusBarItem(alignmentOrOptions?: extHostTypes.StatusBarAlignment | vscode.window.StatusBarItemOptions, priority?: number): StatusBarEntry {
72+
return new StatusBarEntry(statusbarService, alignmentOrOptions, priority);
7273
},
7374
registerTreeDataProvider: <T>(id: string, dataProvider: vscode.TreeDataProvider<T>): IDisposable => {
7475
const tree = new TreeViewDataProvider(dataProvider);
@@ -82,20 +83,20 @@ export const vscodeApi = (serviceCollection: ServiceCollection): Partial<typeof
8283
notificationService.error(message);
8384
return undefined;
8485
},
85-
} as Partial<typeof vscode.window>,
86+
},
8687
workspace: {
8788
registerFileSystemProvider: (scheme: string, provider: vscode.FileSystemProvider): IDisposable => {
8889
return fileService.registerProvider(scheme, new FileSystemProvider(provider));
8990
},
90-
} as Partial<typeof vscode.workspace>,
91-
} as Partial<typeof vscode>; // Without this it complains that the type isn't `| undefined`.
91+
},
92+
};
9293
};
9394

9495
/**
9596
* Coder API. This should only provide functionality that can't be made
9697
* available through the VS Code API.
9798
*/
98-
export const coderApi = (serviceCollection: ServiceCollection): typeof coder => {
99+
export const coderApi = (serviceCollection: ServiceCollection): CoderApi => {
99100
const getService = <T>(id: ServiceIdentifier<T>): T => serviceCollection.get<T>(id) as T;
100101
return {
101102
registerView: (viewId, viewName, containerId, containerName, icon): void => {
@@ -275,72 +276,71 @@ class TreeViewDataProvider<T> implements ITreeViewDataProvider {
275276
}
276277
}
277278

278-
class ThemeColor {
279-
public id: string;
280-
constructor(id: string) {
281-
this.id = id;
282-
}
283-
}
284-
285279
interface IStatusBarEntry extends IStatusbarEntry {
286280
alignment: StatusbarAlignment;
287281
priority?: number;
288282
}
289283

290-
enum StatusBarAlignment {
291-
Left = 1,
292-
Right = 2
293-
}
294-
295284
class StatusBarEntry implements vscode.StatusBarItem {
296285
private static ID = 0;
297286

298287
private _id: number;
299288
private entry: IStatusBarEntry;
300-
private _visible: boolean;
289+
private visible: boolean;
301290
private disposed: boolean;
302291
private statusId: string;
303292
private statusName: string;
304293
private accessor?: IStatusbarEntryAccessor;
305294
private timeout: any;
306295

307-
constructor(private readonly statusbarService: IStatusbarService, alignment?: vscode.StatusBarAlignment, priority?: number) {
296+
constructor(private readonly statusbarService: IStatusbarService, alignmentOrOptions?: extHostTypes.StatusBarAlignment | vscode.window.StatusBarItemOptions, priority?: number) {
308297
this._id = StatusBarEntry.ID--;
309-
this.statusId = "web-api";
310-
this.statusName = "Web API";
311-
this.entry = {
312-
alignment: alignment && alignment === StatusBarAlignment.Left
313-
? StatusbarAlignment.LEFT : StatusbarAlignment.RIGHT,
314-
text: "",
315-
priority,
316-
};
298+
if (alignmentOrOptions && typeof alignmentOrOptions !== "number") {
299+
this.statusId = alignmentOrOptions.id;
300+
this.statusName = alignmentOrOptions.name;
301+
this.entry = {
302+
alignment: alignmentOrOptions.alignment === extHostTypes.StatusBarAlignment.Right
303+
? StatusbarAlignment.RIGHT : StatusbarAlignment.LEFT,
304+
priority,
305+
text: "",
306+
};
307+
} else {
308+
this.statusId = "web-api";
309+
this.statusName = "Web API";
310+
this.entry = {
311+
alignment: alignmentOrOptions === extHostTypes.StatusBarAlignment.Right
312+
? StatusbarAlignment.RIGHT : StatusbarAlignment.LEFT,
313+
priority,
314+
text: "",
315+
};
316+
}
317317
}
318318

319-
public get alignment(): vscode.StatusBarAlignment {
320-
return this.entry.alignment === StatusbarAlignment.LEFT
321-
? StatusBarAlignment.Left : StatusBarAlignment.Right;
319+
public get alignment(): extHostTypes.StatusBarAlignment {
320+
return this.entry.alignment === StatusbarAlignment.RIGHT
321+
? extHostTypes.StatusBarAlignment.Right : extHostTypes.StatusBarAlignment.Left;
322322
}
323323

324324
public get id(): number { return this._id; }
325325
public get priority(): number | undefined { return this.entry.priority; }
326326
public get text(): string { return this.entry.text; }
327327
public get tooltip(): string | undefined { return this.entry.tooltip; }
328-
public get color(): string | ThemeColor | undefined { return this.entry.color; }
328+
public get color(): string | extHostTypes.ThemeColor | undefined { return this.entry.color; }
329329
public get command(): string | undefined { return this.entry.command; }
330330

331331
public set text(text: string) { this.update({ text }); }
332332
public set tooltip(tooltip: string | undefined) { this.update({ tooltip }); }
333-
public set color(color: string | ThemeColor | undefined) { this.update({ color }); }
333+
public set color(color: string | extHostTypes.ThemeColor | undefined) { this.update({ color }); }
334334
public set command(command: string | undefined) { this.update({ command }); }
335335

336336
public show(): void {
337-
this._visible = true;
337+
this.visible = true;
338338
this.update();
339339
}
340340

341341
public hide(): void {
342342
clearTimeout(this.timeout);
343-
this._visible = false;
343+
this.visible = false;
344344
if (this.accessor) {
345345
this.accessor.dispose();
346346
this.accessor = undefined;
@@ -349,7 +349,7 @@ class StatusBarEntry implements vscode.StatusBarItem {
349349

350350
private update(values?: Partial<IStatusBarEntry>): void {
351351
this.entry = { ...this.entry, ...values };
352-
if (this.disposed || !this._visible) {
352+
if (this.disposed || !this.visible) {
353353
return;
354354
}
355355
clearTimeout(this.timeout);

typings/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
httpolyglot.d.ts

typings/api.d.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
import * as vscode from "vscode";
22

3-
export { vscode };
3+
// Only export the subset of VS Code we have implemented.
4+
export interface VSCodeApi {
5+
EventEmitter: typeof vscode.EventEmitter;
6+
FileSystemError: typeof vscode.FileSystemError;
7+
FileType: typeof vscode.FileType;
8+
StatusBarAlignment: typeof vscode.StatusBarAlignment;
9+
ThemeColor: typeof vscode.ThemeColor;
10+
TreeItemCollapsibleState: typeof vscode.TreeItemCollapsibleState;
11+
Uri: typeof vscode.Uri;
12+
commands: {
13+
executeCommand: typeof vscode.commands.executeCommand;
14+
registerCommand: typeof vscode.commands.registerCommand;
15+
};
16+
window: {
17+
createStatusBarItem: typeof vscode.window.createStatusBarItem;
18+
registerTreeDataProvider: typeof vscode.window.registerTreeDataProvider;
19+
showErrorMessage: typeof vscode.window.showErrorMessage;
20+
};
21+
workspace: {
22+
registerFileSystemProvider: typeof vscode.workspace.registerFileSystemProvider;
23+
};
24+
}
25+
26+
export interface CoderApi {
27+
registerView: (viewId: string, viewName: string, containerId: string, containerName: string, icon: string) => void;
28+
}
429

530
export interface IdeReadyEvent extends CustomEvent<void> {
6-
readonly vscode: typeof vscode;
7-
readonly ide: typeof coder;
31+
readonly vscode: VSCodeApi;
32+
readonly ide: CoderApi;
833
}
934

1035
declare global {
1136
interface Window {
1237
/**
1338
* Full VS Code extension API.
1439
*/
15-
vscode?: typeof vscode;
40+
vscode?: VSCodeApi;
1641

1742
/**
1843
* Coder API.
1944
*/
20-
ide?: typeof coder;
45+
ide?: CoderApi;
2146

2247
/**
2348
* Listen for when the IDE API has been set and is ready to use.

typings/coder.d.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

typings/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "@coder/ide-api",
3+
"version": "2.0.3",
4+
"typings": "api.d.ts",
5+
"license": "MIT",
6+
"author": "Coder",
7+
"description": "API for interfacing with the API created for content-scripts.",
8+
"dependencies": {
9+
"@types/vscode": "^1.37.0"
10+
}
11+
}

0 commit comments

Comments
 (0)