Skip to content

Commit

Permalink
[refactor] manage comment formats and improve test code using a looku…
Browse files Browse the repository at this point in the history
…p table
  • Loading branch information
glassk authored and yrkim-auto committed May 2, 2024
1 parent a3c370f commit 00e512f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 169 deletions.
18 changes: 3 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,11 @@ export function activate(context: vscode.ExtensionContext) {
const fileIdentifier = includeFilePath
? document.uri.path
: document.uri.path.split("/").pop() || "Untitled";

try {
const comment = generateFileNameComment(document.languageId, fileIdentifier);
const textToCopy = `${comment}\n${text}`;
await vscode.env.clipboard.writeText(textToCopy);
vscode.window.showInformationMessage("Text with file identifier copied to clipboard!");
} catch (error: unknown) {
if (error instanceof Error) {
vscode.window.showErrorMessage(`Error: ${error.message}`);
} else {
vscode.window.showErrorMessage(`An unexpected error occurred`);
}
}

const comment = generateFileNameComment(document.languageId, fileIdentifier);

vscode.env.clipboard.writeText(`${comment}\n${text}`);
await vscode.env.clipboard.writeText(`${comment}\n${text}`);

vscode.window.showInformationMessage("Text with file identifier copied to clipboard!");
});

context.subscriptions.push(disposable);
Expand Down
83 changes: 34 additions & 49 deletions src/test/utils/generateFileNameComment.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,39 @@
import assert from "assert";
import { generateFileNameComment } from "../../utils/generateFileNameComment";

