forked from Acode-Foundation/Acode
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsupportedModes.ts
More file actions
190 lines (163 loc) · 5.1 KB
/
supportedModes.ts
File metadata and controls
190 lines (163 loc) · 5.1 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
import { languages } from "@codemirror/language-data";
import type { Extension } from "@codemirror/state";
import { addMode } from "./modelist";
type FilenameMatcher = string | RegExp;
interface LanguageDescription {
name?: string;
alias?: readonly string[];
extensions?: readonly string[];
filenames?: readonly FilenameMatcher[];
filename?: FilenameMatcher;
load?: () => Promise<Extension>;
}
function normalizeModeKey(value: string): string {
return String(value ?? "")
.trim()
.toLowerCase();
}
function isSafeModeId(value: string): boolean {
return /^[a-z0-9][a-z0-9._-]*$/.test(value);
}
function slugifyModeId(value: string): string {
return normalizeModeKey(value)
.replace(/\+\+/g, "pp")
.replace(/#/g, "sharp")
.replace(/&/g, "and")
.replace(/[^a-z0-9._-]+/g, "-")
.replace(/^-+|-+$/g, "");
}
function collectAliases(
name: string,
aliases: readonly string[] | undefined,
): string[] {
return [
...new Set(
[name, ...(aliases || [])].map(normalizeModeKey).filter(Boolean),
),
];
}
function getModeId(name: string, aliases: string[]): string {
const normalizedName = normalizeModeKey(name);
if (isSafeModeId(normalizedName)) return normalizedName;
const safeAlias = aliases.find(
(alias) => alias !== normalizedName && isSafeModeId(alias),
);
return safeAlias || slugifyModeId(name) || normalizedName || "text";
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
async function shouldAutoCloseTags(): Promise<boolean> {
const { default: appSettings } = await import("lib/settings");
return appSettings.value.autoCloseTags !== false;
}
function createLanguageLoader(name: string, lang: LanguageDescription) {
const normalizedName = normalizeModeKey(name);
switch (normalizedName) {
case "html":
return async () => {
const { html } = await import("@codemirror/lang-html");
return html({ autoCloseTags: await shouldAutoCloseTags() });
};
case "xml":
return async () => {
const { xml } = await import("@codemirror/lang-xml");
return xml({ autoCloseTags: await shouldAutoCloseTags() });
};
case "vue":
return async () => {
const [{ vue }, { html }] = await Promise.all([
import("@codemirror/lang-vue"),
import("@codemirror/lang-html"),
]);
return vue({
base: html({ autoCloseTags: await shouldAutoCloseTags() }),
});
};
case "angular":
return async () => {
const [{ angular }, { html }] = await Promise.all([
import("@codemirror/lang-angular"),
import("@codemirror/lang-html"),
]);
return angular({
base: html({
autoCloseTags: await shouldAutoCloseTags(),
selfClosingTags: true,
}),
});
};
case "php":
return async () => {
const [{ php }, { html }] = await Promise.all([
import("@codemirror/lang-php"),
import("@codemirror/lang-html"),
]);
const htmlSupport = html({
autoCloseTags: await shouldAutoCloseTags(),
matchClosingTags: false,
});
return [
php({ baseLanguage: htmlSupport.language }),
htmlSupport.support,
];
};
}
return typeof lang.load === "function" ? () => lang.load!() : null;
}
// 1) Always register a plain text fallback
addMode("Text", "txt|text|log|plain", "Plain Text", () => []);
// 2) Register all languages provided by @codemirror/language-data
// We convert extensions like [".js", ".mjs"] into a modelist pattern: "js|mjs"
// and preserve aliases and filename regexes for languages like C++ and Dockerfile.
for (const lang of languages as readonly LanguageDescription[]) {
try {
const name = String(lang?.name || "").trim();
if (!name) continue;
const aliases = collectAliases(name, lang.alias);
const modeId = getModeId(name, aliases);
const parts: string[] = [];
const filenameMatchers: RegExp[] = [];
// File extensions
if (Array.isArray(lang.extensions)) {
for (const e of lang.extensions) {
if (typeof e !== "string") continue;
const cleaned = e.replace(/^\./, "").trim();
if (cleaned) parts.push(cleaned);
}
}
// Exact filenames / filename regexes (Dockerfile, PKGBUILD, nginx*.conf, etc.)
const filenames = Array.isArray(lang.filenames)
? lang.filenames
: lang.filename
? [lang.filename]
: [];
for (const fn of filenames) {
if (typeof fn === "string") {
const cleaned = fn.trim();
if (cleaned) {
filenameMatchers.push(new RegExp(`^${escapeRegExp(cleaned)}$`, "i"));
}
continue;
}
if (fn instanceof RegExp) {
filenameMatchers.push(new RegExp(fn.source, fn.flags));
}
}
const pattern = parts.join("|");
// Wrap language-data loader as our modelist language provider
// lang.load() returns a Promise<Extension>; we let the editor handle async loading
const loader = createLanguageLoader(name, lang);
addMode(modeId, pattern, name, loader, {
aliases,
filenameMatchers,
});
} catch (_) {
// Ignore faulty entries to avoid breaking the whole registration
}
}
// Luau isn't bundled in @codemirror/language-data, so register it explicitly.
addMode("Luau", "luau", "Luau", async () => {
const { luau } = await import("./modes/luau");
return luau();
});