Skip to content

Commit

Permalink
Make language configurable. Fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 28, 2024
1 parent 4712f40 commit 0476627
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 26 deletions.
7 changes: 4 additions & 3 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ <h1>
</p>

<h3>TypeScript AI autocompletion</h3>
<div id="app">
<div id="editor"></div>
</div>
<div id="editor"></div>

<h3>Python AI autocompletion</h3>
<div id="editor-python"></div>
</main>
<script type="module" src="./index.ts"></script>
</body>
Expand Down
46 changes: 29 additions & 17 deletions demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import { EditorView, basicSetup } from "codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { copilotPlugin } from "../src/plugin.js";
import { Language, copilotPlugin } from "../src/plugin.js";
import { python } from "@codemirror/lang-python";

(async () => {
new EditorView({
doc: `let hasAnError: string = 10;
new EditorView({
doc: `let hasAnError: string = 10;
function increment(num: number) {
return num + 1;
}
increment('not a number');`,
extensions: [
basicSetup,
javascript({
typescript: true,
jsx: true,
}),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
}),
],
parent: document.querySelector("#editor")!,
});
})().catch((e) => console.error(e));
extensions: [
basicSetup,
javascript({
typescript: true,
jsx: true,
}),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
}),
],
parent: document.querySelector("#editor")!,
});

new EditorView({
doc: `def hi_python():`,
extensions: [
basicSetup,
python(),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
language: Language.PYTHON,
}),
],
parent: document.querySelector("#editor-python")!,
});
31 changes: 28 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"@bufbuild/buf": "^1.30.0",
"@bufbuild/protobuf": "^1.8.0",
"@codemirror/lang-python": "^6.1.4",
"@codemirror/lang-javascript": "^6.2.1",
"codemirror": "^6",
"typescript": "^5.2.2",
Expand Down
5 changes: 3 additions & 2 deletions src/codeium.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createPromiseClient } from "@connectrpc/connect";
import { LanguageServerService } from "./api/proto/exa/language_server_pb/language_server_connect.js";
import { createConnectTransport } from "@connectrpc/connect-web";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";
import { GetCompletionsResponse } from "./api/proto/exa/language_server_pb/language_server_pb.js";
import { CodeiumConfig } from "./config.js";

Expand Down Expand Up @@ -37,7 +36,9 @@ export async function getCodeiumCompletions({
document: {
text: text,
cursorOffset: BigInt(cursorOffset),
language: Language.TYPESCRIPT,
language: config.language,
// TODO: not sure why we have both language and
// editorlanguage
// The types don't like this here, but it works.
editorLanguage: "typescript",
lineEnding: "\n",
Expand Down
10 changes: 9 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { Facet, combineConfig } from "@codemirror/state";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";

export interface CodeiumConfig {
apiKey: string;
language?: Language;
}

export const codeiumConfig = Facet.define<
CodeiumConfig,
Required<CodeiumConfig>
>({
combine(configs) {
return combineConfig<Required<CodeiumConfig>>(configs, {}, {});
return combineConfig<Required<CodeiumConfig>>(
configs,
{
language: Language.TYPESCRIPT,
},
{},
);
},
});
3 changes: 3 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { completionDecoration } from "./completionDecoration.js";
import { completionRequester } from "./completionRequester.js";
import { sameKeyCommand, rejectSuggestionCommand } from "./commands.js";
import { CodeiumConfig, codeiumConfig } from "./config.js";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";

function completionPlugin() {
return EditorView.domEventHandlers({
Expand Down Expand Up @@ -33,6 +34,8 @@ function viewCompletionPlugin() {
});
}

export { Language };

export function copilotPlugin(config: CodeiumConfig): Extension {
return [
codeiumConfig.of(config),
Expand Down

0 comments on commit 0476627

Please sign in to comment.