Repository navigation
Expand file tree
/
Copy pathsyntax-colorizer-runtime.js
More file actions
227 lines (187 loc) · 5.69 KB
/
Copy pathsyntax-colorizer-runtime.js
File metadata and controls
227 lines (187 loc) · 5.69 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
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
'use strict';
const { maskNonCode } = require('./brace-colorizer-runtime');
const TYPE_DECLARATION_KEYWORDS = new Set([
'struct',
'class',
'union',
'enum',
'extension',
'interface',
'protocol',
'alias'
]);
const NON_DECLARATION_PREFIXES = new Set([
'return',
'await',
'throw',
'new',
'if',
'for',
'foreach',
'while',
'switch',
'case'
]);
function range(start, end) {
return { start, end };
}
function isIdentifierStart(ch) {
return Boolean(ch) && /[A-Za-z_]/.test(ch);
}
function isIdentifierPart(ch) {
return Boolean(ch) && /[A-Za-z0-9_]/.test(ch);
}
function previousIdentifierRange(code, endExclusive) {
let end = endExclusive;
while (end > 0 && /\s/.test(code[end - 1])) end -= 1;
let start = end;
while (start > 0 && isIdentifierPart(code[start - 1])) start -= 1;
if (start === end || !isIdentifierStart(code[start])) return null;
return range(start, end);
}
function nextNonWhitespace(code, start) {
let i = start;
while (i < code.length && /\s/.test(code[i])) i += 1;
return i;
}
function findGenericCandidate(code, open) {
// C#-style generic lists attach directly to the preceding identifier.
if (open <= 0 || !isIdentifierPart(code[open - 1])) return null;
const owner = previousIdentifierRange(code, open);
if (!owner) return null;
const delimiters = [open];
const identifiers = [];
let depth = 1;
let i = open + 1;
while (i < code.length) {
const ch = code[i];
if (ch === '<' && i > 0 && isIdentifierPart(code[i - 1])) {
depth += 1;
delimiters.push(i);
i += 1;
continue;
}
if (ch === '>') {
depth -= 1;
delimiters.push(i);
if (depth === 0) {
const after = nextNonWhitespace(code, i + 1);
if (after === i + 1 && after < code.length && isIdentifierPart(code[after])) {
return null;
}
return {
open,
close: i,
owner,
delimiters,
identifiers
};
}
if (depth < 0) return null;
i += 1;
continue;
}
if (isIdentifierStart(ch)) {
const start = i;
i += 1;
while (i < code.length && isIdentifierPart(code[i])) i += 1;
identifiers.push({ start, end: i, depth, text: code.slice(start, i) });
continue;
}
// Valid type-list punctuation. Anything expression-like means this was
// almost certainly a comparison/operator rather than a generic list.
if (/\s/.test(ch) || ',.?:[]*&'.includes(ch)) {
i += 1;
continue;
}
if (';{}=+/%!|^'.includes(ch) || ch === '(' || ch === ')') {
return null;
}
i += 1;
}
return null;
}
function collectGenericCandidates(code) {
const result = [];
for (let i = 0; i < code.length; i += 1) {
if (code[i] !== '<') continue;
const candidate = findGenericCandidate(code, i);
if (!candidate) continue;
result.push(candidate);
i = candidate.close;
}
return result;
}
function linePrefix(code, position) {
const lineStart = Math.max(code.lastIndexOf('\n', position - 1), code.lastIndexOf('\r', position - 1)) + 1;
return code.slice(lineStart, position);
}
function isGenericDeclaration(code, candidate) {
const ownerText = code.slice(candidate.owner.start, candidate.owner.end);
const beforeOwner = linePrefix(code, candidate.owner.start).trimEnd();
const typeDecl = beforeOwner.match(/\b([A-Za-z_][A-Za-z0-9_]*)\s*$/);
if (typeDecl && TYPE_DECLARATION_KEYWORDS.has(typeDecl[1])) {
return true;
}
const after = nextNonWhitespace(code, candidate.close + 1);
if (code[after] !== '(') return false;
const prefix = beforeOwner.trim();
if (!prefix || /[.=,([]\s*$/.test(prefix)) return false;
const lastWord = prefix.match(/([A-Za-z_][A-Za-z0-9_]*)\s*$/)?.[1] ?? '';
if (NON_DECLARATION_PREFIXES.has(lastWord)) return false;
// A generic function declaration has something return-type-like before its
// name. A call such as Foo<T>(...) has no such prefix, and obj.Foo<T>(...)
// is rejected above because its prefix ends in '.'.
return /[A-Za-z0-9_>\]]/.test(prefix[prefix.length - 1] ?? '') && ownerText.length > 0;
}
function computeNamespaceRanges(code) {
const ranges = [];
const regex = /\b(?:using|namespace)\s+([A-Za-z_][A-Za-z0-9_]*(?:\s*\.\s*[A-Za-z_][A-Za-z0-9_]*)*)/g;
let match;
while ((match = regex.exec(code)) !== null) {
const qualifiedName = match[1];
const relativeStart = match[0].lastIndexOf(qualifiedName);
const start = match.index + relativeStart;
ranges.push(range(start, start + qualifiedName.length));
}
return ranges;
}
function computeSyntaxColorRanges(source) {
const code = maskNonCode(source);
const namespaceRanges = computeNamespaceRanges(code);
const candidates = collectGenericCandidates(code);
const genericDelimiterRanges = [];
const declaredTypeParameters = new Set();
for (const candidate of candidates) {
for (const offset of candidate.delimiters) {
genericDelimiterRanges.push(range(offset, offset + 1));
}
if (isGenericDeclaration(code, candidate)) {
for (const identifier of candidate.identifiers) {
if (identifier.depth === 1) {
declaredTypeParameters.add(identifier.text);
}
}
}
}
const typeParameterRanges = [];
if (declaredTypeParameters.size > 0) {
for (const candidate of candidates) {
for (const identifier of candidate.identifiers) {
if (declaredTypeParameters.has(identifier.text)) {
typeParameterRanges.push(range(identifier.start, identifier.end));
}
}
}
}
return {
namespaceRanges,
genericDelimiterRanges,
typeParameterRanges
};
}
module.exports = {
collectGenericCandidates,
computeSyntaxColorRanges,
isGenericDeclaration
};