describe("generateFileNameComment()", () => {
it("should generate correct comment for JavaScript", () => {
const result = generateFileNameComment("javascript", "example.js");
assert.strictEqual(result, "// example.js");
});

it("should generate correct comment for Python", () => {
const result = generateFileNameComment("python", "example.py");
assert.strictEqual(result, "# example.py");
});

it("should generate correct comment for C++", () => {
const result = generateFileNameComment("cpp", "example.cpp");
assert.strictEqual(result, "// example.cpp");
});

it("should generate correct comment for HTML", () => {
const result = generateFileNameComment("html", "example.html");
assert.strictEqual(result, "<!-- example.html -->");
});

it("should generate correct comment for CSS", () => {
const result = generateFileNameComment("css", "example.css");
assert.strictEqual(result, "/* example.css */");
});

it("should handle plaintext without comment syntax", () => {
const result = generateFileNameComment("plaintext", "example.txt");
assert.strictEqual(result, "example.txt");
});

it("should generate correct comment for SQL", () => {
const result = generateFileNameComment("sql", "example.sql");
assert.strictEqual(result, "-- example.sql");
});

it("should generate correct comment for Bat scripts", () => {
const result = generateFileNameComment("bat", "example.bat");
assert.strictEqual(result, "REM example.bat");
});

it("should return default comment for unsupported language", () => {
const result = generateFileNameComment("unsupported-language", "example.unknown");
assert.strictEqual(result, "// example.unknown");
});

it("should generate correct comment for JSONC", () => {
const result = generateFileNameComment("jsonc", "example.jsonc");
assert.strictEqual(result, "// example.jsonc");
describe("generateFileNameComment", function () {
const testCases: Record<
string,
{ languageId: string; fileIdentifier: string; expectedComment: string }
> = {
javascript: {
languageId: "javascript",
fileIdentifier: "example.js",
expectedComment: "// example.js",
},
python: { languageId: "python", fileIdentifier: "example.py", expectedComment: "# example.py" },
html: {
languageId: "html",
fileIdentifier: "example.html",
expectedComment: "<!-- example.html -->",
},
css: { languageId: "css", fileIdentifier: "example.css", expectedComment: "/* example.css */" },
latex: { languageId: "latex", fileIdentifier: "example.tex", expectedComment: "% example.tex" },
plaintext: {
languageId: "plaintext",
fileIdentifier: "example.txt",
expectedComment: "example.txt",
},
unrecognized: {
languageId: "nonexistent-language",
fileIdentifier: "example.unknown",
expectedComment: "// example.unknown",
},
};

Object.values(testCases).forEach(({ languageId, fileIdentifier, expectedComment }) => {
it(`should return correct comment format for ${languageId}`, function () {
assert.strictEqual(generateFileNameComment(languageId, fileIdentifier), expectedComment);
});
});
});
180 changes: 75 additions & 105 deletions src/utils/generateFileNameComment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,82 @@
* @returns Comment with file name and prefix/suffix based on the given language identifier
*/
export function generateFileNameComment(languageId: string, fileIdentifier: string): string {
let comment: string;
const commentStyles: Record<string, string[]> = {
"double-slash": [
"c",
"cpp",
"csharp",
"cuda-cpp",
"d",
"go",
"groovy",
"java",
"javascript",
"javascriptreact",
"jsonc",
"objective-c",
"objective-cpp",
"rust",
"swift",
"typescript",
"typescriptreact",
"shaderlab",
],
hash: [
"coffeescript",
"perl",
"python",
"r",
"ruby",
"shellscript",
"yaml",
"dockercompose",
"dockerfile",
"erlang",
"git-commit",
"git-rebase",
"ini",
"makefile",
"powershell",
],
"html-like": ["html", "markdown", "svelte", "vue", "vue-html", "xml", "xsl"],
"multi-line": ["css", "less", "scss", "stylus"],
percent: ["bibtex", "latex", "tex"],
semicolon: ["clojure"],
"lua-style": ["lua"],
"razor-style": ["razor"],
"sql-style": ["sql"],
"vb-style": ["vb"],
"handlebars-style": ["handlebars"],
"abap-style": ["abap"],
"batch-style": ["bat"],
"fsharp-style": ["fsharp", "haskell", "ocaml"],
plaintext: ["plaintext"],
};

switch (languageId) {
case "abap":
comment = `" ${fileIdentifier}`;
break;
case "bat":
comment = `REM ${fileIdentifier}`;
break;
case "bibtex":
case "latex":
case "tex":
comment = `% ${fileIdentifier}`;
break;
case "clojure":
comment = `;; ${fileIdentifier}`;
break;
case "coffeescript":
case "perl":
case "python":
case "r":
case "ruby":
case "shellscript":
case "yaml":
comment = `# ${fileIdentifier}`;
break;
case "c":
case "cpp":
case "csharp":
case "cuda-cpp":
case "d":
case "go":
case "groovy":
case "java":
case "javascript":
case "javascriptreact":
case "json": // Normally, JSON doesn't support comments, but provided for completeness
case "jsonc":
case "objective-c":
case "objective-cpp":
case "rust":
case "swift":
case "typescript":
case "typescriptreact":
comment = `// ${fileIdentifier}`;
break;
case "css":
case "less":
case "scss":
case "stylus":
comment = `/* ${fileIdentifier} */`;
break;
case "dockercompose":
case "dockerfile":
case "erlang":
case "git-commit":
case "git-rebase":
case "ini":
case "makefile":
case "powershell":
comment = `# ${fileIdentifier}`;
break;
case "html":
case "markdown":
case "svelte":
case "vue":
case "vue-html":
case "xml":
case "xsl":
comment = `<!-- ${fileIdentifier} -->`;
break;
case "haml":
case "pug":
case "slim":
comment = `// ${fileIdentifier}`;
break;
case "fsharp":
case "haskell":
case "ocaml":
comment = `(* ${fileIdentifier} *)`;
break;
case "handlebars":
comment = `{{!-- ${fileIdentifier} --}}`;
break;
case "lua":
comment = `-- ${fileIdentifier}`;
break;
case "razor":
comment = `@* ${fileIdentifier} *@`;
break;
case "sql":
comment = `-- ${fileIdentifier}`;
break;
case "vb":
comment = `' ${fileIdentifier}`;
break;
case "shaderlab":
comment = `// ${fileIdentifier}`;
break;
case "plaintext":
comment = fileIdentifier; // Plain text has no comment syntax
break;
default:
comment = `// ${fileIdentifier}`; // Default comment style
break;
const commentPrefixes: Record<string, string> = {
"double-slash": `// ${fileIdentifier}`,
hash: `# ${fileIdentifier}`,
"html-like": `<!-- ${fileIdentifier} -->`,
"multi-line": `/* ${fileIdentifier} */`,
percent: `% ${fileIdentifier}`,
semicolon: `;; ${fileIdentifier}`,
"lua-style": `-- ${fileIdentifier}`,
"razor-style": `@* ${fileIdentifier} *@`,
"sql-style": `-- ${fileIdentifier}`,
"vb-style": `' ${fileIdentifier}`,
"handlebars-style": `{{!-- ${fileIdentifier} --}}`,
"abap-style": `" ${fileIdentifier}`,
"batch-style": `REM ${fileIdentifier}`,
"fsharp-style": `(* ${fileIdentifier} *)`,
plaintext: fileIdentifier,
};

for (const [style, languages] of Object.entries(commentStyles)) {
if (languages.includes(languageId)) {
return commentPrefixes[style];
}
}

return comment;
return `// ${fileIdentifier}`;
}

0 comments on commit 00e512f

Please sign in to comment.