From 5059a63b635eaa508db2d92b8a7bc0c3940e2489 Mon Sep 17 00:00:00 2001 From: Baalateja Kataru Date: Tue, 26 Dec 2023 22:44:39 +0000 Subject: [PATCH 1/2] Refactored code to use custom interface generics from monaco-editor and added type-guard for inlineCompletionItems --- package.json | 2 +- .../CodeiumEditor/CompletionProvider.ts | 34 ++++--------------- .../CodeiumEditor/InlineCompletion.ts | 26 ++++++++++++++ .../CodeiumEditor/InlineCompletionProvider.ts | 6 +++- 4 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 src/components/CodeiumEditor/InlineCompletion.ts diff --git a/package.json b/package.json index f46b71d..042a444 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ }, "packageManager": "pnpm@8.9.0", "scripts": { - "generate": "rm -rf ./src/proto && node generate.js", + "generate": "rm -rf ./src/api/proto && node generate.js", "rollup": "rollup -c", "clean": "rm -rf node_modules && rm -rf dist", "test": "echo \"Error: no test specified\" && exit 1", diff --git a/src/components/CodeiumEditor/CompletionProvider.ts b/src/components/CodeiumEditor/CompletionProvider.ts index 9e324d3..63f8286 100644 --- a/src/components/CodeiumEditor/CompletionProvider.ts +++ b/src/components/CodeiumEditor/CompletionProvider.ts @@ -19,29 +19,7 @@ import { } from "../../api/proto/exa/codeium_common_pb/codeium_common_pb"; import { Status } from "./Status"; import { uuid } from "../../utils/uuid"; - -class MonacoInlineCompletion implements monaco.languages.InlineCompletion { - readonly insertText: string; - // TODO(prem): Why is this property needed? - readonly text: string; - readonly range: monaco.IRange; - readonly command: { - id: string; - title: string; - arguments: string[]; - }; - - constructor(insertText: string, range: monaco.IRange, completionId: string) { - this.insertText = insertText; - this.text = insertText; - this.range = range; - this.command = { - id: "codeium.acceptCompletion", - title: "Accept Completion", - arguments: [completionId, insertText], - }; - } -} +import MonacoInlineCompletion from "./InlineCompletion"; /** * CompletionProvider class for Codeium. @@ -81,8 +59,7 @@ export class MonacoCompletionProvider { monacoPosition: monaco.Position, token: CancellationToken ): Promise< - | monaco.languages.InlineCompletions - | undefined + monaco.languages.InlineCompletions | undefined > { const document = new Document(model); const position = Position.fromMonaco(monacoPosition); @@ -144,7 +121,10 @@ export class MonacoCompletionProvider { .map((completionItem) => this.createInlineCompletionItem(completionItem, document) ) - .filter((item) => !!item); + .filter( + (item?: MonacoInlineCompletion): item is MonacoInlineCompletion => + !!item + ); this.setStatus(Status.SUCCESS); let message = `Generated ${inlineCompletionItems.length} completions`; @@ -154,7 +134,7 @@ export class MonacoCompletionProvider { this.setMessage(message); return { - items: inlineCompletionItems as monaco.languages.InlineCompletion[], + items: inlineCompletionItems, }; } diff --git a/src/components/CodeiumEditor/InlineCompletion.ts b/src/components/CodeiumEditor/InlineCompletion.ts new file mode 100644 index 0000000..7567bcb --- /dev/null +++ b/src/components/CodeiumEditor/InlineCompletion.ts @@ -0,0 +1,26 @@ +import * as monaco from "monaco-editor/esm/vs/editor/editor.api"; + +export default class MonacoInlineCompletion + implements monaco.languages.InlineCompletion +{ + readonly insertText: string; + // TODO(prem): Why is this property needed? + readonly text: string; + readonly range: monaco.IRange; + readonly command: { + id: string; + title: string; + arguments: string[]; + }; + + constructor(insertText: string, range: monaco.IRange, completionId: string) { + this.insertText = insertText; + this.text = insertText; + this.range = range; + this.command = { + id: "codeium.acceptCompletion", + title: "Accept Completion", + arguments: [completionId, insertText], + }; + } +} diff --git a/src/components/CodeiumEditor/InlineCompletionProvider.ts b/src/components/CodeiumEditor/InlineCompletionProvider.ts index ee78883..f2e877d 100644 --- a/src/components/CodeiumEditor/InlineCompletionProvider.ts +++ b/src/components/CodeiumEditor/InlineCompletionProvider.ts @@ -4,6 +4,7 @@ import { PromiseClient } from "@connectrpc/connect"; import { Status } from "./Status"; import { MonacoCompletionProvider } from "./CompletionProvider"; import { LanguageServerService } from "../../api/proto/exa/language_server_pb/language_server_connect"; +import MonacoInlineCompletion from "./InlineCompletion"; declare module "monaco-editor" { namespace editor { @@ -14,7 +15,10 @@ declare module "monaco-editor" { } export class InlineCompletionProvider - implements monaco.languages.InlineCompletionsProvider + implements + monaco.languages.InlineCompletionsProvider< + monaco.languages.InlineCompletions + > { private numCompletionsProvided: number; readonly completionProvider: MonacoCompletionProvider; From a623ca5cc3d895fa38a0ac5ca1474611eaf1cbd9 Mon Sep 17 00:00:00 2001 From: Baalateja Kataru Date: Tue, 26 Dec 2023 23:15:59 +0000 Subject: [PATCH 2/2] Changed TS module resolution to use bundler in order to resolve imports without .ts extensions --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index b27cc05..70fb28b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,7 +15,7 @@ "declarationDir": "types", "sourceMap": true, "outDir": "dist", - "moduleResolution": "node", + "moduleResolution": "Bundler", "allowSyntheticDefaultImports": true, "emitDeclarationOnly": true }