forked from code-hike/codehike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlines.tsx
More file actions
148 lines (135 loc) · 3.6 KB
/
Copy pathlines.tsx
File metadata and controls
148 lines (135 loc) · 3.6 KB
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
import { BlockAnnotation, InternalToken, Tokens } from "./types.js"
export type LineGroup = {
annotation: BlockAnnotation
lines: LinesOrGroups
range: [number, number]
}
export type LineTokens = {
tokens: InternalToken[]
range: [number, number]
lineNumber: number
indentation: number
totalLines: number
}
export type LinesOrGroups = (LineTokens | LineGroup)[]
const TAB_TO_SPACES = " "
export function toLines(tokens: Tokens): LineTokens[] {
const lines = [[]] as InternalToken[][]
const tokenStack = tokens.slice()
let col = 1
while (tokenStack.length) {
const token = tokenStack.shift()!
if (typeof token === "string") {
const [value, ...tail] = token.split("\n")
if (value) {
let start = col
col += value.length
lines[lines.length - 1].push({
value,
range: [start, col],
})
}
if (tail.length) {
lines[lines.length - 1].push({
value: "\n",
range: [col, col + 1],
})
lines.push([])
col = 1
tokenStack.unshift(tail.join("\n"))
}
} else {
const [value, color, rest = {}] = token
let start = col
col += value.length
lines[lines.length - 1].push({
value,
style: { color, ...rest },
range: [start, col],
})
}
}
const totalLines = lines.length
return lines.map((tokens, i) => ({
tokens,
range: [i + 1, i + 1],
lineNumber: i + 1,
indentation:
tokens[0]?.value?.replace(/\t/g, TAB_TO_SPACES).match(/^\s*/)?.[0]
.length || 0,
totalLines,
}))
}
export function toLineGroups(
lines: LineTokens[],
annotations: BlockAnnotation[],
): LinesOrGroups {
let groups = lines as LinesOrGroups
for (let i = annotations.length - 1; i >= 0; i--) {
const annotation = annotations[i]
groups = applyBlockAnnotation(groups, annotation)
}
return groups
}
function applyBlockAnnotation(
lines: LinesOrGroups,
annotation: BlockAnnotation,
): LinesOrGroups {
const { fromLineNumber, toLineNumber } = annotation
const range = [fromLineNumber, toLineNumber]
let groups = splitGroups(lines, range[0])
groups = splitGroups(groups, range[1] + 1)
let before = [] as LinesOrGroups
let inside = [] as LinesOrGroups
let after = [] as LinesOrGroups
groups.forEach((group) => {
if (group.range[1] < range[0]) {
before.push(group)
} else if (group.range[0] > range[1]) {
after.push(group)
} else {
inside.push(group)
}
})
return [
...before,
{
annotation,
lines: inside,
range: [inside[0].range[0], inside[inside.length - 1].range[1]],
},
...after,
]
}
function splitGroups(groups: LinesOrGroups, lineNumber: number): LinesOrGroups {
const index = groups.findIndex((group) => {
return (
isGroup(group) &&
group.range[0] < lineNumber &&
lineNumber <= group.range[1]
)
})
if (index === -1) {
return groups
}
const group = groups[index] as LineGroup
const lines = splitGroups(group.lines, lineNumber)
let before = [] as LinesOrGroups
let after = [] as LinesOrGroups
lines.forEach((lineOrGroup) => {
if (lineOrGroup.range[1] < lineNumber) {
before.push(lineOrGroup)
} else {
after.push(lineOrGroup)
}
})
return [
...groups.slice(0, index),
{ ...group, lines: before, range: [group.range[0], lineNumber - 1] },
{ ...group, lines: after, range: [lineNumber, group.range[1]] },
...groups.slice(index + 1),
]
}
export function isGroup(item: LinesOrGroups[0]): item is LineGroup {
return (item as LineGroup).annotation !== undefined
}