feat(languages): Jinja2 Template language for .jinja, .jinja2 and .j2 files (#6645)

.jinja, .jinja2 and .j2 files opened as Plain Text. They now open as Jinja2 templates, with the extension before them deciding the content between the tags: Jinja2 HTML (.html.j2, .html.jinja, .htm.*) gets the HTML language server, Emmet, tag closing and linked tags like Django templates; any other name (nginx.conf.j2, values.yaml.j2, base.jinja) is a Jinja2 Template over plain text without HTML tooling.

Both use @codemirror/lang-jinja (now a direct dependency, the same 6.0.1 already in the lockfile): highlighting, folding and indentation of block statements, completion of tags, filters and tests, and tag matching. The file icon follows the extension before .j2, and breakpoints in a file typed as a Jinja language turn on debugpy's jinja support.

Closes #6644

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-29 13:23:45 +08:00
1 джерело 5b486f1588
коміт 9ee8f67e9f
22 files changed
+288 -14

No files matched your search

+3
Переглянути файл
@@ -260,6 +260,9 @@ describe("run config → launch config", () => {
expect(isJinjaTemplate(p), p).toBe(true);
for (const p of ["/p/static/index.html", "/p/templates.py", "/p/templates/style.css", "/p/app.py"])
expect(isJinjaTemplate(p), p).toBe(false);
// A file whose Override File Type is Jinja2 Template.
expect(isJinjaTemplate("/p/static/index.html", "jinja")).toBe(true);
expect(isJinjaTemplate("/p/static/index.html", "jinja-html")).toBe(true);
});
it("debugpy: uv / poetry run the debuggee in the project's environment", () => {
+21 -2
Переглянути файл
@@ -5,10 +5,13 @@ import { useBreakpoints } from "../breakpoints/store";
const ipc = vi.hoisted(() => ({
languages: vi.fn(async () => [{ id: "python", debuggable: true }]),
// Django Templates by the backend's detection (a `templates` folder of a `manage.py` project).
// Django Templates by the backend's detection (a `templates` folder of a `manage.py` project), and
// files whose Override File Type is a template language.
docLanguage: vi.fn(async (path: string) => ({
status: "ok" as const,
data: { language: path.startsWith("/shop/") && path.endsWith(".html") ? "django-html" : "html" },
data: {
language: OVERRIDES[path] ?? (path.startsWith("/shop/") && path.endsWith(".html") ? "django-html" : "html"),
},
})),
}));
@@ -17,6 +20,12 @@ vi.mock("@/platform/ipc", async (original) => {
return { ...real, isTauri: () => true, commands: { ...real.commands, ...ipc } };
});
const OVERRIDES: Record<string, string> = {
"/flask/static/page.html": "jinja-html",
"/flask/static/robots.txt": "jinja",
"/shop/mail/welcome.j2": "django-html",
};
afterEach(() => useBreakpoints.setState({ byFile: {} }));
const withBreakpoints = (byFile: Record<string, boolean>) =>
@@ -37,6 +46,16 @@ it("tells Django templates from Jinja2 ones by the file's language", async () =>
expect(ipc.docLanguage).not.toHaveBeenCalledWith("/shop/views.py", null);
});
it("goes by the language a file is typed as", async () => {
withBreakpoints({ "/flask/static/page.html": true });
expect(await templateBreakpoints()).toEqual({ django: false, jinja: true });
withBreakpoints({ "/flask/static/robots.txt": true });
expect(await templateBreakpoints()).toEqual({ django: false, jinja: true });
// A `.j2` file typed as a Django Template is Django's, not Jinja2's.
withBreakpoints({ "/shop/mail/welcome.j2": true });
expect(await templateBreakpoints()).toEqual({ django: true, jinja: false });
});
it("accepts breakpoints in templates without a debugger of their own", async () => {
canPlaceBreakpoint("python", "/a.py");
await vi.waitFor(() => expect(canPlaceBreakpoint("text", "/notes.txt")).toBe(false));
+11 -6
Переглянути файл
@@ -23,19 +23,24 @@ function load() {
.finally(() => (loading = false));
}
/** Jinja2's own extensions: no language of theirs yet, but debugpy stops in them (`jinja: true`). */
const JINJA_EXTENSION = /\.(jinja2?|j2)$/i;
/** A Jinja2 template for debugpy: a `.jinja` / `.j2` file, or markup or text in a `templates` folder. */
export const isJinjaTemplate = (path: string) =>
JINJA_EXTENSION.test(path) || /[\\/]templates[\\/].*\.(html?|xml|txt)$/i.test(path);
/**
* A Jinja2 template for debugpy: a Jinja2 Template file (also by extension, should File Types give
* it to another language), or markup or text in a `templates` folder.
*/
export const isJinjaTemplate = (path: string, language?: string | null) =>
language === "jinja" ||
language === "jinja-html" ||
JINJA_EXTENSION.test(path) ||
/[\\/]templates[\\/].*\.(html?|xml|txt)$/i.test(path);
/** Whether a new breakpoint may be placed in a file of `language`. */
export function canPlaceBreakpoint(language: string, path = ""): boolean {
if (classFileBreakpointReason(path)) return false;
// Templates have no adapter of their own: a Python Debug configuration stops in them (debugpy
// `django` / `jinja`). Django's doesn't declare debugpy, or it would shadow Python's `[debugger]`.
if (workflowSettings().debugger.breakpointsAnyFile || language === "django-html" || isJinjaTemplate(path)) {
if (workflowSettings().debugger.breakpointsAnyFile || language === "django-html" || isJinjaTemplate(path, language)) {
return true;
}
load();
@@ -70,6 +75,6 @@ export async function templateBreakpoints(): Promise<TemplateBreakpoints> {
const languages = await Promise.all(paths.map(languageOf));
return {
django: languages.includes("django-html"),
jinja: paths.some((path, i) => languages[i] !== "django-html" && isJinjaTemplate(path)),
jinja: paths.some((path, i) => languages[i] !== "django-html" && isJinjaTemplate(path, languages[i])),
};
}