xonecas.io
70160
Ce

cel-tui / files

cel-tui / packages / clew / src / languages / diff.ts

3.4 KB
1import type { ClewOutput, ClewToken } from "../model.js";
2import type { ClewLanguageSupport } from "../registry.js";
3import { pushTokenRange, splitLines, stableLineBoundary } from "../shared.js";
4
5const GIT_FILE_HEADER_PREFIXES = [
6  "diff --git ",
7  "index ",
8  "old mode ",
9  "new mode ",
10  "deleted file mode ",
11  "new file mode ",
12  "similarity index ",
13  "dissimilarity index ",
14  "rename from ",
15  "rename to ",
16  "copy from ",
17  "copy to ",
18] as const;
19
20function pushToken(
21  tokens: ClewToken[],
22  content: string,
23  start: number,
24  end: number,
25  type: string,
26  scopes?: readonly string[],
27): void {
28  pushTokenRange(tokens, content, start, end, type, scopes);
29}
30
31function isGitFileHeader(text: string): boolean {
32  return GIT_FILE_HEADER_PREFIXES.some((prefix) => text.startsWith(prefix));
33}
34
35function isDiffPackageIndexHeader(text: string): boolean {
36  return text.startsWith("Index: ") || /^={3,}$/.test(text);
37}
38
39function tokenizePrefixedLine(
40  tokens: ClewToken[],
41  content: string,
42  lineStart: number,
43  lineEnd: number,
44  bodyType: string,
45  bodyScopes: readonly string[],
46): void {
47  pushToken(tokens, content, lineStart, lineStart + 1, "operator", [
48    "operator",
49    "diff.marker",
50  ]);
51  pushToken(tokens, content, lineStart + 1, lineEnd, bodyType, bodyScopes);
52}
53
54function tokenizeDiff(content: string): ClewOutput {
55  const tokens: ClewToken[] = [];
56
57  for (const line of splitLines(content)) {
58    const lineStart = line.start;
59    const lineEnd = line.start + line.text.length;
60    const text = line.text;
61
62    if (text.length === 0) {
63      // Empty lines are represented by their delimiter below.
64    } else if (isGitFileHeader(text) || isDiffPackageIndexHeader(text)) {
65      pushToken(tokens, content, lineStart, lineEnd, "meta", [
66        "meta",
67        "diff.header",
68      ]);
69    } else if (text.startsWith("--- ")) {
70      pushToken(tokens, content, lineStart, lineEnd, "meta", [
71        "meta",
72        "diff.file.old",
73      ]);
74    } else if (text.startsWith("+++ ")) {
75      pushToken(tokens, content, lineStart, lineEnd, "meta", [
76        "meta",
77        "diff.file.new",
78      ]);
79    } else if (/^@@\s+-\d+(?:,\d+)?\s+\+\d+(?:,\d+)?\s+@@/.test(text)) {
80      pushToken(tokens, content, lineStart, lineEnd, "meta", [
81        "meta",
82        "diff.hunk",
83      ]);
84    } else if (text.startsWith("+")) {
85      tokenizePrefixedLine(tokens, content, lineStart, lineEnd, "string", [
86        "string",
87        "diff.inserted",
88      ]);
89    } else if (text.startsWith("-")) {
90      tokenizePrefixedLine(tokens, content, lineStart, lineEnd, "comment", [
91        "comment",
92        "diff.deleted",
93      ]);
94    } else if (text.startsWith(" ")) {
95      tokenizePrefixedLine(tokens, content, lineStart, lineEnd, "text", [
96        "text",
97        "diff.context",
98      ]);
99    } else if (text === "\\ No newline at end of file") {
100      pushToken(tokens, content, lineStart, lineEnd, "comment", [
101        "comment",
102        "diff.no-newline",
103      ]);
104    } else {
105      pushToken(tokens, content, lineStart, lineEnd, "text");
106    }
107
108    if (line.delimiter.length > 0) {
109      pushToken(
110        tokens,
111        content,
112        lineEnd,
113        lineEnd + line.delimiter.length,
114        "whitespace",
115      );
116    }
117  }
118
119  return { tokens };
120}
121
122export const diffLanguageSupport: ClewLanguageSupport = {
123  ids: ["diff", "patch"],
124  stableBoundary: stableLineBoundary,
125  tokenize: tokenizeDiff,
126};