Skip to content

Commit

Permalink
Accept suggestions by clicking
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Mar 28, 2024
1 parent 0476627 commit ab09b8a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 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;
}
29 changes: 26 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ 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) {
console.log(head);
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 +39,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 ab09b8a

Please sign in to comment.