From 34bd62742eaf7ce6e408099e27ed8988ffc9401c Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Sat, 19 Oct 2019 20:14:53 +0800 Subject: Add support for multiline template --- src/Lang/Cpp/CppDocGen.ts | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts index 5d2483f..16aae4e 100644 --- a/src/Lang/Cpp/CppDocGen.ts +++ b/src/Lang/Cpp/CppDocGen.ts @@ -216,23 +216,23 @@ export class CppDocGen implements IDocGen { } protected generateBrief(lines: string[]) { - lines.push(this.cfg.C.commentPrefix + this.getTemplatedString(this.cfg.textTemplateReplace, - this.cfg.Generic.briefTemplate, - this.getSmartText())); + lines.push( + ...this.getTemplatedString( + this.cfg.textTemplateReplace, + this.cfg.Generic.briefTemplate, + this.getSmartText(), + ).split("\n"), + ); } protected generateFromTemplate(lines: string[], replace: string, template: string, templateWith: string[]) { - let line: string = ""; - templateWith.forEach((element: string) => { // Ignore null values if (element !== null) { - line = this.cfg.C.commentPrefix; - line += this.getTemplatedString(replace, template, element); - lines.push(line); + lines.push(...this.getTemplatedString(replace, template, element).split("\n")); } }); } @@ -271,12 +271,12 @@ export class CppDocGen implements IDocGen { protected generateAuthorTag(lines: string[]) { if (this.cfg.Generic.authorTag.trim().length !== 0) { // Allow substitution of {author} and {email} only - lines.push(this.cfg.C.commentPrefix + - this.getMultiTemplatedString( + lines.push( + ...this.getMultiTemplatedString( [this.cfg.authorTemplateReplace, this.cfg.emailTemplateReplace], this.cfg.Generic.authorTag, [this.cfg.Generic.authorName, this.cfg.Generic.authorEmail], - ), + ).split("\n"), ); } } @@ -294,7 +294,7 @@ export class CppDocGen implements IDocGen { protected generateVersionTag(lines: string[]) { if (this.cfg.File.versionTag.trim().length !== 0) { - lines.push(this.cfg.C.commentPrefix + this.cfg.File.versionTag); + lines.push(...this.cfg.File.versionTag.split("\n")); } } @@ -318,14 +318,14 @@ export class CppDocGen implements IDocGen { // For each line of the customTag this.cfg.File.customTag.forEach((element) => { // Allow any of date, year, author, email to be replaced - lines.push(this.cfg.C.commentPrefix + - this.getMultiTemplatedString( + lines.push( + ...this.getMultiTemplatedString( [this.cfg.authorTemplateReplace, this.cfg.emailTemplateReplace, this.cfg.dateTemplateReplace, this.cfg.yearTemplateReplace], element, [this.cfg.Generic.authorName, this.cfg.Generic.authorEmail, moment().format(dateFormat), moment().format("YYYY")], - ), + ).split("\n"), ); }); } @@ -356,7 +356,7 @@ export class CppDocGen implements IDocGen { protected insertLastLine(lines: string[]) { if (this.cfg.C.lastLine.trim().length !== 0) { - lines.push(this.cfg.C.lastLine); + lines[lines.length - 1] += `\n${this.cfg.C.lastLine}`; } } @@ -372,7 +372,7 @@ export class CppDocGen implements IDocGen { break; } case "empty": { - lines.push(this.cfg.C.commentPrefix); + lines.push(""); break; } case "file": { @@ -407,7 +407,7 @@ export class CppDocGen implements IDocGen { this.insertLastLine(lines); - return lines.join("\n"); + return lines.join(`\n${this.cfg.C.commentPrefix}`); } protected generateComment(): string { @@ -422,7 +422,7 @@ export class CppDocGen implements IDocGen { break; } case "empty": { - lines.push(this.cfg.C.commentPrefix); + lines.push(""); break; } case "tparam": { @@ -464,8 +464,7 @@ export class CppDocGen implements IDocGen { this.insertLastLine(lines); - const comment: string = lines.join("\n" + this.getIndentation()); - return comment; + return lines.join(`\n${this.getIndentation()}${this.cfg.C.commentPrefix}`); } protected moveCursurToFirstDoxyCommand(comment: string, baseLine: number, baseCharacter) { -- cgit v1.2.3 From effaa807f150653418eeb9f0e2825a15cc65e6b0 Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Sat, 19 Oct 2019 21:27:58 +0800 Subject: Fix failed tests --- src/Lang/Cpp/CppDocGen.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts index 16aae4e..7be7da9 100644 --- a/src/Lang/Cpp/CppDocGen.ts +++ b/src/Lang/Cpp/CppDocGen.ts @@ -344,7 +344,7 @@ export class CppDocGen implements IDocGen { protected insertFirstLine(lines: string[]) { if (this.cfg.C.firstLine.trim().length !== 0) { - lines.push(this.cfg.C.firstLine); + lines.unshift(this.cfg.C.firstLine); } } @@ -356,14 +356,12 @@ export class CppDocGen implements IDocGen { protected insertLastLine(lines: string[]) { if (this.cfg.C.lastLine.trim().length !== 0) { - lines[lines.length - 1] += `\n${this.cfg.C.lastLine}`; + lines.push(this.cfg.C.lastLine); } } protected generateFileDescription(): string { - const lines: string[] = []; - - this.insertFirstLine(lines); + let lines: string[] = []; this.cfg.File.fileOrder.forEach((element) => { switch (element) { @@ -405,15 +403,15 @@ export class CppDocGen implements IDocGen { } }); + lines = lines.map((line) => `${this.cfg.C.commentPrefix}${line}`); + this.insertFirstLine(lines); this.insertLastLine(lines); - return lines.join(`\n${this.cfg.C.commentPrefix}`); + return lines.join("\n"); } protected generateComment(): string { - const lines: string[] = []; - - this.insertFirstLine(lines); + let lines: string[] = []; this.cfg.Generic.order.forEach((element) => { switch (element) { @@ -462,9 +460,11 @@ export class CppDocGen implements IDocGen { } }); + lines = lines.map((line) => `${this.cfg.C.commentPrefix}${line}`); + this.insertFirstLine(lines); this.insertLastLine(lines); - return lines.join(`\n${this.getIndentation()}${this.cfg.C.commentPrefix}`); + return lines.join(`\n${this.getIndentation()}`); } protected moveCursurToFirstDoxyCommand(comment: string, baseLine: number, baseCharacter) { -- cgit v1.2.3 From 5648d5ed96d6150f14530f14df5842dc248bf4be Mon Sep 17 00:00:00 2001 From: EternalPhane Date: Sat, 16 Nov 2019 11:11:08 +0800 Subject: add unit test --- src/test/CppTests/Config.test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/CppTests/Config.test.ts b/src/test/CppTests/Config.test.ts index b218de9..bfccf89 100644 --- a/src/test/CppTests/Config.test.ts +++ b/src/test/CppTests/Config.test.ts @@ -189,4 +189,26 @@ suite("C++ - Configuration Tests", () => { assert.equal("/**\n * @brief Short desc\n * \n * @tparam T I'm a template\n * @param bar Parameters everywhere\n * @param foobar Parameters everywhere\n */", result); }); + test("Multiline template", () => { + testSetup.cfg.C.commentPrefix = "/// "; + testSetup.cfg.C.firstLine = ""; + testSetup.cfg.C.lastLine = ""; + testSetup.cfg.Generic.briefTemplate = "\n{text}\n"; + testSetup.cfg.Generic.paramTemplate = "\n"; + testSetup.cfg.Generic.returnTemplate = "\n"; + const result = testSetup.SetLine(" int foo(bool a);").GetResult(); + // tslint:disable:no-trailing-whitespace + assert.equal( + result, `/// + /// + /// + /// + /// + /// + /// + /// `, + ); + // tslint:enable:no-trailing-whitespace + }); + }); -- cgit v1.2.3