Fixes #164: Rewrote all CM view plugins to statefields

This commit is contained in:
Zef Hemel
2022-12-09 16:12:55 +01:00
parent 453c613ef4
commit c8c4271aeb
16 changed files with 433 additions and 739 deletions
+22 -15
View File
@@ -3,11 +3,12 @@
// License: Apache License 2.0.
import {
Decoration,
DecorationSet,
EditorState,
EditorView,
foldedRanges,
SyntaxNodeRef,
syntaxTree,
StateField,
Transaction,
WidgetType,
} from "../deps.ts";
@@ -39,6 +40,25 @@ export class LinkWidget extends WidgetType {
}
}
export function decoratorStateField(
stateToDecoratorMapper: (state: EditorState) => DecorationSet,
) {
return StateField.define<DecorationSet>({
create(state: EditorState) {
return stateToDecoratorMapper(state);
},
update(value: DecorationSet, tr: Transaction) {
// if (tr.docChanged || tr.selection) {
return stateToDecoratorMapper(tr.state);
// }
// return value;
},
provide: (f) => EditorView.decorations.from(f),
});
}
export class ButtonWidget extends WidgetType {
constructor(
readonly text: string,
@@ -106,19 +126,6 @@ export function isCursorInRange(state: EditorState, range: [number, number]) {
*/
export const invisibleDecoration = Decoration.replace({});
export function iterateTreeInVisibleRanges(
view: EditorView,
iterateFns: {
enter(node: SyntaxNodeRef): boolean | void;
leave?(node: SyntaxNodeRef): void;
},
) {
// for (const { from, to } of view.visibleRanges) {
// syntaxTree(view.state).iterate({ ...iterateFns, from, to });
// }
syntaxTree(view.state).iterate(iterateFns);
}
/**
* Returns the lines of the editor that are in the given range and not folded.
* This function is of use when you need to get the lines of a particular