Skip to content

Commit 7ef2336

Browse files
committed
feat(editor): add setting to disable HTML tag auto closing
1 parent 2ef5d86 commit 7ef2336

4 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/cm/supportedModes.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,72 @@ function escapeRegExp(value: string): string {
5757
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5858
}
5959

60+
async function shouldAutoCloseTags(): Promise<boolean> {
61+
const { default: appSettings } = await import("lib/settings");
62+
return appSettings.value.autoCloseTags !== false;
63+
}
64+
65+
function createLanguageLoader(name: string, lang: LanguageDescription) {
66+
const normalizedName = normalizeModeKey(name);
67+
68+
switch (normalizedName) {
69+
case "html":
70+
return async () => {
71+
const { html } = await import("@codemirror/lang-html");
72+
return html({ autoCloseTags: await shouldAutoCloseTags() });
73+
};
74+
75+
case "xml":
76+
return async () => {
77+
const { xml } = await import("@codemirror/lang-xml");
78+
return xml({ autoCloseTags: await shouldAutoCloseTags() });
79+
};
80+
81+
case "vue":
82+
return async () => {
83+
const [{ vue }, { html }] = await Promise.all([
84+
import("@codemirror/lang-vue"),
85+
import("@codemirror/lang-html"),
86+
]);
87+
return vue({
88+
base: html({ autoCloseTags: await shouldAutoCloseTags() }),
89+
});
90+
};
91+
92+
case "angular":
93+
return async () => {
94+
const [{ angular }, { html }] = await Promise.all([
95+
import("@codemirror/lang-angular"),
96+
import("@codemirror/lang-html"),
97+
]);
98+
return angular({
99+
base: html({
100+
autoCloseTags: await shouldAutoCloseTags(),
101+
selfClosingTags: true,
102+
}),
103+
});
104+
};
105+
106+
case "php":
107+
return async () => {
108+
const [{ php }, { html }] = await Promise.all([
109+
import("@codemirror/lang-php"),
110+
import("@codemirror/lang-html"),
111+
]);
112+
const htmlSupport = html({
113+
autoCloseTags: await shouldAutoCloseTags(),
114+
matchClosingTags: false,
115+
});
116+
return [
117+
php({ baseLanguage: htmlSupport.language }),
118+
htmlSupport.support,
119+
];
120+
};
121+
}
122+
123+
return typeof lang.load === "function" ? () => lang.load!() : null;
124+
}
125+
60126
// 1) Always register a plain text fallback
61127
addMode("Text", "txt|text|log|plain", "Plain Text", () => []);
62128

@@ -106,7 +172,7 @@ for (const lang of languages as readonly LanguageDescription[]) {
106172

107173
// Wrap language-data loader as our modelist language provider
108174
// lang.load() returns a Promise<Extension>; we let the editor handle async loading
109-
const loader = typeof lang.load === "function" ? () => lang.load!() : null;
175+
const loader = createLanguageLoader(name, lang);
110176

111177
addMode(modeId, pattern, name, loader, {
112178
aliases,

src/lib/editorManager.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,11 @@ async function EditorManager($header, $body) {
15751575
applyOptions(["liveAutoCompletion"]);
15761576
});
15771577

1578+
appSettings.on("update:autoCloseTags", function () {
1579+
const file = manager.activeFile;
1580+
if (file?.type === "editor") applyFileToEditor(file);
1581+
});
1582+
15781583
appSettings.on("update:linenumbers", function () {
15791584
updateMargin(true);
15801585
updateEditorLineNumbersFromSettings();

src/lib/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class Settings {
143143
fullscreen: false,
144144
floatingButton: !this.#IS_TABLET,
145145
liveAutoCompletion: true,
146+
autoCloseTags: true,
146147
showPrintMargin: false,
147148
printMargin: 80,
148149
scrollbarSize: 20,

src/settings/editorSettings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ export default function editorSettings() {
126126
info: strings["settings-info-editor-live-autocomplete"],
127127
category: categories.assistance,
128128
},
129+
{
130+
key: "autoCloseTags",
131+
text: strings["auto close tags"],
132+
checkbox: values.autoCloseTags ?? true,
133+
info: strings["settings-info-editor-auto-close-tags"],
134+
category: categories.assistance,
135+
},
129136
{
130137
key: "colorPreview",
131138
text: strings["color preview"],

0 commit comments

Comments
 (0)