Skip to content

Commit

Permalink
Merge pull request #12 from val-town/accept-suggestions-clicking
Browse files Browse the repository at this point in the history
Accept suggestions by clicking
  • Loading branch information
tmcw authored Mar 28, 2024
2 parents 0476627 + a91b6dc commit 212a370
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ import { copilotPlugin } from "@val-town/codemirror-codeium";
// This is a CodeMirror extension
copilotPlugin();
```

### CSS

This adds a `.ghostText` class to CodeMirror decorations for the AI-written
text. You can add your own style for this class. The demo uses this style:

```css
.cm-ghostText,
.cm-ghostText * {
opacity: 0.6;
filter: grayscale(20%);
cursor: pointer;
}

.cm-ghostText:hover {
background: #eee;
}
```
6 changes: 5 additions & 1 deletion demo/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ main {

.cm-ghostText,
.cm-ghostText * {
/* color: rgb(120, 120, 120, 0.8) !important; */
opacity: 0.6;
filter: grayscale(20%);
cursor: pointer;
}

.cm-ghostText:hover {
background: #eee;
}
2 changes: 0 additions & 2 deletions src/completionDecoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export const completionDecoration = StateField.define<CompletionState>({
update(state: CompletionState, transaction: Transaction) {
for (const effect of transaction.effects) {
if (effect.is(addSuggestions)) {
// When adding a suggestion, we set th ghostText

// NOTE: here we're adjusting the decoration range
// to refer to locations in the document _after_ we've
// inserted the text.
Expand Down
28 changes: 25 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@ import { EditorView } from "@codemirror/view";
import { Extension, Prec } from "@codemirror/state";
import { completionDecoration } from "./completionDecoration.js";
import { completionRequester } from "./completionRequester.js";
import { sameKeyCommand, rejectSuggestionCommand } from "./commands.js";
import {
sameKeyCommand,
rejectSuggestionCommand,
acceptSuggestionCommand,
} from "./commands.js";
import { CodeiumConfig, codeiumConfig } from "./config.js";
import { Language } from "./api/proto/exa/codeium_common_pb/codeium_common_pb.js";

function isDecorationClicked(view: EditorView) {
let inRange = false;
const head = view.state.selection.asSingle().ranges.at(0)?.head;
if (head !== undefined) {
view.state
.field(completionDecoration)
.decorations?.between(head, head, () => {
inRange = true;
});
return inRange;
}
return false;
}

function completionPlugin() {
return EditorView.domEventHandlers({
keydown(event, view) {
Expand All @@ -20,8 +38,12 @@ function completionPlugin() {
return false;
}
},
mousedown(_event, view) {
return rejectSuggestionCommand(view);
mouseup(_event, view) {
if (isDecorationClicked(view)) {
return acceptSuggestionCommand(view);
} else {
return rejectSuggestionCommand(view);
}
},
});
}
Expand Down

0 comments on commit 212a370

Please sign in to comment.