-
Notifications
You must be signed in to change notification settings - Fork 2
/
guides.ts
132 lines (111 loc) · 3.95 KB
/
guides.ts
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
/** @module guides */
import { createTemplate } from "../core.js"
import { Extension, PrismEditor } from "../types.js"
const template = createTemplate(
'<div class=guide-indents style=left:var(--padding-left)><div style=position:relative;display:inline-block> ',
)
const indentTemplate = createTemplate(
'<div style=width:1px;position:absolute;background:var(--bg-guide-indent)>',
)
export interface IndentGuides extends Extension {
/** Collection of all the guide lines. */
readonly lines: HTMLCollectionOf<HTMLDivElement>
/** Indentation for each line. Is -1 for lines with only whitespace. */
readonly indentLevels: number[]
}
/** Extension adding indent guides to an editor. Does not work with word wrap. */
export const indentGuides = (): IndentGuides => {
let tabSize: number,
prevLength = 0,
lineIndentMap: number[],
active = -1,
currentEditor: PrismEditor
const lines: HTMLDivElement[] = [],
indents: number[][] = [],
container = template(),
guideHeight = <HTMLDivElement>container.lastChild,
indentLevels: number[] = []
const update = (code: string) => {
lineIndentMap = []
const newIndents = getIndents(code.split("\n")),
l = newIndents.length
for (let i = 0, prev: number[] = [], next = newIndents[0]; next; i++) {
const style = (lines[i] ||= indentTemplate()).style,
[top, height, left] = next,
old = indents[i]
next = newIndents[i + 1]
if (top != old?.[0]) style.top = top + "00%"
if (height != old?.[1]) style.height = height + "00%"
if (left != old?.[2]) style.left = left * 100 + "%"
const isSingleIndent = prev[0] != top && next?.[0] != top,
isSingleOutdent = prev[0] + prev[1] != top + height && next?.[0] + next?.[1] != top + height
for (let j = -isSingleIndent, l = height + <any>isSingleOutdent; j < l; j++)
lineIndentMap[j + top] = i
prev = indents[i] = newIndents[i]
}
for (let i = prevLength; i > l; ) lines[--i].remove()
guideHeight.append(...lines.slice(prevLength, (prevLength = l)))
}
const updateActive = () => {
const newActive = lineIndentMap[currentEditor.activeLineNumber - 1] ?? -1
if (newActive != active) {
active > -1 && (lines[active].className = "")
newActive > -1 && (lines[newActive].className = "active")
}
active = newActive
}
const getIndents = (lines: string[]) => {
const l = lines.length,
stack: number[][] = [],
results: number[][] = []
for (let prevIndent = 0, emptyPos = -1, i = 0, p = 0; ; i++) {
const last = i == l,
indent = last ? 0 : (indentLevels[i] = getIndentCount(lines[i]))
if (indent < 0) {
if (emptyPos < 0) emptyPos = i
} else {
for (let j = indent; j < prevIndent; j++) {
// Updating height of the closed lines
stack[j][1] = (emptyPos < 0 || (j == indent && !last) ? i : emptyPos) - stack[j][0]
}
for (let j = prevIndent; j < indent; ) {
// Adding new indentation lines
results[p++] = stack[j] = [
emptyPos < 0 || j > prevIndent ? i : emptyPos,
0,
j++ * tabSize,
]
}
emptyPos = -1
prevIndent = indent
}
if (last) break
}
indentLevels.length = l
return results
}
const getIndentCount = (text: string) => {
let l = text.search(/\S/),
result = 0
if (l < 0) return l
for (let i = 0; i < l; ) {
result += text[i++] == "\t" ? tabSize - (result % tabSize) : 1
}
return Math.ceil(result / tabSize)
}
return {
lines: <HTMLCollectionOf<HTMLDivElement>>guideHeight.children,
indentLevels,
update(editor, options) {
if (!currentEditor) {
currentEditor = editor
editor.extensions.indentGuides = this
editor.overlays.append(container)
editor.addListener("update", update)
editor.addListener("selectionChange", updateActive)
}
container.style.display = options.wordWrap ? "none" : ""
if (tabSize != (tabSize = options.tabSize || 2)) update(editor.value), updateActive()
},
}
}