Skip to content

Commit

Permalink
Make timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 28, 2024
1 parent 212a370 commit cb6d44c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
27 changes: 18 additions & 9 deletions src/codeium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LanguageServerService } from "./api/proto/exa/language_server_pb/langua
import { createConnectTransport } from "@connectrpc/connect-web";
import { GetCompletionsResponse } from "./api/proto/exa/language_server_pb/language_server_pb.js";
import { CodeiumConfig } from "./config.js";
import { ChangeSpec } from "@codemirror/state";

// This is the same as the monaco editor example
const transport = createConnectTransport({
Expand All @@ -23,7 +24,7 @@ export async function getCodeiumCompletions({
cursorOffset: number;
config: CodeiumConfig;
}) {
const completions = (await client.getCompletions(
return (await client.getCompletions(
{
metadata: {
ideName: "web",
Expand Down Expand Up @@ -58,20 +59,28 @@ export async function getCodeiumCompletions({
},
// TODO: why doesn't this work by default?
)) as GetCompletionsResponse;
}

const parts = completions.completionItems[0]!.completionParts.filter(
(part) => {
// Type 3 overwrites existing text. Maybe we need this eventually,
// but not right now and it usually is duplicative.
return part.type !== 3;
},
).map((part) => {
export function simplifyCompletions(completions: GetCompletionsResponse) {
return completions.completionItems[0]!.completionParts.filter((part) => {
// Type 3 overwrites existing text. Maybe we need this eventually,
// but not right now and it usually is duplicative.
return part.type !== 3;
}).map((part) => {
return {
...part,
offset: Number(part.offset),
text: part.type === 2 ? `\n${part.text}` : part.text,
};
});
}

return parts;
export function completionsToChangeSpec(
completions: GetCompletionsResponse,
): ChangeSpec[] {
return simplifyCompletions(completions).map((part) => ({
from: Number(part.offset),
to: Number(part.offset),
insert: part.text,
}));
}
25 changes: 12 additions & 13 deletions src/completionRequester.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { completionStatus } from "@codemirror/autocomplete";
import { ChangeSet, Transaction } from "@codemirror/state";
import { EditorView, ViewUpdate } from "@codemirror/view";
import { getCodeiumCompletions } from "./codeium.js";
import {
simplifyCompletions,
completionsToChangeSpec,
getCodeiumCompletions,
} from "./codeium.js";
import {
acceptSuggestion,
addSuggestions,
Expand All @@ -11,10 +15,6 @@ import { completionDecoration } from "./completionDecoration.js";
import { copilotEvent } from "./annotations.js";
import { codeiumConfig } from "./config.js";

// milliseconds before cancelling request
// against codeium
const TIMEOUT = 150;

/**
* To request a completion, the document needs to have been
* updated and the update should not have been because
Expand Down Expand Up @@ -91,7 +91,10 @@ export function completionRequester() {
config,
});

if (!completionResult || completionResult.length === 0) {
if (
!completionResult ||
completionResult.completionItems.length === 0
) {
return;
}

Expand All @@ -112,11 +115,7 @@ export function completionRequester() {
// If the completion starts before the end of the line,
// check the end of the line with the end of the completion
const insertChangeSet = ChangeSet.of(
completionResult.map((part) => ({
from: Number(part.offset),
to: Number(part.offset),
insert: part.text,
})),
completionsToChangeSpec(completionResult),
state.doc.length,
);

Expand All @@ -126,7 +125,7 @@ export function completionRequester() {
changes: insertChangeSet,
effects: addSuggestions.of({
reverseChangeSet,
suggestions: completionResult.map((part) => ({
suggestions: simplifyCompletions(completionResult).map((part) => ({
displayText: part.text,
endReplacement: 0, // "",
text: part.text,
Expand All @@ -147,7 +146,7 @@ export function completionRequester() {

await new Promise((resolve) => setTimeout(resolve, 300));
}
}, TIMEOUT);
}, config.timeout);
// Update the last position
lastPos = pos;
});
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import { Facet, combineConfig } from "@codemirror/state";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";

export interface CodeiumConfig {
/**
* Codeium API key
*/
apiKey: string;
language?: Language;
/**
* Time in millseconds after typing to fetch
* completions from codeium
*/
timeout?: number;
}

export const codeiumConfig = Facet.define<
Expand All @@ -15,6 +23,7 @@ export const codeiumConfig = Facet.define<
configs,
{
language: Language.TYPESCRIPT,
timeout: 150,
},
{},
);
Expand Down

0 comments on commit cb6d44c

Please sign in to comment.