Skip to content

Commit

Permalink
Add ignore annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Apr 2, 2024
1 parent 46a3a05 commit 8c799d4
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 8 deletions.
8 changes: 8 additions & 0 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ increment('not a number');`,
}),
copilotPlugin({
apiKey: "d49954eb-cfba-4992-980f-d8fb37f0e942",
otherDocuments: [
{
absolutePath: "https://esm.town/v/foo.ts",
text: "export const foo = 10;",
language: Language.TYPESCRIPT,
editorLanguage: "typescript",
},
],
}),
],
parent: document.querySelector("#editor")!,
Expand Down
7 changes: 7 additions & 0 deletions src/annotations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { Annotation } from "@codemirror/state";

export const copilotEvent = Annotation.define<null>();

/**
* Annotation that signals to upstream integrations
* that this transaction should not be included
* in history or treated otherwise as a user edit.
*/
export const copilotIgnore = Annotation.define<null>();
2 changes: 1 addition & 1 deletion src/codeium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function getCodeiumCompletions({
tabSize: 2n,
insertSpaces: true,
},
otherDocuments: [],
otherDocuments: config.otherDocuments,
multilineConfig: undefined,
},
{
Expand Down
25 changes: 20 additions & 5 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Transaction, EditorSelection } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { copilotEvent } from "./annotations.js";
import { copilotEvent, copilotIgnore } from "./annotations.js";
import { completionDecoration } from "./completionDecoration.js";
import { acceptSuggestion, clearSuggestion } from "./effects.js";

Expand All @@ -18,12 +18,20 @@ export function acceptSuggestionCommand(view: EditorView) {
view.state.doc,
);

// This is removing the previous ghost text. Don't
// add this to history.
// This is removing the previous ghost text.
view.dispatch({
changes: stateField.reverseChangeSet,
effects: acceptSuggestion.of(null),
annotations: [copilotEvent.of(null), Transaction.addToHistory.of(false)],
annotations: [
// Tell upstream integrations to ignore this
// change.
copilotIgnore.of(null),
// Tell ourselves not to request a completion
// because of this change.
copilotEvent.of(null),
// Don't add this to history.
Transaction.addToHistory.of(false),
],
});

let lastIndex = 0;
Expand Down Expand Up @@ -52,7 +60,14 @@ export function rejectSuggestionCommand(view: EditorView) {
view.dispatch({
changes: stateField.reverseChangeSet,
effects: clearSuggestion.of(null),
annotations: [copilotEvent.of(null), Transaction.addToHistory.of(false)],
annotations: [
// Tell upstream integrations to ignore this
// change. This was never really in the document
// in the first place - we were just showing ghost text.
copilotIgnore.of(null),
copilotEvent.of(null),
Transaction.addToHistory.of(false),
],
});

return false;
Expand Down
3 changes: 2 additions & 1 deletion src/completionRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
clearSuggestion,
} from "./effects.js";
import { completionDecoration } from "./completionDecoration.js";
import { copilotEvent } from "./annotations.js";
import { copilotEvent, copilotIgnore } from "./annotations.js";
import { codeiumConfig } from "./config.js";

/**
Expand Down Expand Up @@ -135,6 +135,7 @@ export function completionRequester() {
})),
}),
annotations: [
copilotIgnore.of(null),
copilotEvent.of(null),
Transaction.addToHistory.of(false),
],
Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Facet, combineConfig } from "@codemirror/state";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";
import { Document } from "./api/proto/exa/language_server_pb/language_server_pb.js";
import { type PartialMessage } from "@bufbuild/protobuf";

export interface CodeiumConfig {
/**
Expand All @@ -14,6 +16,8 @@ export interface CodeiumConfig {
timeout?: number;

authSource?: number;

otherDocuments?: PartialMessage<Document>[];
}

export const codeiumConfig = Facet.define<
Expand All @@ -26,6 +30,7 @@ export const codeiumConfig = Facet.define<
{
language: Language.TYPESCRIPT,
timeout: 150,
otherDocuments: [],
},
{},
);
Expand Down
18 changes: 17 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EditorView } from "@codemirror/view";
import { EditorView, ViewUpdate } from "@codemirror/view";
import { Extension, Prec } from "@codemirror/state";
import { completionDecoration } from "./completionDecoration.js";
import { completionRequester } from "./completionRequester.js";
Expand All @@ -9,6 +9,7 @@ import {
} from "./commands.js";
import { CodeiumConfig, codeiumConfig } from "./config.js";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";
import { copilotIgnore } from "./annotations.js";

function isDecorationClicked(view: EditorView) {
let inRange = false;
Expand Down Expand Up @@ -67,3 +68,18 @@ export function copilotPlugin(config: CodeiumConfig): Extension {
completionRequester(),
];
}

/**
* Returns false if this ViewUpdate is just the plugin
* adding or removing ghost text, and it should not be
* considered when saving this CodeMirror state into other
* systems, like draft recovery.
*/
export function shouldTakeUpdate(update: ViewUpdate) {
for (const tr of update.transactions) {
if (tr.annotation(copilotIgnore) !== undefined) {
return false;
}
}
return true;
}

0 comments on commit 8c799d4

Please sign in to comment.