-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdiff.ts
60 lines (53 loc) · 1.29 KB
/
diff.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
import type { Grammar, LanguageProto } from '../types';
/**
* A map from the name of a block to its line prefix.
*/
export const PREFIXES = {
'deleted-sign': '-',
'deleted-arrow': '<',
'inserted-sign': '+',
'inserted-arrow': '>',
'unchanged': ' ',
'diff': '!',
};
export default {
id: 'diff',
grammar () {
const diff: Grammar = {
'coord': [
// Match all kinds of coord lines (prefixed by "+++", "---" or "***").
/^(?:\*{3}|-{3}|\+{3}).*$/m,
// Match "@@ ... @@" coord lines in unified diff.
/^@@.*@@$/m,
// Match coord lines in normal diff (starts with a number).
/^\d.*$/m,
],
// deleted, inserted, unchanged, diff
};
// add a token for each prefix
Object.keys(PREFIXES).forEach(name => {
const prefix = PREFIXES[name as keyof typeof PREFIXES];
const alias = [];
const mainName = /\w+/.exec(name)?.[0] || name;
if (mainName !== name) {
// "deleted-sign" -> "deleted"
alias.push(mainName);
}
if (name === 'diff') {
alias.push('bold');
}
diff[name] = {
pattern: RegExp(`^(?:[${prefix}].*(?:\r\n?|\n|(?![\\s\\S])))+`, 'm'),
alias,
inside: {
'prefix': {
pattern: RegExp(`^[${prefix}]`, 'm'),
greedy: true,
alias: mainName,
},
},
};
});
return diff;
},
} as LanguageProto<'diff'>;