Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix syntax highlighting for .class files #3708

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
11 changes: 8 additions & 3 deletions core/util/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ World

it("should handle strings with only whitespace", () => {
const result = dedent`

`;
expect(result).toBe("");
});
Expand Down Expand Up @@ -249,9 +249,9 @@ World

it("should handle a string with only one non-empty line", () => {
const result = dedent`

Hello World!

`;
expect(result).toBe("Hello World!");
});
Expand Down Expand Up @@ -373,11 +373,16 @@ describe("getMarkdownLanguageTagForFile", () => {
expect(getMarkdownLanguageTagForFile("test.py")).toBe("python");
expect(getMarkdownLanguageTagForFile("test.tsx")).toBe("tsx");
expect(getMarkdownLanguageTagForFile("test.java")).toBe("java");
expect(getMarkdownLanguageTagForFile("test.class")).toBe("java");
expect(getMarkdownLanguageTagForFile("test.md")).toBe("markdown");
expect(getMarkdownLanguageTagForFile("test.sh")).toBe("shell");
expect(getMarkdownLanguageTagForFile("test.sql")).toBe("sql");
});

it("should sanitize ranges from the extension", () => {
expect(getMarkdownLanguageTagForFile("test.java (1-5)")).toBe("java");
});

it("should return the extension if not in the known list", () => {
expect(getMarkdownLanguageTagForFile("file.unknownext")).toBe("unknownext");
});
Expand Down
16 changes: 14 additions & 2 deletions core/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function getMarkdownLanguageTagForFile(filepath: string): string {
tsx: "tsx",
ts: "typescript",
java: "java",
class:"java", //.class files decompile to Java
go: "go",
rb: "ruby",
rs: "rust",
Expand All @@ -96,17 +97,28 @@ export function getMarkdownLanguageTagForFile(filepath: string): string {
ps1: "powershell",
};

const ext = filepath.split(".").pop();
const ext = sanitizeExtension(filepath.split(".").pop());
return ext ? (extToLangMap[ext] ?? ext) : "";
}


function sanitizeExtension(ext?: string): string|undefined {
if (ext) {
//ignore ranges in extension eg. "java (11-23)"
const match = ext.match(/^(\S+)\s*(\(.*\))?$/);
if (match) {
ext = match[1];
}
}
return ext;
}

export function copyOf(obj: any): any {
if (obj === null || obj === undefined) {
return obj;
}
return JSON.parse(JSON.stringify(obj));
}
``;

export function deduplicateArray<T>(
array: T[],
Expand Down
Loading