git-tm 1.1.2 → 2.0.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 +42 -3
- package/package.json +5 -1
- package/src/cli.js +160 -4
- package/src/edit.js +508 -0
- package/src/git.js +86 -3
- package/src/index.js +1 -0
- package/src/listen-again.js +30 -0
- package/src/paths.js +21 -0
- package/src/public/app.css +817 -0
- package/src/public/index.html +4 -1
- package/src/public/js/compare.js +24 -8
- package/src/public/js/detail.js +12 -3
- package/src/public/js/diff.js +151 -63
- package/src/public/js/edit.js +384 -0
- package/src/public/js/highlight.js +481 -0
- package/src/public/js/history.js +70 -1
- package/src/public/js/main.js +71 -3
- package/src/public/js/reflog.js +244 -0
- package/src/public/js/ui.js +130 -16
- package/src/server.js +106 -5
- package/src/types/edit.d.ts +69 -0
- package/src/types/git.d.ts +43 -0
- package/src/types/index.d.ts +2 -0
- package/src/types/server.d.ts +6 -0
|
@@ -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
|
+
}
|
package/src/public/js/history.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// History view: virtualised commit list with graph + commit detail pane.
|
|
2
2
|
|
|
3
|
-
import { api, html, raw, esc, $, avatar, refBadges, relTime, fullDate, short, plural, empty, errorBox, loading, icons, storage, WORKTREE } from "./ui.js";
|
|
3
|
+
import { api, html, raw, esc, $, avatar, refBadges, relTime, fullDate, short, plural, empty, errorBox, loading, icons, storage, toast, copyText, openContextMenu, WORKTREE } from "./ui.js";
|
|
4
4
|
import { createLayout, layout, rowSvg, LANE_W } from "./graph.js";
|
|
5
5
|
import { renderDetail } from "./detail.js";
|
|
6
|
+
import { openEdit } from "./edit.js";
|
|
6
7
|
|
|
7
8
|
const ROW_H = 34;
|
|
8
9
|
const PAGE = 400;
|
|
@@ -267,6 +268,74 @@ export function createHistory(app) {
|
|
|
267
268
|
app.navigate(`#/commit/${c.hash}`);
|
|
268
269
|
});
|
|
269
270
|
|
|
271
|
+
// Listen on the scroller, which holds keyboard focus, so Shift+F10 / the Menu key opens the menu for the selected row.
|
|
272
|
+
$(".log-scroll", el).addEventListener("contextmenu", (e) => {
|
|
273
|
+
const keyboard = e.clientX === 0 && e.clientY === 0;
|
|
274
|
+
const r = e.target.closest(".row") || (keyboard && s.selected >= 0 ? $(`.row[data-i="${s.selected}"]`, rows) : null);
|
|
275
|
+
if (!r) return;
|
|
276
|
+
const i = Number(r.dataset.i);
|
|
277
|
+
const c = s.commits[i];
|
|
278
|
+
if (!c) return;
|
|
279
|
+
s.selected = i;
|
|
280
|
+
paint(true);
|
|
281
|
+
// paint() re-renders the rows, so look the row up again to anchor a keyboard-opened menu.
|
|
282
|
+
const anchor = keyboard ? $(`.row[data-i="${i}"]`, rows) : null;
|
|
283
|
+
const isHead = app.repo?.head && c.hash === app.repo.head;
|
|
284
|
+
// The "Uncommitted changes" row is not a commit: no SHA to copy and nothing to edit.
|
|
285
|
+
const isWt = c.hash === WORKTREE || c.worktree;
|
|
286
|
+
if (isWt) {
|
|
287
|
+
openContextMenu(e, [
|
|
288
|
+
{ label: "Open changes", icon: icons.file, onClick: () => app.navigate(`#/commit/${WORKTREE}`) },
|
|
289
|
+
app.repo?.head ? { label: "Compare HEAD to working tree", icon: icons.compare, onClick: () => app.navigate(`#/compare/HEAD..${WORKTREE}`) } : null,
|
|
290
|
+
], anchor);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
openContextMenu(e, [
|
|
294
|
+
{ label: "Open commit", icon: icons.file, onClick: () => app.navigate(`#/commit/${c.hash}`) },
|
|
295
|
+
"sep",
|
|
296
|
+
{
|
|
297
|
+
label: "Travel from here",
|
|
298
|
+
icon: icons.compare,
|
|
299
|
+
onClick: () => {
|
|
300
|
+
travel.picking = true;
|
|
301
|
+
renderHead();
|
|
302
|
+
pick("from", c);
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
{
|
|
306
|
+
label: "Travel to here",
|
|
307
|
+
icon: icons.compare,
|
|
308
|
+
onClick: () => {
|
|
309
|
+
travel.picking = true;
|
|
310
|
+
renderHead();
|
|
311
|
+
pick("to", c);
|
|
312
|
+
},
|
|
313
|
+
},
|
|
314
|
+
!isHead && app.repo?.head ? { label: "Compare to HEAD", icon: icons.history, onClick: () => app.navigate(`#/compare/${c.hash}..HEAD`) } : null,
|
|
315
|
+
"sep",
|
|
316
|
+
{ label: `Copy SHA (${short(c.hash)})`, icon: icons.copy, onClick: () => copyText(c.hash, "Commit SHA copied") },
|
|
317
|
+
{ label: "Copy subject", icon: icons.copy, onClick: () => copyText(c.subject, "Subject copied") },
|
|
318
|
+
app.repo?.edit ? "sep" : null,
|
|
319
|
+
app.repo?.edit
|
|
320
|
+
? {
|
|
321
|
+
label: "Edit commit",
|
|
322
|
+
cls: "warn",
|
|
323
|
+
onClick: async () => {
|
|
324
|
+
// The list row has no message body, so a failed load must not open a Reword form without it.
|
|
325
|
+
let full;
|
|
326
|
+
try {
|
|
327
|
+
full = (await api("commit", { rev: c.hash })).commit;
|
|
328
|
+
} catch (err) {
|
|
329
|
+
toast(`Could not load the commit: ${err.message}`, "error");
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
openEdit(app, full);
|
|
333
|
+
},
|
|
334
|
+
}
|
|
335
|
+
: null,
|
|
336
|
+
], anchor);
|
|
337
|
+
});
|
|
338
|
+
|
|
270
339
|
head.addEventListener("click", (e) => {
|
|
271
340
|
const act = e.target.closest("[data-act]");
|
|
272
341
|
if (!act) return;
|