Skip to content

Commit 026dc2a

Browse files
kylecarbscode-asher
authored andcommitted
Add IDE API (#36)
1 parent 091c1bd commit 026dc2a

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

packages/ide-api/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ide-api
2+
3+
Provides window listeners for interfacing with the IDE.
4+
5+
Created for content-scripts.

packages/ide-api/api.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
interface EvalHelper { }
2+
interface ActiveEvalEmitter {
3+
removeAllListeners(event?: string): void;
4+
emit(event: string, ...args: any[]): void;
5+
on(event: string, cb: (...args: any[]) => void): void;
6+
}
7+
interface Disposer {
8+
onDidDispose: (cb: () => void) => void;
9+
dispose(): void;
10+
}
11+
interface IdeApi {
12+
readonly client: {
13+
run(func: (helper: ActiveEvalEmitter) => Disposer): ActiveEvalEmitter;
14+
run<T1>(func: (helper: ActiveEvalEmitter, a1: T1) => Disposer, a1: T1): ActiveEvalEmitter;
15+
run<T1, T2>(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2) => Disposer, a1: T1, a2: T2): ActiveEvalEmitter;
16+
run<T1, T2, T3>(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3) => Disposer, a1: T1, a2: T2, a3: T3): ActiveEvalEmitter;
17+
run<T1, T2, T3, T4>(func: (helper: ActiveEvalEmitter, a1: T1, a2: T2, a3: T3, a4: T4) => Disposer, a1: T1, a2: T2, a3: T3, a4: T4): ActiveEvalEmitter;
18+
run<T1, T2, T3, T4, T5>(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;
19+
run<T1, T2, T3, T4, T5, T6>(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;
20+
21+
evaluate<R>(func: (helper: EvalHelper) => R | Promise<R>): Promise<R>;
22+
evaluate<R, T1>(func: (helper: EvalHelper, a1: T1) => R | Promise<R>, a1: T1): Promise<R>;
23+
evaluate<R, T1, T2>(func: (helper: EvalHelper, a1: T1, a2: T2) => R | Promise<R>, a1: T1, a2: T2): Promise<R>;
24+
evaluate<R, T1, T2, T3>(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3) => R | Promise<R>, a1: T1, a2: T2, a3: T3): Promise<R>;
25+
evaluate<R, T1, T2, T3, T4>(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4) => R | Promise<R>, a1: T1, a2: T2, a3: T3, a4: T4): Promise<R>;
26+
evaluate<R, T1, T2, T3, T4, T5>(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5) => R | Promise<R>, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5): Promise<R>;
27+
evaluate<R, T1, T2, T3, T4, T5, T6>(func: (helper: EvalHelper, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6) => R | Promise<R>, a1: T1, a2: T2, a3: T3, a4: T4, a5: T5, a6: T6): Promise<R>;
28+
};
29+
readonly workbench: {
30+
getService<T>(identifier: any): T | undefined;
31+
};
32+
}
33+
34+
declare interface Window {
35+
ide?: IdeApi;
36+
37+
addEventListener(event: "ide-ready", callback: (ide: CustomEvent & { readonly ide: IdeApi }) => void): void;
38+
}

packages/ide-api/package.json

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

packages/vscode/src/client.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IdeClient } from "@coder/ide";
2+
import { client as ideClientInstance } from "@coder/ide/src/fill/client";
23
import * as paths from "./fill/paths";
34
import "./vscode.scss";
45
// NOTE: shouldn't import anything from VS Code here or anything that will
@@ -14,6 +15,19 @@ class VSClient extends IdeClient {
1415
// callback, meaning we are safe to include everything from VS Code now.
1516
const { workbench } = require("./workbench") as typeof import("./workbench");
1617
await workbench.initialize();
18+
19+
window.ide = {
20+
client: ideClientInstance,
21+
workbench: {
22+
// tslint:disable-next-line:no-any
23+
getService: <T>(id: any): T => workbench.serviceCollection.get<T>(id) as T,
24+
},
25+
};
26+
27+
const event = new CustomEvent("ide-ready");
28+
// tslint:disable-next-line:no-any
29+
(<any>event).ide = window.ide;
30+
window.dispatchEvent(event);
1731
}, this.initData, this.sharedProcessData);
1832
}
1933
}

0 commit comments

Comments
 (0)