From 7b5871136bee580f4af2b02ebd93df409b268c43 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 26 Feb 2019 12:12:42 -0600 Subject: [PATCH] Add IDE API (#36) --- packages/ide-api/README.md | 5 +++++ packages/ide-api/api.d.ts | 38 +++++++++++++++++++++++++++++++++++ packages/ide-api/package.json | 7 +++++++ packages/vscode/src/client.ts | 14 +++++++++++++ 4 files changed, 64 insertions(+) create mode 100644 packages/ide-api/README.md create mode 100644 packages/ide-api/api.d.ts create mode 100644 packages/ide-api/package.json diff --git a/packages/ide-api/README.md b/packages/ide-api/README.md new file mode 100644 index 00000000..3e26f7a6 --- /dev/null +++ b/packages/ide-api/README.md @@ -0,0 +1,5 @@ +# ide-api + +Provides window listeners for interfacing with the IDE. + +Created for content-scripts. \ No newline at end of file diff --git a/packages/ide-api/api.d.ts b/packages/ide-api/api.d.ts new file mode 100644 index 00000000..5d09b746 --- /dev/null +++ b/packages/ide-api/api.d.ts @@ -0,0 +1,38 @@ +interface EvalHelper { } +interface ActiveEvalEmitter { + removeAllListeners(event?: string): void; + emit(event: string, ...args: any[]): void; + on(event: string, cb: (...args: any[]) => void): void; +} +interface Disposer { + onDidDispose: (cb: () => void) => void; + dispose(): void; +} +interface IdeApi { + readonly client: { + run(func: (helper: ActiveEvalEmitter) => Disposer): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1) => Disposer, a1: T1): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2) => Disposer, a1: T1, a2: T2): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3) => Disposer, a1: T1, a2: T2, a3: T3): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3, a4: T4) => Disposer, a1: T1, a2: T2, a3: T3, a4: T4): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5) => Disposer, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5): ActiveEvalEmitter; + run(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6) => Disposer, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6): ActiveEvalEmitter; + + evaluate(func: (helper: EvalHelper) => R | Promise): Promise; + evaluate(func: (helper: EvalHelper, a1: T1) => R | Promise, a1: T1): Promise; + evaluate(func: (helper: EvalHelper, a1: T1, a2: T2) => R | Promise, a1: T1, a2: T2): Promise; + evaluate(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3) => R | Promise, a1: T1, a2: T2, a3: T3): Promise; + evaluate(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4) => R | Promise, a1: T1, a2: T2, a3: T3, a4: T4): Promise; + evaluate(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5) => R | Promise, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5): Promise; + evaluate(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6) => R | Promise, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6): Promise; + }; + readonly workbench: { + getService(identifier: any): T | undefined; + }; +} + +declare interface Window { + ide?: IdeApi; + + addEventListener(event: "ide-ready", callback: (ide: CustomEvent & { readonly ide: IdeApi }) => void): void; +} \ No newline at end of file diff --git a/packages/ide-api/package.json b/packages/ide-api/package.json new file mode 100644 index 00000000..952fd5b2 --- /dev/null +++ b/packages/ide-api/package.json @@ -0,0 +1,7 @@ +{ + "name": "@coder/ide-api", + "typings": "api.d.ts", + "author": "Coder", + "license": "MIT", + "description": "API for interfacing with the API created for content-scripts" +} \ No newline at end of file diff --git a/packages/vscode/src/client.ts b/packages/vscode/src/client.ts index 92bce618..ebec1a86 100644 --- a/packages/vscode/src/client.ts +++ b/packages/vscode/src/client.ts @@ -1,4 +1,5 @@ import { IdeClient } from "@coder/ide"; +import { client as ideClientInstance } from "@coder/ide/src/fill/client"; import * as paths from "./fill/paths"; import "./vscode.scss"; // NOTE: shouldn't import anything from VS Code here or anything that will @@ -14,6 +15,19 @@ class VSClient extends IdeClient { // callback, meaning we are safe to include everything from VS Code now. const { workbench } = require("./workbench") as typeof import("./workbench"); await workbench.initialize(); + + window.ide = { + client: ideClientInstance, + workbench: { + // tslint:disable-next-line:no-any + getService: (id: any): T => workbench.serviceCollection.get(id) as T, + }, + }; + + const event = new CustomEvent("ide-ready"); + // tslint:disable-next-line:no-any + (event).ide = window.ide; + window.dispatchEvent(event); }, this.initData, this.sharedProcessData); } }