-
Notifications
You must be signed in to change notification settings - Fork 0
/
indentmarkers.js
230 lines (230 loc) · 8.43 KB
/
indentmarkers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(() => {
const {
ViewPlugin,
Decoration,
EditorView,
WidgetType,
} = require("@codemirror/view");
const { getIndentUnit } = require("@codemirror/language");
const { RangeSetBuilder } = require("@codemirror/state");
const indentationMark = Decoration.mark({
class: "cm-indentation-marker",
tagName: "span",
});
/**
* Widget used to simulate N indentation markers on empty lines.
*/
class IndentationWidget extends WidgetType {
constructor(numIndent) {
super();
this.numIndent = numIndent;
}
eq(other) {
return this.numIndent === other.numIndent;
}
toDOM(view) {
const indentSize = getIndentUnit(view.state);
const wrapper = document.createElement("span");
wrapper.style.top = "0";
wrapper.style.left = "4px";
wrapper.style.position = "absolute";
wrapper.style.pointerEvents = "none";
for (let indent = 0; indent < this.numIndent; indent++) {
const element = document.createElement("span");
element.className = "cm-indentation-marker";
element.innerHTML = `${" ".repeat(indentSize)}`;
wrapper.appendChild(element);
}
return wrapper;
}
}
/**
* Returns the number of indentation markers a non-empty line should have
* based on the text in the line and the size of the indent.
*/
function getNumIndentMarkersForNonEmptyLine(
text,
indentSize,
onIndentMarker
) {
let numIndents = 0;
let numConsecutiveSpaces = 0;
let prevChar = null;
for (let char = 0; char < text.length; char++) {
// Bail if we encounter a non-whitespace character
if (text[char] !== " " && text[char] !== "\t") {
// We still increment the indentation level if we would
// have added a marker here had this been a space or tab.
if (numConsecutiveSpaces % indentSize === 0 && char !== 0) {
numIndents++;
}
return numIndents;
}
// Every tab and N space has an indentation marker
const shouldAddIndent =
prevChar === "\t" || numConsecutiveSpaces % indentSize === 0;
if (shouldAddIndent) {
numIndents++;
if (onIndentMarker) {
onIndentMarker(char);
}
}
if (text[char] === " ") {
numConsecutiveSpaces++;
} else {
numConsecutiveSpaces = 0;
}
prevChar = text[char];
}
return numIndents;
}
/**
* Returns the number of indent markers an empty line should have
* based on the number of indent markers of the previous
* and next non-empty lines.
*/
function getNumIndentMarkersForEmptyLine(prev, next) {
const min = Math.min(prev, next);
const max = Math.max(prev, next);
// If only one side is non-zero, we add one marker
// until the next non-empty line.
if (min === 0 && max > 0) {
return 1;
}
// If they're equal and nonzero then
// take one less than the minimum
if (min === max && min > 0) {
return min - 1;
}
// Else, default to the minimum of the two
return min;
}
/**
* Returns the next non-empty line and its indent level.
*/
function findNextNonEmptyLineAndIndentLevel(doc, startLine, indentSize) {
const numLines = doc.lines;
let lineNo = startLine;
while (lineNo <= numLines) {
const { text } = doc.line(lineNo);
if (text.trim().length === 0) {
lineNo++;
continue;
}
const indent = getNumIndentMarkersForNonEmptyLine(text, indentSize);
return [lineNo, indent];
}
// Reached the end of the doc
return [numLines + 1, 0];
}
/**
* Adds indentation markers to all lines within view.
*/
function addIndentationMarkers(view) {
const builder = new RangeSetBuilder();
const indentSize = getIndentUnit(view.state);
const markers = [];
for (const { from, to } of view.visibleRanges) {
let pos = from;
let prevIndentMarkers = 0;
let nextIndentMarkers = 0;
let nextNonEmptyLine = 0;
while (pos <= to) {
const line = view.state.doc.lineAt(pos);
const { text } = line;
// If a line is empty, we match the indentation according
// to a heuristic based on the indentations of the
// previous and next non-empty lines.
if (text.trim().length === 0) {
// To retrieve the next non-empty indentation level,
// we perform a lookahead and cache the result.
if (nextNonEmptyLine < line.number) {
const [nextLine, nextIndent] =
findNextNonEmptyLineAndIndentLevel(
view.state.doc,
line.number + 1,
indentSize
);
nextNonEmptyLine = nextLine;
nextIndentMarkers = nextIndent;
}
const numIndentMarkers = getNumIndentMarkersForEmptyLine(
prevIndentMarkers,
nextIndentMarkers
);
const indentationWidget = Decoration.widget({
widget: new IndentationWidget(numIndentMarkers),
});
// Add the indent widget and move on to next line
markers.push({
from: line.from,
to: line.from,
decoration: indentationWidget,
});
pos = line.to + 1;
continue;
}
prevIndentMarkers = getNumIndentMarkersForNonEmptyLine(
text,
indentSize,
(char) => {
const charPos = line.from + char;
markers.push({
from: charPos,
to: charPos + 1,
decoration: indentationMark,
});
}
);
// Move on to the next line
pos = line.to + 1;
}
}
markers.sort((a, b) => a.from - b.from);
markers.forEach(({ from, to, decoration }) => {
builder.add(from, to, decoration);
});
return builder.finish();
}
function createIndentationMarkerPlugin() {
return ViewPlugin.define(
(view) => ({
decorations: addIndentationMarkers(view),
update(update) {
if (update.docChanged || update.viewportChanged) {
this.decorations = addIndentationMarkers(update.view);
}
},
}),
{
decorations: (v) => v.decorations,
}
);
}
const LIGHT_BACKGROUND =
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAE0lEQVQImWP4////f4bLly//BwAmVgd1/w11/gAAAABJRU5ErkJggg==") left repeat-y';
const DARK_BACKGROUND =
'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAEklEQVQImWNgYGBgYHB3d/8PAAOIAdULw8qMAAAAAElFTkSuQmCC") left repeat-y';
const indentationMarkerBaseTheme = EditorView.baseTheme({
".cm-line": {
position: `relative`,
},
".cm-indentation-marker": {
display: `inline-block`,
},
"&light .cm-indentation-marker": {
background: LIGHT_BACKGROUND,
},
"&dark .cm-indentation-marker": {
background: DARK_BACKGROUND,
},
});
require.addMod("@replit/indent-markers", {
indentationMarkers() {
return [
createIndentationMarkerPlugin(),
indentationMarkerBaseTheme,
];
},
});
})();