git-tm 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -2
- package/package.json +2 -2
- package/src/cli.js +76 -9
- package/src/git.js +73 -0
- package/src/public/app.css +288 -1
- package/src/public/index.html +3 -1
- package/src/public/js/detail.js +8 -3
- package/src/public/js/diff.js +78 -32
- package/src/public/js/highlight.js +481 -0
- package/src/public/js/insights.js +2 -2
- package/src/public/js/main.js +9 -1
- package/src/public/js/reflog.js +215 -0
- package/src/public/js/ui.js +9 -7
- package/src/server.js +8 -0
- package/src/types/git.d.ts +36 -0
package/src/public/js/diff.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Changed-file list with lazily loaded, unified or side-by-side diffs.
|
|
2
2
|
|
|
3
3
|
import { api, html, raw, esc, icons, plural, storage, $, $$, loading, errorBox, WORKTREE } from "./ui.js";
|
|
4
|
+
import { detectLanguage, highlightLine } from "./highlight.js";
|
|
4
5
|
|
|
5
6
|
const STATUS = {
|
|
6
7
|
A: ["added", "Added"],
|
|
@@ -49,9 +50,9 @@ function parsePatch(text) {
|
|
|
49
50
|
|
|
50
51
|
const TOKEN = /\w+|\s+|[^\w\s]/g;
|
|
51
52
|
|
|
52
|
-
/**
|
|
53
|
-
function
|
|
54
|
-
if (a.length > 400 || b.length > 400) return [
|
|
53
|
+
/** Find the changed middle character range of a removed/added line pair (token prefix/suffix). */
|
|
54
|
+
function wordDiffRanges(a, b) {
|
|
55
|
+
if (a.length > 400 || b.length > 400) return [null, null];
|
|
55
56
|
const ta = a.match(TOKEN) || [];
|
|
56
57
|
const tb = b.match(TOKEN) || [];
|
|
57
58
|
let p = 0;
|
|
@@ -61,35 +62,37 @@ function wordDiff(a, b) {
|
|
|
61
62
|
const changedA = ta.length - p - s;
|
|
62
63
|
const changedB = tb.length - p - s;
|
|
63
64
|
// Mostly-different lines read better without highlights.
|
|
64
|
-
if (changedA > ta.length * 0.7 && changedB > tb.length * 0.7) return [
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
65
|
+
if (changedA > ta.length * 0.7 && changedB > tb.length * 0.7) return [null, null];
|
|
66
|
+
const range = (t, cls) => {
|
|
67
|
+
const start = t.slice(0, p).join("").length;
|
|
68
|
+
const len = t.slice(p, t.length - s).join("").length;
|
|
69
|
+
return len ? { start, end: start + len, cls } : null;
|
|
68
70
|
};
|
|
69
|
-
return [
|
|
71
|
+
return [range(ta, "del"), range(tb, "ins")];
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
/** Pair runs of removed lines with the added lines that follow them. */
|
|
73
|
-
function
|
|
74
|
-
const
|
|
75
|
-
for (let i = 0; i <
|
|
76
|
-
if (
|
|
74
|
+
/** Pair runs of removed lines with the added lines that follow them and compute word-diff marks. */
|
|
75
|
+
function computeMarks(lines, maxLen) {
|
|
76
|
+
const marks = new Array(Math.min(lines.length, maxLen)).fill(null);
|
|
77
|
+
for (let i = 0; i < lines.length && i < maxLen; ) {
|
|
78
|
+
if (lines[i].t !== "-") {
|
|
77
79
|
i++;
|
|
78
80
|
continue;
|
|
79
81
|
}
|
|
80
82
|
let j = i;
|
|
81
|
-
while (j <
|
|
83
|
+
while (j < lines.length && lines[j].t === "-") j++;
|
|
82
84
|
let k = j;
|
|
83
|
-
while (k <
|
|
85
|
+
while (k < lines.length && lines[k].t === "+") k++;
|
|
84
86
|
const pairs = Math.min(j - i, k - j);
|
|
85
87
|
for (let x = 0; x < pairs; x++) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
88
|
+
if (i + x >= maxLen && j + x >= maxLen) break;
|
|
89
|
+
const [d, a] = wordDiffRanges(lines[i + x].text, lines[j + x].text);
|
|
90
|
+
if (i + x < maxLen) marks[i + x] = d;
|
|
91
|
+
if (j + x < maxLen) marks[j + x] = a;
|
|
89
92
|
}
|
|
90
93
|
i = k;
|
|
91
94
|
}
|
|
92
|
-
return
|
|
95
|
+
return marks;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
// ------------------------------------------------------------------ rendering
|
|
@@ -98,41 +101,66 @@ function hunkRow(h, cols) {
|
|
|
98
101
|
return `<tr class="hunk"><td class="ln" colspan="${cols - 1}"></td><td class="code">${esc(h.header.replace(/ @@.*$/, " @@"))}<span class="section">${esc(h.section)}</span></td></tr>`;
|
|
99
102
|
}
|
|
100
103
|
|
|
101
|
-
function renderUnified(hunks, limit) {
|
|
104
|
+
function renderUnified(hunks, limit, lang) {
|
|
102
105
|
let rows = "";
|
|
103
106
|
let count = 0;
|
|
104
107
|
for (const h of hunks) {
|
|
105
108
|
rows += hunkRow(h, 3);
|
|
106
|
-
|
|
109
|
+
const remaining = limit - count;
|
|
110
|
+
const marks = computeMarks(h.lines, remaining);
|
|
111
|
+
let oldState = null;
|
|
112
|
+
let newState = null;
|
|
113
|
+
for (let i = 0; i < h.lines.length; i++) {
|
|
107
114
|
if (count++ >= limit) return { rows, cut: true };
|
|
115
|
+
const l = h.lines[i];
|
|
108
116
|
if (l.t === "\\") {
|
|
109
117
|
rows += `<tr class="note"><td class="ln"></td><td class="ln"></td><td class="code">${esc(l.text)}</td></tr>`;
|
|
110
118
|
continue;
|
|
111
119
|
}
|
|
120
|
+
let rendered;
|
|
121
|
+
if (l.t === "-") {
|
|
122
|
+
const res = highlightLine(l.text, lang, oldState, marks[i]);
|
|
123
|
+
rendered = res.html;
|
|
124
|
+
oldState = res.state;
|
|
125
|
+
} else if (l.t === "+") {
|
|
126
|
+
const res = highlightLine(l.text, lang, newState, marks[i]);
|
|
127
|
+
rendered = res.html;
|
|
128
|
+
newState = res.state;
|
|
129
|
+
} else {
|
|
130
|
+
const res = highlightLine(l.text, lang, newState);
|
|
131
|
+
rendered = res.html;
|
|
132
|
+
oldState = res.state;
|
|
133
|
+
newState = res.state;
|
|
134
|
+
}
|
|
112
135
|
const cls = l.t === "+" ? "add" : l.t === "-" ? "del" : "ctx";
|
|
113
|
-
rows += `<tr class="${cls}"><td class="ln" data-n="${l.o ?? ""}"></td><td class="ln" data-n="${l.n ?? ""}"></td><td class="code"><span class="sign">${l.t === " " ? " " : l.t}</span>${
|
|
136
|
+
rows += `<tr class="${cls}"><td class="ln" data-n="${l.o ?? ""}"></td><td class="ln" data-n="${l.n ?? ""}"></td><td class="code"><span class="sign">${l.t === " " ? " " : l.t}</span>${rendered}</td></tr>`;
|
|
114
137
|
}
|
|
115
138
|
}
|
|
116
139
|
return { rows, cut: false };
|
|
117
140
|
}
|
|
118
141
|
|
|
119
|
-
function renderSplit(hunks, limit) {
|
|
142
|
+
function renderSplit(hunks, limit, lang) {
|
|
120
143
|
let rows = "";
|
|
121
144
|
let count = 0;
|
|
122
|
-
const cell = (l, side) => {
|
|
145
|
+
const cell = (l, side, lineHtml) => {
|
|
123
146
|
if (!l) return `<td class="ln blank"></td><td class="code blank"></td>`;
|
|
124
147
|
const cls = l.t === "+" ? "add" : l.t === "-" ? "del" : "ctx";
|
|
125
148
|
const num = side === "old" ? l.o : l.n;
|
|
126
|
-
return `<td class="ln ${cls}" data-n="${num ?? ""}"></td><td class="code ${cls}">${
|
|
149
|
+
return `<td class="ln ${cls}" data-n="${num ?? ""}"></td><td class="code ${cls}">${lineHtml}</td>`;
|
|
127
150
|
};
|
|
128
151
|
for (const h of hunks) {
|
|
129
152
|
rows += `<tr class="hunk"><td class="ln"></td><td class="code" colspan="3">${esc(h.header.replace(/ @@.*$/, " @@"))}<span class="section">${esc(h.section)}</span></td></tr>`;
|
|
130
|
-
const lines =
|
|
153
|
+
const lines = h.lines.filter((l) => l.t !== "\\");
|
|
154
|
+
let oldState = null;
|
|
155
|
+
let newState = null;
|
|
131
156
|
for (let i = 0; i < lines.length; ) {
|
|
132
157
|
if (count >= limit) return { rows, cut: true };
|
|
133
158
|
const l = lines[i];
|
|
134
159
|
if (l.t === " ") {
|
|
135
|
-
|
|
160
|
+
const res = highlightLine(l.text, lang, newState);
|
|
161
|
+
oldState = res.state;
|
|
162
|
+
newState = res.state;
|
|
163
|
+
rows += `<tr>${cell(l, "old", res.html)}${cell(l, "new", res.html)}</tr>`;
|
|
136
164
|
i++;
|
|
137
165
|
count++;
|
|
138
166
|
continue;
|
|
@@ -143,8 +171,25 @@ function renderSplit(hunks, limit) {
|
|
|
143
171
|
while (k < lines.length && lines[k].t === "+") k++;
|
|
144
172
|
const dels = lines.slice(i, j);
|
|
145
173
|
const adds = lines.slice(j, k);
|
|
146
|
-
|
|
147
|
-
|
|
174
|
+
const total = Math.max(dels.length, adds.length);
|
|
175
|
+
for (let x = 0; x < total; x++) {
|
|
176
|
+
if (count >= limit) return { rows, cut: true };
|
|
177
|
+
const d = dels[x];
|
|
178
|
+
const a = adds[x];
|
|
179
|
+
const [dMark, aMark] = d && a ? wordDiffRanges(d.text, a.text) : [null, null];
|
|
180
|
+
let dHtml = "";
|
|
181
|
+
let aHtml = "";
|
|
182
|
+
if (d) {
|
|
183
|
+
const res = highlightLine(d.text, lang, oldState, dMark);
|
|
184
|
+
dHtml = res.html;
|
|
185
|
+
oldState = res.state;
|
|
186
|
+
}
|
|
187
|
+
if (a) {
|
|
188
|
+
const res = highlightLine(a.text, lang, newState, aMark);
|
|
189
|
+
aHtml = res.html;
|
|
190
|
+
newState = res.state;
|
|
191
|
+
}
|
|
192
|
+
rows += `<tr>${cell(d, "old", dHtml)}${cell(a, "new", aHtml)}</tr>`;
|
|
148
193
|
count++;
|
|
149
194
|
}
|
|
150
195
|
i = k;
|
|
@@ -153,11 +198,12 @@ function renderSplit(hunks, limit) {
|
|
|
153
198
|
return { rows, cut: false };
|
|
154
199
|
}
|
|
155
200
|
|
|
156
|
-
function renderPatch(text, { split = false, limit = MAX_LINES } = {}) {
|
|
201
|
+
export function renderPatch(text, { split = false, limit = MAX_LINES, path = "" } = {}) {
|
|
157
202
|
const { hunks, binary } = parsePatch(text);
|
|
158
203
|
if (binary) return `<div class="diff-note">Binary file changed</div>`;
|
|
159
204
|
if (!hunks.length) return `<div class="diff-note">No content changes (mode change, rename or whitespace only)</div>`;
|
|
160
|
-
const
|
|
205
|
+
const lang = detectLanguage(path);
|
|
206
|
+
const { rows, cut } = split ? renderSplit(hunks, limit, lang) : renderUnified(hunks, limit, lang);
|
|
161
207
|
return (
|
|
162
208
|
`<table class="diff ${split ? "split" : "unified"}"><colgroup>${split ? '<col class="c-ln"><col><col class="c-ln"><col>' : '<col class="c-ln"><col class="c-ln"><col>'}</colgroup><tbody>${rows}</tbody></table>` +
|
|
163
209
|
(cut ? `<div class="diff-note"><button class="btn btn-sm" data-act="all-lines">Show all lines</button></div>` : "")
|
|
@@ -280,7 +326,7 @@ export async function mountChanges(el, opts) {
|
|
|
280
326
|
</div>`;
|
|
281
327
|
$(".file-body", sec).innerHTML =
|
|
282
328
|
String(tools) +
|
|
283
|
-
renderPatch(res.patch, { split: state.split, limit: sec._all ? Infinity : MAX_LINES }) +
|
|
329
|
+
renderPatch(res.patch, { split: state.split, limit: sec._all ? Infinity : MAX_LINES, path: f.path }) +
|
|
284
330
|
(res.truncated ? `<div class="diff-note">Diff truncated (too large to display)</div>` : "");
|
|
285
331
|
}
|
|
286
332
|
|
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
// Lightweight, zero-dependency syntax highlighter for diffs and the file viewer.
|
|
2
|
+
|
|
3
|
+
import { html, raw } from "./ui.js";
|
|
4
|
+
|
|
5
|
+
export const MAX_LINE_LENGTH = 1000;
|
|
6
|
+
|
|
7
|
+
// ------------------------------------------------------------------ language detection
|
|
8
|
+
|
|
9
|
+
const EXT_MAP = {
|
|
10
|
+
js: "js", mjs: "js", cjs: "js", jsx: "js",
|
|
11
|
+
ts: "js", mts: "js", cts: "js", tsx: "js",
|
|
12
|
+
py: "py", pyw: "py", pyi: "py",
|
|
13
|
+
go: "go",
|
|
14
|
+
rs: "rs",
|
|
15
|
+
c: "c", h: "c", cpp: "c", cc: "c", cxx: "c", hpp: "c", hh: "c", hxx: "c",
|
|
16
|
+
java: "java", cs: "java", kt: "java", kts: "java", swift: "java", php: "java",
|
|
17
|
+
rb: "rb",
|
|
18
|
+
sh: "sh", bash: "sh", zsh: "sh", fish: "sh", env: "sh",
|
|
19
|
+
html: "html", htm: "html", xml: "html", svg: "html", vue: "html", svelte: "html",
|
|
20
|
+
css: "css", scss: "css", sass: "css", less: "css",
|
|
21
|
+
json: "json", jsonc: "json", webmanifest: "json",
|
|
22
|
+
yml: "yaml", yaml: "yaml", toml: "yaml", ini: "yaml", cfg: "yaml",
|
|
23
|
+
sql: "sql",
|
|
24
|
+
md: "md", mdx: "md", markdown: "md",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const NAME_MAP = {
|
|
28
|
+
dockerfile: "dockerfile",
|
|
29
|
+
makefile: "sh",
|
|
30
|
+
gnumakefile: "sh",
|
|
31
|
+
gemfile: "rb",
|
|
32
|
+
rakefile: "rb",
|
|
33
|
+
".bashrc": "sh",
|
|
34
|
+
".bash_profile": "sh",
|
|
35
|
+
".zshrc": "sh",
|
|
36
|
+
".profile": "sh",
|
|
37
|
+
".env": "sh",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export function detectLanguage(path) {
|
|
41
|
+
if (!path) return null;
|
|
42
|
+
const base = path.slice(path.lastIndexOf("/") + 1).toLowerCase();
|
|
43
|
+
if (NAME_MAP[base]) return NAME_MAP[base];
|
|
44
|
+
if (base.startsWith("dockerfile.")) return "dockerfile";
|
|
45
|
+
if (base.startsWith(".env.")) return "sh";
|
|
46
|
+
const dot = base.lastIndexOf(".");
|
|
47
|
+
if (dot <= 0) return null;
|
|
48
|
+
return EXT_MAP[base.slice(dot + 1)] || null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ------------------------------------------------------------------ grammar definitions
|
|
52
|
+
|
|
53
|
+
const NUM = /\b(?:0[xX][0-9a-fA-F_]+|0[bB][01_]+|0[oO][0-7_]+|\d[\d_]*(?:\.[\d_]+)?(?:[eE][+-]?\d+)?n?)\b/;
|
|
54
|
+
const DQ = /"(?:\\.|[^"\\\n])*"/;
|
|
55
|
+
const SQ = /'(?:\\.|[^'\\\n])*'/;
|
|
56
|
+
const BQ = /`(?:\\.|[^`\\\n])*`/;
|
|
57
|
+
|
|
58
|
+
function words(list) {
|
|
59
|
+
return new RegExp(`\\b(?:${list.split(" ").join("|")})\\b`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function makeScanner({ rules, flags = "g" }) {
|
|
63
|
+
const re = new RegExp(rules.map(([r]) => `(${r.source})`).join("|"), flags);
|
|
64
|
+
return (line, tokens) => {
|
|
65
|
+
let last = 0;
|
|
66
|
+
let nextState = null;
|
|
67
|
+
re.lastIndex = 0;
|
|
68
|
+
for (const m of line.matchAll(re)) {
|
|
69
|
+
if (m.index > last) tokens.push([null, line.slice(last, m.index)]);
|
|
70
|
+
const text = m[0];
|
|
71
|
+
last = m.index + text.length;
|
|
72
|
+
for (let i = 0; i < rules.length; i++) {
|
|
73
|
+
if (m[i + 1] !== undefined) {
|
|
74
|
+
const [, type, openState] = rules[i];
|
|
75
|
+
tokens.push([type, text]);
|
|
76
|
+
if (openState) nextState = openState;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (last < line.length) tokens.push([null, line.slice(last)]);
|
|
82
|
+
return nextState;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const C_BLOCK_CLOSED = /\/\*[^]*?\*\//;
|
|
87
|
+
const C_BLOCK_OPEN = /\/\*[^\n]*/;
|
|
88
|
+
const C_BLOCK_STATE = { end: "*/", type: "comment" };
|
|
89
|
+
const SLASH_COMMENT = /\/\/[^\n]*/;
|
|
90
|
+
const HASH_COMMENT = /#[^\n]*/;
|
|
91
|
+
const FN_CALL = /\b[A-Za-z_$][\w$]*(?=\s*\()/;
|
|
92
|
+
|
|
93
|
+
const SCANNERS = {
|
|
94
|
+
js: makeScanner({
|
|
95
|
+
rules: [
|
|
96
|
+
[SLASH_COMMENT, "comment"],
|
|
97
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
98
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
99
|
+
[DQ, "string"],
|
|
100
|
+
[SQ, "string"],
|
|
101
|
+
[BQ, "string"],
|
|
102
|
+
[
|
|
103
|
+
words(
|
|
104
|
+
"import from export default const let var function return if else for of in while do switch case break continue try catch finally throw new class extends super this async await yield typeof instanceof void delete static get set true false null undefined as type interface enum implements readonly private public protected abstract declare namespace keyof infer satisfies",
|
|
105
|
+
),
|
|
106
|
+
"keyword",
|
|
107
|
+
],
|
|
108
|
+
[words("string number boolean any unknown never object symbol bigint Promise Array Map Set Record Partial Required"), "type"],
|
|
109
|
+
[NUM, "number"],
|
|
110
|
+
[FN_CALL, "function"],
|
|
111
|
+
],
|
|
112
|
+
}),
|
|
113
|
+
|
|
114
|
+
py: makeScanner({
|
|
115
|
+
rules: [
|
|
116
|
+
[HASH_COMMENT, "comment"],
|
|
117
|
+
[/[rRfFbBuU]{0,2}"""(?:\\.|[^\\])*?"""/, "string"],
|
|
118
|
+
[/[rRfFbBuU]{0,2}"""[^\n]*/, "string", { end: '"""', type: "string" }],
|
|
119
|
+
[/[rRfFbBuU]{0,2}'''(?:\\.|[^\\])*?'''/, "string"],
|
|
120
|
+
[/[rRfFbBuU]{0,2}'''[^\n]*/, "string", { end: "'''", type: "string" }],
|
|
121
|
+
[/[rRfFbBuU]{0,2}"(?:\\.|[^"\\\n])*"/, "string"],
|
|
122
|
+
[/[rRfFbBuU]{0,2}'(?:\\.|[^'\\\n])*'/, "string"],
|
|
123
|
+
[/@[A-Za-z_][\w.]*/, "function"],
|
|
124
|
+
[
|
|
125
|
+
words(
|
|
126
|
+
"and as assert async await break class continue def del elif else except False finally for from global if import in is lambda None nonlocal not or pass raise return True try while with yield match case self",
|
|
127
|
+
),
|
|
128
|
+
"keyword",
|
|
129
|
+
],
|
|
130
|
+
[words("int float str bool list dict set tuple bytes object Exception"), "type"],
|
|
131
|
+
[NUM, "number"],
|
|
132
|
+
[FN_CALL, "function"],
|
|
133
|
+
],
|
|
134
|
+
}),
|
|
135
|
+
|
|
136
|
+
go: makeScanner({
|
|
137
|
+
rules: [
|
|
138
|
+
[SLASH_COMMENT, "comment"],
|
|
139
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
140
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
141
|
+
[DQ, "string"],
|
|
142
|
+
[BQ, "string"],
|
|
143
|
+
[/'(?:\\.|[^'\\\n])'/, "string"],
|
|
144
|
+
[
|
|
145
|
+
words(
|
|
146
|
+
"break case chan const continue default defer else fallthrough for func go goto if import interface map package range return select struct switch type var true false nil iota",
|
|
147
|
+
),
|
|
148
|
+
"keyword",
|
|
149
|
+
],
|
|
150
|
+
[
|
|
151
|
+
words(
|
|
152
|
+
"string bool int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr byte rune float32 float64 complex64 complex128 error any",
|
|
153
|
+
),
|
|
154
|
+
"type",
|
|
155
|
+
],
|
|
156
|
+
[NUM, "number"],
|
|
157
|
+
[FN_CALL, "function"],
|
|
158
|
+
],
|
|
159
|
+
}),
|
|
160
|
+
|
|
161
|
+
rs: makeScanner({
|
|
162
|
+
rules: [
|
|
163
|
+
[SLASH_COMMENT, "comment"],
|
|
164
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
165
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
166
|
+
[DQ, "string"],
|
|
167
|
+
[/'(?:\\.|[^'\\\n])'/, "string"],
|
|
168
|
+
[
|
|
169
|
+
words(
|
|
170
|
+
"as async await break const continue crate dyn else enum extern false fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait true type unsafe use where while",
|
|
171
|
+
),
|
|
172
|
+
"keyword",
|
|
173
|
+
],
|
|
174
|
+
[
|
|
175
|
+
words("bool char str i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize f32 f64 Option Result Vec String Box Some None Ok Err"),
|
|
176
|
+
"type",
|
|
177
|
+
],
|
|
178
|
+
[/\b[A-Za-z_]\w*!/, "function"],
|
|
179
|
+
[NUM, "number"],
|
|
180
|
+
[FN_CALL, "function"],
|
|
181
|
+
],
|
|
182
|
+
}),
|
|
183
|
+
|
|
184
|
+
c: makeScanner({
|
|
185
|
+
rules: [
|
|
186
|
+
[SLASH_COMMENT, "comment"],
|
|
187
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
188
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
189
|
+
[/^\s*#\s*[a-z]+/, "keyword"],
|
|
190
|
+
[DQ, "string"],
|
|
191
|
+
[/'(?:\\.|[^'\\\n])'/, "string"],
|
|
192
|
+
[
|
|
193
|
+
words(
|
|
194
|
+
"auto break case const continue default do else enum extern for goto if inline register restrict return sizeof static struct switch typedef union volatile while class namespace template typename virtual override final public private protected new delete try catch throw using constexpr nullptr true false NULL",
|
|
195
|
+
),
|
|
196
|
+
"keyword",
|
|
197
|
+
],
|
|
198
|
+
[words("void char short int long float double signed unsigned bool size_t ssize_t uint8_t uint16_t uint32_t uint64_t int8_t int16_t int32_t int64_t std string vector"), "type"],
|
|
199
|
+
[NUM, "number"],
|
|
200
|
+
[FN_CALL, "function"],
|
|
201
|
+
],
|
|
202
|
+
}),
|
|
203
|
+
|
|
204
|
+
java: makeScanner({
|
|
205
|
+
rules: [
|
|
206
|
+
[SLASH_COMMENT, "comment"],
|
|
207
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
208
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
209
|
+
[DQ, "string"],
|
|
210
|
+
[SQ, "string"],
|
|
211
|
+
[/@[A-Za-z_]\w*/, "function"],
|
|
212
|
+
[
|
|
213
|
+
words(
|
|
214
|
+
"abstract assert break case catch class const continue default do else enum extends final finally for if implements import instanceof interface native new package private protected public return static super switch synchronized this throw throws transient try void volatile while true false null var val fun when object sealed data async await using namespace struct readonly override virtual internal record func let guard defer echo function",
|
|
215
|
+
),
|
|
216
|
+
"keyword",
|
|
217
|
+
],
|
|
218
|
+
[words("boolean byte char double float int long short String Object List Map Set Int Bool Float Double"), "type"],
|
|
219
|
+
[NUM, "number"],
|
|
220
|
+
[FN_CALL, "function"],
|
|
221
|
+
],
|
|
222
|
+
}),
|
|
223
|
+
|
|
224
|
+
rb: makeScanner({
|
|
225
|
+
rules: [
|
|
226
|
+
[HASH_COMMENT, "comment"],
|
|
227
|
+
[DQ, "string"],
|
|
228
|
+
[SQ, "string"],
|
|
229
|
+
[/:[A-Za-z_]\w*[!?]?/, "attr"],
|
|
230
|
+
[
|
|
231
|
+
words(
|
|
232
|
+
"alias and begin break case class def defined? do else elsif end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield require require_relative include extend attr_reader attr_writer attr_accessor",
|
|
233
|
+
),
|
|
234
|
+
"keyword",
|
|
235
|
+
],
|
|
236
|
+
[NUM, "number"],
|
|
237
|
+
[FN_CALL, "function"],
|
|
238
|
+
],
|
|
239
|
+
}),
|
|
240
|
+
|
|
241
|
+
sh: makeScanner({
|
|
242
|
+
rules: [
|
|
243
|
+
[HASH_COMMENT, "comment"],
|
|
244
|
+
[DQ, "string"],
|
|
245
|
+
[SQ, "string"],
|
|
246
|
+
[/\$\{[^}\n]+\}|\$[A-Za-z_]\w*|\$[@*#?$!0-9]/, "attr"],
|
|
247
|
+
[/(?<=\s)--?[A-Za-z0-9][\w-]*/, "number"],
|
|
248
|
+
[
|
|
249
|
+
words(
|
|
250
|
+
"if then else elif fi for in do done while until case esac function return exit export local readonly unset set shift source eval exec true false",
|
|
251
|
+
),
|
|
252
|
+
"keyword",
|
|
253
|
+
],
|
|
254
|
+
[NUM, "number"],
|
|
255
|
+
],
|
|
256
|
+
}),
|
|
257
|
+
|
|
258
|
+
dockerfile: makeScanner({
|
|
259
|
+
rules: [
|
|
260
|
+
[HASH_COMMENT, "comment"],
|
|
261
|
+
[DQ, "string"],
|
|
262
|
+
[SQ, "string"],
|
|
263
|
+
[/\$\{[^}\n]+\}|\$[A-Za-z_]\w*/, "attr"],
|
|
264
|
+
[
|
|
265
|
+
/^\s*(?:FROM|RUN|CMD|LABEL|EXPOSE|ENV|ADD|COPY|ENTRYPOINT|VOLUME|USER|WORKDIR|ARG|ONBUILD|STOPSIGNAL|HEALTHCHECK|SHELL)\b/i,
|
|
266
|
+
"keyword",
|
|
267
|
+
],
|
|
268
|
+
[/\bAS\b/i, "keyword"],
|
|
269
|
+
[/(?<=\s)--?[A-Za-z0-9][\w-]*/, "number"],
|
|
270
|
+
],
|
|
271
|
+
}),
|
|
272
|
+
|
|
273
|
+
css: makeScanner({
|
|
274
|
+
rules: [
|
|
275
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
276
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
277
|
+
[SLASH_COMMENT, "comment"],
|
|
278
|
+
[DQ, "string"],
|
|
279
|
+
[SQ, "string"],
|
|
280
|
+
[/@[a-zA-Z-]+/, "keyword"],
|
|
281
|
+
[/--[A-Za-z0-9_-]+/, "attr"],
|
|
282
|
+
[/#[0-9a-fA-F]{3,8}\b/, "number"],
|
|
283
|
+
[/\b\d+(?:\.\d+)?(?:px|rem|em|vh|vw|vmin|vmax|ch|ex|cm|mm|in|pt|pc|deg|rad|turn|s|ms|fr|%)\b/, "number"],
|
|
284
|
+
[NUM, "number"],
|
|
285
|
+
[/\b[A-Za-z-]+(?=\s*:)/, "attr"],
|
|
286
|
+
[/\b[A-Za-z-]+(?=\()/, "function"],
|
|
287
|
+
],
|
|
288
|
+
}),
|
|
289
|
+
|
|
290
|
+
json: makeScanner({
|
|
291
|
+
rules: [
|
|
292
|
+
[SLASH_COMMENT, "comment"],
|
|
293
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
294
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
295
|
+
[/"(?:\\.|[^"\\\n])*"(?=\s*:)/, "attr"],
|
|
296
|
+
[DQ, "string"],
|
|
297
|
+
[/\b(?:true|false|null)\b/, "keyword"],
|
|
298
|
+
[/-?\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/, "number"],
|
|
299
|
+
],
|
|
300
|
+
}),
|
|
301
|
+
|
|
302
|
+
yaml: makeScanner({
|
|
303
|
+
rules: [
|
|
304
|
+
[/(?:^|(?<=\s))#[^\n]*/, "comment"],
|
|
305
|
+
[/^\s*\[[^\]\n]+\]/, "tag"],
|
|
306
|
+
[/^\s*(?:-\s+)?[A-Za-z0-9_.-]+(?=\s*[:=])/, "attr"],
|
|
307
|
+
[DQ, "string"],
|
|
308
|
+
[SQ, "string"],
|
|
309
|
+
[/\b(?:true|false|null|yes|no|on|off)\b/i, "keyword"],
|
|
310
|
+
[NUM, "number"],
|
|
311
|
+
],
|
|
312
|
+
}),
|
|
313
|
+
|
|
314
|
+
sql: makeScanner({
|
|
315
|
+
rules: [
|
|
316
|
+
[/--[^\n]*/, "comment"],
|
|
317
|
+
[C_BLOCK_CLOSED, "comment"],
|
|
318
|
+
[C_BLOCK_OPEN, "comment", C_BLOCK_STATE],
|
|
319
|
+
[SQ, "string"],
|
|
320
|
+
[DQ, "string"],
|
|
321
|
+
[
|
|
322
|
+
words(
|
|
323
|
+
"SELECT FROM WHERE INSERT INTO VALUES UPDATE SET DELETE CREATE ALTER DROP TABLE INDEX VIEW JOIN LEFT RIGHT INNER OUTER FULL CROSS ON AS AND OR NOT IN IS NULL TRUE FALSE ORDER BY GROUP HAVING LIMIT OFFSET DISTINCT UNION ALL CASE WHEN THEN ELSE END PRIMARY KEY FOREIGN REFERENCES DEFAULT CONSTRAINT BEGIN COMMIT ROLLBACK WITH RECURSIVE EXISTS BETWEEN LIKE ILIKE ASC DESC",
|
|
324
|
+
),
|
|
325
|
+
"keyword",
|
|
326
|
+
],
|
|
327
|
+
[NUM, "number"],
|
|
328
|
+
[FN_CALL, "function"],
|
|
329
|
+
],
|
|
330
|
+
flags: "gi",
|
|
331
|
+
}),
|
|
332
|
+
|
|
333
|
+
html: scanHtml,
|
|
334
|
+
md: scanMarkdown,
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
function scanHtml(line, tokens) {
|
|
338
|
+
const re = /(<!--[^]*?-->)|(<!--[^\n]*)|(<!DOCTYPE[^>]*>)|(<\/?)([A-Za-z][\w:-]*)([^>]*?)(\/?>)/gi;
|
|
339
|
+
let last = 0;
|
|
340
|
+
let nextState = null;
|
|
341
|
+
for (const m of line.matchAll(re)) {
|
|
342
|
+
if (m.index > last) tokens.push([null, line.slice(last, m.index)]);
|
|
343
|
+
last = m.index + m[0].length;
|
|
344
|
+
if (m[1]) {
|
|
345
|
+
tokens.push(["comment", m[1]]);
|
|
346
|
+
} else if (m[2]) {
|
|
347
|
+
tokens.push(["comment", m[2]]);
|
|
348
|
+
nextState = { end: "-->", type: "comment" };
|
|
349
|
+
} else if (m[3]) {
|
|
350
|
+
tokens.push(["comment", m[3]]);
|
|
351
|
+
} else {
|
|
352
|
+
tokens.push([null, m[4]]);
|
|
353
|
+
tokens.push(["tag", m[5]]);
|
|
354
|
+
if (m[6]) scanHtmlAttrs(m[6], tokens);
|
|
355
|
+
tokens.push([null, m[7]]);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (last < line.length) tokens.push([null, line.slice(last)]);
|
|
359
|
+
return nextState;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function scanHtmlAttrs(src, tokens) {
|
|
363
|
+
const re = /("(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*')|([A-Za-z_:@][\w:.-]*)(?=\s*=|\s|$)/g;
|
|
364
|
+
let last = 0;
|
|
365
|
+
for (const m of src.matchAll(re)) {
|
|
366
|
+
if (m.index > last) tokens.push([null, src.slice(last, m.index)]);
|
|
367
|
+
last = m.index + m[0].length;
|
|
368
|
+
tokens.push([m[1] ? "string" : "attr", m[0]]);
|
|
369
|
+
}
|
|
370
|
+
if (last < src.length) tokens.push([null, src.slice(last)]);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function scanMarkdown(line, tokens) {
|
|
374
|
+
if (/^\s*```/.test(line)) {
|
|
375
|
+
tokens.push(["comment", line]);
|
|
376
|
+
return { end: "```", type: "string", fence: true };
|
|
377
|
+
}
|
|
378
|
+
if (/^\s*#{1,6}\s/.test(line)) {
|
|
379
|
+
tokens.push(["keyword", line]);
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
if (/^\s*>/.test(line)) {
|
|
383
|
+
tokens.push(["comment", line]);
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
const re = /(`[^`\n]+`)|(\*\*[^*\n]+\*\*)|(\[[^\]\n]+\]\([^)\n]+\))/g;
|
|
387
|
+
let last = 0;
|
|
388
|
+
for (const m of line.matchAll(re)) {
|
|
389
|
+
if (m.index > last) tokens.push([null, line.slice(last, m.index)]);
|
|
390
|
+
last = m.index + m[0].length;
|
|
391
|
+
tokens.push([m[1] ? "string" : m[2] ? "keyword" : "function", m[0]]);
|
|
392
|
+
}
|
|
393
|
+
if (last < line.length) tokens.push([null, line.slice(last)]);
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// ------------------------------------------------------------------ tokenizer + renderer
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Tokenize a single line for a detected language, carrying optional multi-line state.
|
|
401
|
+
* Returns { tokens: Array<[type | null, text]>, state }.
|
|
402
|
+
*/
|
|
403
|
+
export function tokenizeLine(line, lang, state = null) {
|
|
404
|
+
if (!line || !lang || line.length > MAX_LINE_LENGTH) {
|
|
405
|
+
return { tokens: line ? [[null, line]] : [], state };
|
|
406
|
+
}
|
|
407
|
+
const scanner = SCANNERS[lang];
|
|
408
|
+
if (!scanner) return { tokens: [[null, line]], state: null };
|
|
409
|
+
|
|
410
|
+
const tokens = [];
|
|
411
|
+
let rest = line;
|
|
412
|
+
let curState = state;
|
|
413
|
+
|
|
414
|
+
if (curState) {
|
|
415
|
+
if (curState.fence) {
|
|
416
|
+
if (/^\s*```/.test(rest)) {
|
|
417
|
+
tokens.push(["comment", rest]);
|
|
418
|
+
return { tokens, state: null };
|
|
419
|
+
}
|
|
420
|
+
tokens.push([curState.type, rest]);
|
|
421
|
+
return { tokens, state: curState };
|
|
422
|
+
}
|
|
423
|
+
const endIdx = rest.indexOf(curState.end);
|
|
424
|
+
if (endIdx === -1) {
|
|
425
|
+
tokens.push([curState.type, rest]);
|
|
426
|
+
return { tokens, state: curState };
|
|
427
|
+
}
|
|
428
|
+
const cut = endIdx + curState.end.length;
|
|
429
|
+
tokens.push([curState.type, rest.slice(0, cut)]);
|
|
430
|
+
rest = rest.slice(cut);
|
|
431
|
+
curState = null;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const nextState = rest ? scanner(rest, tokens) : null;
|
|
435
|
+
return { tokens, state: nextState };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function renderTokens(tokens) {
|
|
439
|
+
let out = "";
|
|
440
|
+
for (const [type, text] of tokens) {
|
|
441
|
+
out += type ? String(html`<span class="tok-${type}">${text}</span>`) : String(html`${text}`);
|
|
442
|
+
}
|
|
443
|
+
return out;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Highlight a single line into escaped HTML, optionally wrapping a character range
|
|
448
|
+
* [mark.start, mark.end) in <mark class="${mark.cls}"> for word-level diffs.
|
|
449
|
+
*/
|
|
450
|
+
export function highlightLine(line, lang, state = null, mark = null) {
|
|
451
|
+
const { tokens, state: nextState } = tokenizeLine(line, lang, state);
|
|
452
|
+
if (!mark || mark.start >= mark.end) {
|
|
453
|
+
return { html: renderTokens(tokens), state: nextState };
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const before = [];
|
|
457
|
+
const inside = [];
|
|
458
|
+
const after = [];
|
|
459
|
+
let pos = 0;
|
|
460
|
+
|
|
461
|
+
for (const [type, text] of tokens) {
|
|
462
|
+
const end = pos + text.length;
|
|
463
|
+
if (end <= mark.start) {
|
|
464
|
+
before.push([type, text]);
|
|
465
|
+
} else if (pos >= mark.end) {
|
|
466
|
+
after.push([type, text]);
|
|
467
|
+
} else {
|
|
468
|
+
if (pos < mark.start) before.push([type, text.slice(0, mark.start - pos)]);
|
|
469
|
+
const a = Math.max(0, mark.start - pos);
|
|
470
|
+
const b = Math.min(text.length, mark.end - pos);
|
|
471
|
+
if (b > a) inside.push([type, text.slice(a, b)]);
|
|
472
|
+
if (end > mark.end) after.push([type, text.slice(mark.end - pos)]);
|
|
473
|
+
}
|
|
474
|
+
pos = end;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
const rendered = String(
|
|
478
|
+
html`${raw(renderTokens(before))}<mark class="${mark.cls}">${raw(renderTokens(inside))}</mark>${raw(renderTokens(after))}`,
|
|
479
|
+
);
|
|
480
|
+
return { html: rendered, state: nextState };
|
|
481
|
+
}
|