@valtown/codemirror-codeium
Version:
codemirror integration for codeium
58 lines • 2.67 kB
JavaScript
import { StateField } from "@codemirror/state";
import { Decoration, EditorView } from "@codemirror/view";
import { addSuggestions, acceptSuggestion, clearSuggestion, } from "./effects.js";
const ghostMark = Decoration.mark({ class: "cm-ghostText" });
export const completionDecoration = StateField.define({
create(_state) {
return { ghostTexts: null };
},
update(state, transaction) {
for (const effect of transaction.effects) {
if (effect.is(addSuggestions)) {
// NOTE: here we're adjusting the decoration range
// to refer to locations in the document _after_ we've
// inserted the text.
let decorationOffset = 0;
const decorations = Decoration.set(effect.value.suggestions.map((suggestion) => {
const endGhostText = suggestion.cursorPos + suggestion.displayText.length;
let range = ghostMark.range(decorationOffset + suggestion.cursorPos, decorationOffset + endGhostText);
decorationOffset += suggestion.displayText.length;
return range;
}));
// TODO
return {
decorations,
reverseChangeSet: effect.value.reverseChangeSet,
ghostTexts: effect.value.suggestions.map((suggestion) => {
const endGhostText = suggestion.cursorPos + suggestion.displayText.length;
return {
text: suggestion.text,
displayText: suggestion.text,
startPos: suggestion.startPos,
endPos: suggestion.endPos,
decorations,
// TODO: what's the difference between this
// and startPos?
displayPos: suggestion.cursorPos,
endReplacement: suggestion.endReplacement,
endGhostText,
};
}),
};
}
else if (effect.is(acceptSuggestion)) {
if (state.ghostTexts) {
return { ghostTexts: null };
}
}
else if (effect.is(clearSuggestion)) {
return { ghostTexts: null };
}
}
return state;
},
provide: (field) => EditorView.decorations.from(field, (value) => {
return value.decorations || Decoration.none;
}),
});
//# sourceMappingURL=completionDecoration.js.map