Skip to content

Commit

Permalink
Split otherDocuments configuration from the rest (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw authored Apr 10, 2024
1 parent da32777 commit c6b55da
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
16 changes: 12 additions & 4 deletions demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { EditorView, basicSetup } from "codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { Language, copilotPlugin } from "../src/plugin.js";
import {
codeiumOtherDocumentsConfig,
Language,
copilotPlugin,
} from "../src/plugin.js";
import { python } from "@codemirror/lang-python";

new EditorView({
Expand All @@ -17,17 +21,21 @@ increment('not a number');`,
typescript: true,
jsx: true,
}),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
codeiumOtherDocumentsConfig.of({
otherDocuments: [
{
absolutePath: "https://esm.town/v/foo.ts",
text: "export const foo = 10;",
text: `export const foo = 10;
const hiddenValue = "https://macwright.com/"`,
language: Language.TYPESCRIPT,
editorLanguage: "typescript",
},
],
}),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
}),
],
parent: document.querySelector("#editor")!,
});
Expand Down
10 changes: 8 additions & 2 deletions src/codeium.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
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 { GetCompletionsResponse } from "./api/proto/exa/language_server_pb/language_server_pb.js";
import {
Document,
GetCompletionsResponse,
} from "./api/proto/exa/language_server_pb/language_server_pb.js";
import { CodeiumConfig } from "./config.js";
import { ChangeSpec } from "@codemirror/state";
import { type PartialMessage } from "@bufbuild/protobuf";

// This is the same as the monaco editor example
const transport = createConnectTransport({
Expand All @@ -19,10 +23,12 @@ export async function getCodeiumCompletions({
text,
cursorOffset,
config,
otherDocuments,
}: {
text: string;
cursorOffset: number;
config: CodeiumConfig;
otherDocuments: PartialMessage<Document>[];
}) {
return (await client.getCompletions(
{
Expand All @@ -49,7 +55,7 @@ export async function getCodeiumCompletions({
tabSize: 2n,
insertSpaces: true,
},
otherDocuments: config.otherDocuments,
otherDocuments: otherDocuments,
multilineConfig: undefined,
},
{
Expand Down
8 changes: 5 additions & 3 deletions src/completionRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "./effects.js";
import { completionDecoration } from "./completionDecoration.js";
import { copilotEvent, copilotIgnore } from "./annotations.js";
import { codeiumConfig } from "./config.js";
import { codeiumConfig, codeiumOtherDocumentsConfig } from "./config.js";

/**
* To request a completion, the document needs to have been
Expand Down Expand Up @@ -61,6 +61,9 @@ export function completionRequester() {

return EditorView.updateListener.of((update: ViewUpdate) => {
const config = update.view.state.facet(codeiumConfig);
const { otherDocuments } = update.view.state.facet(
codeiumOtherDocumentsConfig,
);

if (!shouldRequestCompletion(update)) return;

Expand Down Expand Up @@ -89,6 +92,7 @@ export function completionRequester() {
text: source,
cursorOffset: pos,
config,
otherDocuments,
});

if (
Expand Down Expand Up @@ -129,8 +133,6 @@ export function completionRequester() {
suggestions: simplifyCompletions(completionResult).map((part) => {
try {
return {
displayText: part.text,
endReplacement: 0, // "",
text: part.text,
cursorPos: pos,
startPos: combinedOffset + Number(part.offset),
Expand Down
20 changes: 18 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export interface CodeiumConfig {
timeout?: number;

authSource?: number;

otherDocuments?: PartialMessage<Document>[];
}

export const codeiumConfig = Facet.define<
Expand All @@ -30,6 +28,24 @@ export const codeiumConfig = Facet.define<
{
language: Language.TYPESCRIPT,
timeout: 150,
},
{},
);
},
});

export interface CodeiumOtherDocumentsConfig {
otherDocuments?: PartialMessage<Document>[];
}

export const codeiumOtherDocumentsConfig = Facet.define<
CodeiumOtherDocumentsConfig,
Required<CodeiumOtherDocumentsConfig>
>({
combine(configs) {
return combineConfig<Required<CodeiumOtherDocumentsConfig>>(
configs,
{
otherDocuments: [],
},
{},
Expand Down
8 changes: 6 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
rejectSuggestionCommand,
acceptSuggestionCommand,
} from "./commands.js";
import { CodeiumConfig, codeiumConfig } from "./config.js";
import {
CodeiumConfig,
codeiumConfig,
codeiumOtherDocumentsConfig,
} from "./config.js";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";
import { copilotIgnore } from "./annotations.js";

Expand Down Expand Up @@ -56,7 +60,7 @@ function viewCompletionPlugin() {
});
}

export { Language, copilotIgnore };
export { Language, copilotIgnore, codeiumConfig, codeiumOtherDocumentsConfig };

export function copilotPlugin(config: CodeiumConfig): Extension {
return [
Expand Down
4 changes: 0 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { type DecorationSet } from "@codemirror/view";

export interface Suggestion {
text: string;
displayText: string;
cursorPos: number;
startPos: number;
endPos: number;
endReplacement: number;
}

export type CompletionState = null | {
Expand All @@ -17,11 +15,9 @@ export type CompletionState = null | {

export interface GhostText {
text: string;
displayText: string;
displayPos: number;
startPos: number;
endGhostText: number;
endReplacement: number;
endPos: number;
decorations: DecorationSet;
}

0 comments on commit c6b55da

Please sign in to comment.