|
1 | | -import { Transaction, EditorSelection } from "@codemirror/state"; |
2 | | -import type { EditorView } from "@codemirror/view"; |
| 1 | +import { Transaction, EditorSelection, ChangeSet } from "@codemirror/state"; |
| 2 | +import type { Command, EditorView } from "@codemirror/view"; |
3 | 3 | import { copilotEvent, copilotIgnore } from "./annotations.js"; |
4 | 4 | import { completionDecoration } from "./completionDecoration.js"; |
5 | | -import { acceptSuggestion, clearSuggestion } from "./effects.js"; |
| 5 | +import { |
| 6 | + acceptSuggestion, |
| 7 | + addSuggestions, |
| 8 | + clearSuggestion, |
| 9 | +} from "./effects.js"; |
6 | 10 |
|
7 | 11 | /** |
8 | 12 | * Accepting a suggestion: we remove the ghost text, which |
@@ -53,6 +57,79 @@ export function acceptSuggestionCommand(view: EditorView) { |
53 | 57 | return true; |
54 | 58 | } |
55 | 59 |
|
| 60 | +/** |
| 61 | + * Cycle through suggested AI completed code. |
| 62 | + */ |
| 63 | +export const nextSuggestionCommand: Command = (view: EditorView) => { |
| 64 | + const { state } = view; |
| 65 | + // We delete the suggestion, then carry through with the original keypress |
| 66 | + const stateField = state.field(completionDecoration); |
| 67 | + |
| 68 | + if (!stateField) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + const { changeSpecs } = stateField; |
| 73 | + |
| 74 | + if (changeSpecs.length < 2) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + // Loop through next suggestion. |
| 79 | + const index = (stateField.index + 1) % changeSpecs.length; |
| 80 | + const nextSpec = changeSpecs.at(index); |
| 81 | + if (!nextSpec) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * First, get the original document, by applying the stored |
| 87 | + * reverse changeset against the currently-displayed document. |
| 88 | + */ |
| 89 | + const originalDocument = stateField.reverseChangeSet.apply(state.doc); |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the changeset that we will apply that will |
| 93 | + * |
| 94 | + * 1. Reverse the currently-displayed suggestion, to get us back to |
| 95 | + * the original document |
| 96 | + * 2. Apply the next suggestion. |
| 97 | + * |
| 98 | + * It does both in the same changeset, so there is no flickering. |
| 99 | + */ |
| 100 | + const reverseCurrentSuggestionAndApplyNext = ChangeSet.of( |
| 101 | + stateField.reverseChangeSet, |
| 102 | + state.doc.length, |
| 103 | + ).compose(ChangeSet.of(nextSpec, originalDocument.length)); |
| 104 | + |
| 105 | + /** |
| 106 | + * Generate the next changeset |
| 107 | + */ |
| 108 | + const nextSet = ChangeSet.of(nextSpec, originalDocument.length); |
| 109 | + const reverseChangeSet = nextSet.invert(originalDocument); |
| 110 | + |
| 111 | + view.dispatch({ |
| 112 | + changes: reverseCurrentSuggestionAndApplyNext, |
| 113 | + effects: addSuggestions.of({ |
| 114 | + index, |
| 115 | + reverseChangeSet, |
| 116 | + changeSpecs, |
| 117 | + }), |
| 118 | + annotations: [ |
| 119 | + // Tell upstream integrations to ignore this |
| 120 | + // change. |
| 121 | + copilotIgnore.of(null), |
| 122 | + // Tell ourselves not to request a completion |
| 123 | + // because of this change. |
| 124 | + copilotEvent.of(null), |
| 125 | + // Don't add this to history. |
| 126 | + Transaction.addToHistory.of(false), |
| 127 | + ], |
| 128 | + }); |
| 129 | + |
| 130 | + return true; |
| 131 | +}; |
| 132 | + |
56 | 133 | /** |
57 | 134 | * Rejecting a suggestion: this looks at the currently-shown suggestion |
58 | 135 | * and reverses it, clears the suggestion, and makes sure |
|
0 commit comments