Skip to content

Commit 79d501a

Browse files
committed
[ADD] handling for header prepend and append options
1 parent 0921ed0 commit 79d501a

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

vscode/asperheader/src/modules/commentGenerator.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -598,30 +598,34 @@ export class CommentGenerator {
598598
this.documentVersion = this.documentBody.version;
599599
}
600600

601-
private prependIfPresent(buildHeader: string[]): string[] {
602-
603-
// Apply language-specific prepend if configured
604-
const languageId = this.languageId?.toLowerCase() || "";
605-
const prependConfig = this.Config.get("languagePrepend");
606-
if (prependConfig && prependConfig[languageId]) {
607-
buildHeader.push(prependConfig[languageId]);
608-
logger.debug(getMessage("languagePrependApplied", languageId));
601+
private prependIfPresent(buildHeader: string[], eol: vscode.EndOfLine, languageId?: string): string[] {
602+
if (languageId === undefined) {
603+
return buildHeader;
604+
}
605+
const instance = this.languageAppend[languageId];
606+
if (instance !== undefined) {
607+
if (Array.isArray(instance)) {
608+
buildHeader.push(instance.join(this.determineNewLine(eol)));
609+
} else {
610+
buildHeader.push(this.languageAppend[languageId]);
611+
}
612+
logger.debug(getMessage("languageAppendApplied", languageId));
609613
}
610614
return buildHeader;
611615
}
612616

613-
private appendIfPresent(buildHeader: string[]): string[] {
614-
// Apply language-specific append if configured
615-
const appendConfig = this.Config.get("languageAppend");
616-
if (appendConfig && appendConfig[languageId]) {
617-
buildHeader.push(appendConfig[languageId]);
618-
logger.debug(getMessage("languageAppendApplied", languageId));
617+
private appendIfPresent(buildHeader: string[], eol: vscode.EndOfLine, languageId?: string): string[] {
618+
if (languageId === undefined) {
619+
return buildHeader;
619620
}
620-
621-
// Remove trailing spaces if configured
622-
if (this.Config.get("removeTrailingHeaderSpaces")) {
623-
buildHeader = buildHeader.map(line => line.trimEnd());
624-
logger.debug(getMessage("trailingSpacesRemoved"));
621+
const instance = this.languageAppend[languageId];
622+
if (instance !== undefined) {
623+
if (Array.isArray(instance)) {
624+
buildHeader.push(instance.join(this.determineNewLine(eol)));
625+
} else {
626+
buildHeader.push(this.languageAppend[languageId]);
627+
}
628+
logger.debug(getMessage("languageAppendApplied", languageId));
625629
}
626630
return buildHeader;
627631
}
@@ -718,7 +722,7 @@ export class CommentGenerator {
718722
* - Telegraph-style closing markers
719723
* - Closing comment delimiter
720724
*/
721-
private async buildTheHeader(comments: string[]): Promise<string[]> {
725+
private async buildTheHeader(comments: string[], languageId?: string): Promise<string[]> {
722726
logger.debug(getMessage("inFunction", "buildTheHeader", "CommentGenerator"));
723727
const eol: vscode.EndOfLine = this.documentEOL || vscode.EndOfLine.LF;
724728
const unknownTerm: string = getMessage("unknown");
@@ -727,7 +731,7 @@ export class CommentGenerator {
727731
const commentCloser: string = comments[2] || "";
728732
let buildHeader: string[] = [];
729733
// Check wether there are elements required to be added before the header
730-
buildHeader = this.prependIfPresent(buildHeader);
734+
buildHeader = this.prependIfPresent(buildHeader, eol, languageId);
731735
// Preparing the header content so that it can be put in a comment and written.
732736
if (commentOpener.length > 0) {
733737
buildHeader.push(`${commentOpener}${this.determineNewLine(eol)}`);
@@ -773,7 +777,7 @@ export class CommentGenerator {
773777
buildHeader.push(`${commentCloser}${this.determineNewLine(eol)}`);
774778
}
775779

776-
buildHeader = this.appendIfPresent(buildHeader);
780+
buildHeader = this.appendIfPresent(buildHeader, eol, languageId);
777781

778782
return buildHeader;
779783
}
@@ -904,9 +908,9 @@ export class CommentGenerator {
904908
* Generates a complete header using buildTheHeader() and inserts it at the
905909
* top of the document. Handles shebang line detection and offset calculation.
906910
*/
907-
private async writeHeaderToFile(document: vscode.TextDocument, comments: string[]): Promise<number> {
911+
private async writeHeaderToFile(document: vscode.TextDocument, comments: string[], languageId?: string): Promise<number> {
908912
logger.debug(getMessage("inFunction", "writeHeaderToFile", "CommentGenerator"));
909-
const headerLines: string[] = await this.buildTheHeader(comments);
913+
const headerLines: string[] = await this.buildTheHeader(comments, languageId);
910914
const headerText: string = headerLines.join(this.determineNewLine(document.eol));
911915
let insertPosition = 0;
912916
if (document.lineCount > 0) {
@@ -982,7 +986,7 @@ export class CommentGenerator {
982986
return;
983987
}
984988
if (response === false) {
985-
let status: number = await this.writeHeaderToFile(editor.document, comments);
989+
let status: number = await this.writeHeaderToFile(editor.document, comments, determineComment.language);
986990
if (status === this.Config.get("statusError")) {
987991
logger.Gui.error(getMessage("headerWriteFailed"));
988992
return;
@@ -1097,7 +1101,7 @@ export class CommentGenerator {
10971101
logger.Gui.info(getMessage("headerInjectQuestionRefused"));
10981102
return;
10991103
}
1100-
const status: number = await this.writeHeaderToFile(document, comments);
1104+
const status: number = await this.writeHeaderToFile(document, comments, determineComment.language);
11011105
if (status === this.Config.get("statusError")) {
11021106
logger.Gui.error(getMessage("headerWriteFailed"));
11031107
return;

0 commit comments

Comments
 (0)