2022-10-10 12:50:21 +00:00
|
|
|
import { syntaxTree } from "../common/deps.ts";
|
2022-06-13 16:31:36 +00:00
|
|
|
import {
|
|
|
|
Decoration,
|
|
|
|
DecorationSet,
|
|
|
|
EditorView,
|
|
|
|
ViewPlugin,
|
|
|
|
ViewUpdate,
|
2022-10-10 12:50:21 +00:00
|
|
|
} from "../common/deps.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-10-10 12:50:21 +00:00
|
|
|
import { Range } from "./deps.ts";
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
interface WrapElement {
|
|
|
|
selector: string;
|
|
|
|
class: string;
|
|
|
|
nesting?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapLines(view: EditorView, wrapElements: WrapElement[]) {
|
|
|
|
let widgets: Range<Decoration>[] = [];
|
2022-10-17 13:48:21 +00:00
|
|
|
const elementStack: string[] = [];
|
2022-08-02 10:44:41 +00:00
|
|
|
const doc = view.state.doc;
|
|
|
|
// Disabling the visible ranges for now, because it may be a bit buggy.
|
|
|
|
// RISK: this may actually become slow for large documents.
|
2022-10-17 13:48:21 +00:00
|
|
|
for (const { from, to } of view.visibleRanges) {
|
2022-09-06 12:36:31 +00:00
|
|
|
syntaxTree(view.state).iterate({
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
enter: ({ type, from, to }) => {
|
2022-10-17 13:48:21 +00:00
|
|
|
for (const wrapElement of wrapElements) {
|
2022-09-06 12:36:31 +00:00
|
|
|
if (type.name == wrapElement.selector) {
|
2022-03-20 08:56:28 +00:00
|
|
|
if (wrapElement.nesting) {
|
2022-09-06 12:36:31 +00:00
|
|
|
elementStack.push(type.name);
|
|
|
|
}
|
|
|
|
const bodyText = doc.sliceString(from, to);
|
|
|
|
let idx = from;
|
2022-10-17 13:48:21 +00:00
|
|
|
for (const line of bodyText.split("\n")) {
|
2022-09-06 12:36:31 +00:00
|
|
|
let cls = wrapElement.class;
|
|
|
|
if (wrapElement.nesting) {
|
|
|
|
cls = `${cls} ${cls}-${elementStack.length}`;
|
|
|
|
}
|
|
|
|
widgets.push(
|
|
|
|
Decoration.line({
|
|
|
|
class: cls,
|
2022-10-10 12:50:21 +00:00
|
|
|
}).range(doc.lineAt(idx).from),
|
2022-09-06 12:36:31 +00:00
|
|
|
);
|
|
|
|
idx += line.length + 1;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 12:36:31 +00:00
|
|
|
},
|
|
|
|
leave({ type }) {
|
2022-10-17 13:48:21 +00:00
|
|
|
for (const wrapElement of wrapElements) {
|
2022-09-06 12:36:31 +00:00
|
|
|
if (type.name == wrapElement.selector && wrapElement.nesting) {
|
|
|
|
elementStack.pop();
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-09-06 12:36:31 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-03-20 08:56:28 +00:00
|
|
|
// Widgets have to be sorted by `from` in ascending order
|
|
|
|
widgets = widgets.sort((a, b) => {
|
|
|
|
return a.from < b.from ? -1 : 1;
|
|
|
|
});
|
|
|
|
return Decoration.set(widgets);
|
|
|
|
}
|
2022-09-06 12:36:31 +00:00
|
|
|
|
2022-03-20 08:56:28 +00:00
|
|
|
export const lineWrapper = (wrapElements: WrapElement[]) =>
|
|
|
|
ViewPlugin.fromClass(
|
|
|
|
class {
|
|
|
|
decorations: DecorationSet;
|
|
|
|
|
|
|
|
constructor(view: EditorView) {
|
|
|
|
this.decorations = wrapLines(view, wrapElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
update(update: ViewUpdate) {
|
|
|
|
if (update.docChanged || update.viewportChanged) {
|
|
|
|
this.decorations = wrapLines(update.view, wrapElements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
decorations: (v) => v.decorations,
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
2022-03-20 08:56:28 +00:00
|
|
|
);
|