Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/tui/src/highlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ const LANGUAGE_ALIASES: Readonly<Record<string, string>> = {

const FILE_LANGUAGES: Readonly<Record<string, string>> = {
dockerfile: "dockerfile",
containerfile: "dockerfile",
makefile: "makefile",
gnumakefile: "makefile",
cmakelists: "cmake",
"cmakelists.txt": "cmake",
gemfile: "ruby",
rakefile: "ruby",
vagrantfile: "ruby",
".bashrc": "bash",
".zshrc": "bash",
".bash_profile": "bash",
".profile": "bash",
};

function languageName(language: string | undefined): string | undefined {
Expand All @@ -75,6 +82,11 @@ export function languageForPath(path: string): string | undefined {
const name = path.split(/[\\/]/u).at(-1)?.toLowerCase() ?? "";
const named = FILE_LANGUAGES[name];
if (named !== undefined) return named;
// Dockerfile.* / Containerfile.* keep dockerfile highlighting even when the
// "extension" is an environment name (dev, prod, …) that is not a language.
if (name.startsWith("dockerfile.") || name.startsWith("containerfile.")) {
return "dockerfile";
}
const extension = name.includes(".") ? name.slice(name.lastIndexOf(".") + 1) : "";
return languageName(extension);
}
Expand Down
49 changes: 49 additions & 0 deletions packages/tui/test/highlight.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2026 Kaushik Kumar
// SPDX-License-Identifier: Apache-2.0

import assert from "node:assert/strict";
import test from "node:test";
import { languageForPath } from "../src/highlight.ts";

test("languageForPath maps exact file names", () => {
const cases: Array<[string, string | undefined]> = [
["Dockerfile", "dockerfile"],
["Containerfile", "dockerfile"],
["Makefile", "makefile"],
["GNUmakefile", "makefile"],
["CMakeLists.txt", "cmake"],
["Gemfile", "ruby"],
["Rakefile", "ruby"],
["Vagrantfile", "ruby"],
[".bashrc", "bash"],
[".zshrc", "bash"],
[".bash_profile", "bash"],
[".profile", "bash"],
];
for (const [path, expected] of cases) {
assert.equal(languageForPath(path), expected, path);
}
});

test("languageForPath maps Dockerfile.* and Containerfile.* prefixes", () => {
assert.equal(languageForPath("Dockerfile.dev"), "dockerfile");
assert.equal(languageForPath("Dockerfile.prod"), "dockerfile");
assert.equal(languageForPath("Containerfile.local"), "dockerfile");
});

test("languageForPath maps common extensions", () => {
assert.equal(languageForPath("src/app.ts"), "typescript");
assert.equal(languageForPath("main.py"), "python");
assert.equal(languageForPath("lib.rs"), "rust");
});

test("languageForPath is case-insensitive and accepts Windows paths", () => {
assert.equal(languageForPath("C:\\repo\\Dockerfile"), "dockerfile");
assert.equal(languageForPath("C:\\repo\\Containerfile"), "dockerfile");
assert.equal(languageForPath("packages\\TUI\\App.TS"), "typescript");
});

test("languageForPath returns undefined for unknown names", () => {
assert.equal(languageForPath("notes"), undefined);
assert.equal(languageForPath("archive.zzz"), undefined);
});