Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 95 additions & 96 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
"escape-string-regexp": "^5.0.0",
"esprima": "^4.0.1",
"filesize": "^11.0.17",
"htm": "^3.1.1",
"html-tag-js": "^2.4.16",
"jszip": "^3.10.1",
"katex": "^0.16.45",
Expand All @@ -193,6 +194,7 @@
"motion": "^12.42.0",
"mustache": "^4.2.0",
"picomatch": "^4.0.4",
"preact": "^10.29.8",
"url-parse": "^1.5.10",
"vanilla-picker": "^2.12.3",
"vscode-css-languageservice": "6.3.10",
Expand Down
123 changes: 123 additions & 0 deletions src/cm/extensions/aiAutocomplete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { ViewPlugin, Decoration, WidgetType, EditorView } from "@codemirror/view";
import { StateField, StateEffect } from "@codemirror/state";

class GhostTextWidget extends WidgetType {
constructor(public text: string) {
super();
}

eq(other: GhostTextWidget) {
return this.text === other.text;
}

toDOM() {
const span = document.createElement("span");
span.className = "cm-ai-ghost-text";
span.textContent = this.text;
span.style.opacity = "0.5";
span.style.fontStyle = "italic";
return span;
}
}

const setGhostText = StateEffect.define<{ pos: number; text: string } | null>();

export const ghostTextState = StateField.define<Decoration>({
create() {
return Decoration.none;
},
update(decorations, tr) {
decorations = decorations.map(tr.changes);
for (let e of tr.effects) {
if (e.is(setGhostText)) {
if (e.value === null) {
return Decoration.none;
}
return Decoration.set([
Decoration.widget({
widget: new GhostTextWidget(e.value.text),
side: 1,
}).range(e.value.pos),
]);
}
}
// If document changed, clear ghost text
if (tr.docChanged) {
return Decoration.none;
}
return decorations;
},
provide: (f) => EditorView.decorations.from(f),
});

export const aiAutocompletePlugin = ViewPlugin.fromClass(
class {
timeout: any;
constructor(public view: any) {}

update(update: any) {
if (update.docChanged) {
import("lib/settings").then((mSettings) => {
const appSettings = mSettings.default;
if (appSettings.value.ai?.provider) {
clearTimeout(this.timeout);
this.view.dispatch({ effects: setGhostText.of(null) });

this.timeout = setTimeout(async () => {
try {
const pos = this.view.state.selection.main.head;
const doc = this.view.state.doc.toString();
const prefix = doc.slice(0, pos);
const suffix = doc.slice(pos);

const { default: aiService } = await import("utils/ai/AIService");
const completion = await aiService.completeCode(prefix, suffix);
if (completion) {
this.view.dispatch({
effects: setGhostText.of({ pos, text: completion }),
});
}
} catch (e) {
// Ignore errors or log them
}
}, 1000); // 1s debounce
}
}).catch(() => {});
}
}

destroy() {
clearTimeout(this.timeout);
}
}
);

// We also need a keymap to accept the suggestion
import { keymap } from "@codemirror/view";
export const acceptAiSuggestionKeymap = keymap.of([
{
key: "Tab",
run: (view) => {
const ghost = view.state.field(ghostTextState, false);
if (ghost && ghost.size > 0) {
let textToInsert = "";
ghost.between(0, view.state.doc.length, (from, to, deco) => {
textToInsert = (deco.spec.widget as GhostTextWidget).text;
});
if (textToInsert) {
const pos = view.state.selection.main.head;
view.dispatch({
changes: { from: pos, insert: textToInsert },
Comment on lines +108 to +110

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Stale completion uses current cursor

When a ghost completion is visible and the user moves the cursor or changes the selection without editing, the plugin retains the decoration but inserts its text at the current selection head, causing a suggestion generated for one location to modify a different part of the document.

Knowledge Base Used: Editor Core (CodeMirror)

effects: setGhostText.of(null),
});
return true;
}
}
return false;
},
},
]);

export function aiAutocomplete() {
return [ghostTextState, aiAutocompletePlugin, acceptAiSuggestionKeymap];
}
4 changes: 4 additions & 0 deletions src/cm/mainEditorExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Extension } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { aiAutocomplete } from "./extensions/aiAutocomplete";

interface MainEditorExtensionOptions {
emmetExtensions?: Extension[];
Expand Down Expand Up @@ -54,6 +55,9 @@ export function createMainEditorExtensions(
pushExtension(extensions, options.searchExtension);
pushExtension(extensions, options.readOnlyExtension);

// Add AI Autocomplete
extensions.push(aiAutocomplete());

if (options.optionExtensions?.length) {
extensions.push(...options.optionExtensions);
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ class Settings {
workspaces: {},
},
},
ai: {
provider: "openai",
model: "gpt-4o-mini",
openaiKey: "",
openRouterKey: "",
},
developerMode: false,
shiftClickSelection: true,
showShareButton: true,
Expand Down
Loading
Loading