From 047662753a7f04c4212a3bdfbc3293efc1c3c67b Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Thu, 28 Mar 2024 13:41:21 -0400 Subject: [PATCH] Make language configurable. Fixes #9 --- demo/index.html | 7 ++++--- demo/index.ts | 46 +++++++++++++++++++++++++++++----------------- package-lock.json | 31 ++++++++++++++++++++++++++++--- package.json | 1 + src/codeium.ts | 5 +++-- src/config.ts | 10 +++++++++- src/plugin.ts | 3 +++ 7 files changed, 77 insertions(+), 26 deletions(-) diff --git a/demo/index.html b/demo/index.html index 3cb43b8..9e08e8b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -20,9 +20,10 @@

TypeScript AI autocompletion

-
-
-
+
+ +

Python AI autocompletion

+
diff --git a/demo/index.ts b/demo/index.ts index 9e93218..0f2bf93 100644 --- a/demo/index.ts +++ b/demo/index.ts @@ -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")!, +}); diff --git a/package-lock.json b/package-lock.json index 98d1891..7105e29 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@bufbuild/buf": "^1.30.0", "@bufbuild/protobuf": "^1.8.0", "@codemirror/lang-javascript": "^6.2.1", + "@codemirror/lang-python": "^6.1.4", "codemirror": "^6", "typescript": "^5.2.2", "vite": "^4.5.0", @@ -198,6 +199,19 @@ "@lezer/javascript": "^1.0.0" } }, + "node_modules/@codemirror/lang-python": { + "version": "6.1.4", + "resolved": "https://registry.npmjs.org/@codemirror/lang-python/-/lang-python-6.1.4.tgz", + "integrity": "sha512-b6d1TDqrkCjFNvMO01SWldFiDoZ39yl3tDMC1Y5f8glA2eZpynPxJhwYVTlGFr0stizcJgrp6ojLEGH2myoZAw==", + "dev": true, + "dependencies": { + "@codemirror/autocomplete": "^6.3.2", + "@codemirror/language": "^6.8.0", + "@codemirror/state": "^6.0.0", + "@lezer/common": "^1.2.1", + "@lezer/python": "^1.1.4" + } + }, "node_modules/@codemirror/language": { "version": "6.9.2", "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.9.2.tgz", @@ -636,9 +650,9 @@ "dev": true }, "node_modules/@lezer/common": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.1.1.tgz", - "integrity": "sha512-aAPB9YbvZHqAW+bIwiuuTDGB4DG0sYNRObGLxud8cW7osw1ZQxfDuTZ8KQiqfZ0QJGcR34CvpTMDXEyo/+Htgg==" + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.2.1.tgz", + "integrity": "sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ==" }, "node_modules/@lezer/highlight": { "version": "1.2.0", @@ -666,6 +680,17 @@ "@lezer/common": "^1.0.0" } }, + "node_modules/@lezer/python": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/@lezer/python/-/python-1.1.13.tgz", + "integrity": "sha512-AdbRAtdQq94PfTNd4kqMEJhH2fqa2JdoyyqqVewY6w34w2Gi6dg2JuOtOgR21Bi0zP9r0KjSSHOUq/tP7FVT8A==", + "dev": true, + "dependencies": { + "@lezer/common": "^1.2.0", + "@lezer/highlight": "^1.0.0", + "@lezer/lr": "^1.0.0" + } + }, "node_modules/@sinclair/typebox": { "version": "0.27.8", "resolved": "https://registry.npmjs.org/@sinclair/typebox/-/typebox-0.27.8.tgz", diff --git a/package.json b/package.json index 44841b0..6521573 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/codeium.ts b/src/codeium.ts index 0843487..8ce7117 100644 --- a/src/codeium.ts +++ b/src/codeium.ts @@ -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"; @@ -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", diff --git a/src/config.ts b/src/config.ts index 5ec0047..db12d88 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,7 +1,9 @@ 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< @@ -9,6 +11,12 @@ export const codeiumConfig = Facet.define< Required >({ combine(configs) { - return combineConfig>(configs, {}, {}); + return combineConfig>( + configs, + { + language: Language.TYPESCRIPT, + }, + {}, + ); }, }); diff --git a/src/plugin.ts b/src/plugin.ts index 39c7bf7..7c91304 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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({ @@ -33,6 +34,8 @@ function viewCompletionPlugin() { }); } +export { Language }; + export function copilotPlugin(config: CodeiumConfig): Extension { return [ codeiumConfig.of(config),