diff --git a/README.md b/README.md index 3a5b4f9..41f8adc 100644 --- a/README.md +++ b/README.md @@ -19,9 +19,22 @@ You can create a header for a new file by: Each time you save a C or C++ file with a header, it will update it automatically. -You can configure the headers in a very simple manner by executing the `epitech-c-cpp-headers.setConfig` command. +You can configure the headers in a very simple manner by executing the `epitech-c-cpp-headers.setConfig` command. By default, it will execute it on launch when a configuration field is left unknown. +It is also possible to replace the regular Epitech header with a custom one by setting the `epitech-c-cpp-headers.customHeader` configuration field. + +```json +{ + "epitech-c-cpp-headers.customHeader": [ + "HEADER LINE 1", + "HEADER LINE 2", + "HEADER LINE 3", + "..." + ] +} +``` + Requirements ------------ @@ -37,6 +50,7 @@ This extension uses the following configuration entries: - `epitech-c-cpp-headers.prompt`: Specify if the extension should prompt for unknown configuration fields (default: true) - `epitech-c-cpp-headers.usePragmaOnce`: Specifies if '#pragma once' should be used as header guard instead of '#ifndef ...' (default: false) - `epitech-c-cpp-headers.autoGenerateClasses`: Specifies if automatic C++ class generation is enabled (default: true) +- `epitech-c-cpp-headers.customHeader`: Specifies a custom header to use instead of the regular Epitech header (default: null) Known Issues ------------ @@ -52,7 +66,7 @@ Initial release ### 1.1.0 -Added support for Makefiles +Added support for Makefiles Added better internal multi-language handling ### 1.2.0 @@ -61,9 +75,9 @@ Added prompt to improve UX while configuring ### 1.3.0 -Added the `epitech-c-cpp-headers.setConfig` command to reconfigure headers -Fixed regex escape issues -Fixed possible line mismatch on date updates +Added the `epitech-c-cpp-headers.setConfig` command to reconfigure headers +Fixed regex escape issues +Fixed possible line mismatch on date updates Fixed date update on Makefiles ### 1.3.1 @@ -80,45 +94,45 @@ Fixed issues in README.md ### 1.5.0 -Added support for Python, Shell scripts, C#, Objective-C, Java, LaTeX -Added better support for C++ -Added End-of-line encoding independance, for inter-operability between different OSs +Added support for Python, Shell scripts, C#, Objective-C, Java, LaTeX +Added better support for C++ +Added End-of-line encoding independance, for inter-operability between different OSs ### 1.6.0 -Added auto-insertion of header define guards for C/C++ empty header files -Added cursor auto-positionning, to set it ready to type +Added auto-insertion of header define guards for C/C++ empty header files +Added cursor auto-positionning, to set it ready to type ### 1.7.0 -Added internal multi-format support -Implemented the new 2017 header format (Promo 2022) +Added internal multi-format support +Implemented the new 2017 header format (Promo 2022) ### 1.7.3 -Changed pre-processors indent style for the 2017 header format +Changed pre-processors indent style for the 2017 header format ### 1.7.4 -Fixed C++ headers -Fixed issue with config being saved as local instead of global +Fixed C++ headers +Fixed issue with config being saved as local instead of global ### 1.8.0 -Added support for '#pragma once' header guards (Thanks to @lodi-g for the pull request) -Added C++ automatic class generator in source and header files +Added support for '#pragma once' header guards (Thanks to @lodi-g for the pull request) +Added C++ automatic class generator in source and header files ### 1.8.1 -Fixed scolar year in post2017 headers +Fixed scolar year in post2017 headers ### 1.8.2 -Fixed the day in pre2017 headers (Thanks to @SaShimy for the pull request) +Fixed the day in pre2017 headers (Thanks to @SaShimy for the pull request) ### 1.8.3 -Reverted the 1.8.1 patch as it was not correct to begin with +Reverted the 1.8.1 patch as it was not correct to begin with ### 1.9.0 @@ -126,17 +140,17 @@ Added header auto-filling (with workspace name and filename, thanks to @lodi-g f ### 1.9.1 -Fixed issues with auto-filled headers (Thanks to @MrYannKee for the bug report and pull request) -Fixed empty auto-filled headers for files without extensions (like Makefile) +Fixed issues with auto-filled headers (Thanks to @MrYannKee for the bug report and pull request) +Fixed empty auto-filled headers for files without extensions (like Makefile) ### 1.9.2 -Added support for Rust files and C++ template files +Added support for Rust files and C++ template files ### 1.9.5 -Added support for Haskell files -Fixed issue where normal files could be flagged as C/C++ files for header generation +Added support for Haskell files +Fixed issue where normal files could be flagged as C/C++ files for header generation ### 1.12.0 @@ -148,16 +162,16 @@ Added ability to rename **`#ifndef`** header guards (Thanks to @ColColty for the ### 1.14.0 -Added setting to toggle indentation of visibility specifiers in generated C++ class declarations. -Indentation in generated code now respects user's preferences instead of always using tabs. +Added setting to toggle indentation of visibility specifiers in generated C++ class declarations. +Indentation in generated code now respects user's preferences instead of always using tabs. (Thanks to @Gr1moire for the pull request introducing these two features) ### 1.15.0 -Added support for Assembly files -Added support for HTML files, CSS files and JavaScript/TypeScript files (Thanks to @IchiiDev for the pull request) -Improved comment style for Haskell files (Thanks to @enzo-cesaretti for the pull request) +Added support for Assembly files +Added support for HTML files, CSS files and JavaScript/TypeScript files (Thanks to @IchiiDev for the pull request) +Improved comment style for Haskell files (Thanks to @enzo-cesaretti for the pull request) ### 1.16.0 -Added support for Makefile `.mk` files (Thanks to @C0Florent for the pull request) +Added support for Makefile `.mk` files (Thanks to @C0Florent for the pull request) diff --git a/package.json b/package.json index ac93b11..76cbb65 100644 --- a/package.json +++ b/package.json @@ -97,6 +97,11 @@ "type": "boolean", "default": true, "description": "Specifies if access specifiers indentation is enabled." + }, + "epitech-c-cpp-headers.customHeader": { + "type": "array", + "default": [], + "description": "Specifies a custom header to use instead of the regular Epitech header." } } } diff --git a/src/commands/add_header.ts b/src/commands/add_header.ts index 080d0df..bab73dc 100644 --- a/src/commands/add_header.ts +++ b/src/commands/add_header.ts @@ -107,7 +107,7 @@ export async function runAddHeader() { const extension = path.basename(fileInfo.fileName).split(".").reverse()[0]; if (!isSupportedExtension(extension)) { - vscode.window.showErrorMessage("The currently opened file (" + extension + ") isn't a supported file."); + vscode.window.showErrorMessage(`The currently opened file (${extension}) isn't a supported file.`); return; } @@ -126,7 +126,7 @@ export async function runAddHeader() { fileInfo.description = description; } - let editContent = GENERATORS[config.headerType](fileInfo, config, date); + let editContent = GENERATORS[(config.customHeader.length > 0) ? "custom" : config.headerType](fileInfo, config, date); let offsetY = SYNTAX[config.headerType].offsetHeaderFile; let offsetX = 0; @@ -169,4 +169,4 @@ export async function runAddHeader() { const pos = new vscode.Position(offsetY, offsetX); fileInfo.editor.selection = new vscode.Selection(pos, pos); } -} \ No newline at end of file +} diff --git a/src/config.ts b/src/config.ts index 8276c4c..09709b6 100644 --- a/src/config.ts +++ b/src/config.ts @@ -83,6 +83,7 @@ export function loadConfig(): Config { usePragmaOnce: z.boolean().default(false), autoGenerateClasses: z.boolean().default(true), indentAccessSpecifiers: z.boolean().default(true), + customHeader: z.array(z.string()).default([]), }).parse(handle); return { ...config, handle }; diff --git a/src/generators.ts b/src/generators.ts index 1afa63f..ee722dc 100644 --- a/src/generators.ts +++ b/src/generators.ts @@ -6,7 +6,8 @@ import { Config, FileInfo, HeaderGenerator as HeaderGenerators } from "./types"; export const GENERATORS: HeaderGenerators = { pre2017: generatePre2017Header, - post2017: generatePost2017Header + post2017: generatePost2017Header, + custom: generateCustomHeader, }; function generatePre2017Header(fileInfo: FileInfo, config: Config, date: Date): string { @@ -39,6 +40,32 @@ function generatePost2017Header(fileInfo: FileInfo, _config: Config, date: Date) ); } +function generateCustomHeader(fileInfo: FileInfo, config: Config): string { + const editContent = ""; + return editContent.concat( + SYNTAX.commentStart[fileInfo.langId], fileInfo.eol, + config.customHeader.map((line) => SYNTAX.commentMid[fileInfo.langId] + " " + customArgs(fileInfo, line) + fileInfo.eol).join(""), + SYNTAX.commentEnd[fileInfo.langId], fileInfo.eol, fileInfo.eol, + ); +} + +// customArgs() replaces the placeholders in the custom header with the actual values. +// It replaces: +// %FILENAME% with the name of the file. +// %PROJECTNAME% with the name of the project. +// %FILEPATH% with the path of the file. +// %DESCRIPTION% with the description of the file. +// It returns the modified line. +// To add a new placeholder, add a new line with the following format: +// line = line.replace(/%PLACEHOLDER%/g, newValue); +function customArgs(fileInfo: FileInfo, line: string) { + line = line.replace(/%FILENAME%/g, path.basename(fileInfo.fileName)); + line = line.replace(/%PROJECTNAME%/g, fileInfo.projName); + line = line.replace(/%FILEPATH%/g, path.dirname(fileInfo.fileName)); + line = line.replace(/%DESCRIPTION%/g, fileInfo.description as string); + return line; +} + // getTabType() generates a tab string based on the user's editors settings. // If the insertSpaces setting is false : it will just return a '\t'. // If it's set to true : it will check the tabSize setting and return a string of that length and made of whitespaces. diff --git a/src/types.ts b/src/types.ts index a8afde5..b85d6dd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,6 +26,7 @@ export type Config = { usePragmaOnce: boolean; autoGenerateClasses: boolean; indentAccessSpecifiers: boolean; + customHeader: string[]; }; export type HeaderGenerator = {