summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorChristoph Schlosser <christophschlosser@users.noreply.github.com>2019-06-28 23:56:15 +0200
committerGitHub <noreply@github.com>2019-06-28 23:56:15 +0200
commit75e169ef3aebcf7a75edba4fa28bee370782b00b (patch)
tree589fad4429487bce7efbbc8daa756a909ebdfcd6 /src
parent2d7a744527e469397a6dce6f27d60fac4382ee53 (diff)
parent903fdc1d30c288a9794ebcf9052112712b10080c (diff)
downloaddoxdocgen-75e169ef3aebcf7a75edba4fa28bee370782b00b.tar.gz
Merge pull request #116 from christophschlosser/indent-option
Indent option
Diffstat (limited to 'src')
-rw-r--r--src/Lang/Cpp/CppDocGen.ts45
-rw-r--r--src/test/CppTests/Config.test.ts31
2 files changed, 70 insertions, 6 deletions
diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts
index bc5a355..5d2483f 100644
--- a/src/Lang/Cpp/CppDocGen.ts
+++ b/src/Lang/Cpp/CppDocGen.ts
@@ -119,8 +119,34 @@ export class CppDocGen implements IDocGen {
return this.activeEditor.document.lineAt(this.activeEditor.selection.start.line).text.match("^\\s*")[0];
}
+ protected getIndentedTemplate(replace: string): string {
+ if (replace === undefined || replace === null || replace === "") {
+ return "";
+ }
+ const snippets = replace.split(/({indent:\d+})/);
+
+ let indentedString: string = "";
+ let indentWidth: number = 0;
+
+ // tslint:disable-next-line:prefer-for-of
+ snippets.forEach((element) => {
+ if (element.match(/{indent:\d+}/)) {
+ const indents = parseInt(element.match(/{indent:(\d+)}/)[1], 10);
+ indentWidth = indents;
+ const numSpaces = Math.max(indentWidth - indentedString.length, 0);
+ indentedString += " ".repeat(numSpaces);
+ } else {
+ // just some text
+ indentedString += element;
+ }
+ });
+
+ return indentedString;
+ }
+
protected getTemplatedString(replace: string, template: string, param: string): string {
- return template.replace(replace, param);
+ const replacedTemplate = template.replace(replace, param);
+ return this.getIndentedTemplate(replacedTemplate);
}
protected getMultiTemplatedString(replace: string[], template: string, param: string[]): string {
@@ -190,12 +216,15 @@ export class CppDocGen implements IDocGen {
}
protected generateBrief(lines: string[]) {
- lines.push(this.getTemplatedString(this.cfg.textTemplateReplace,
- this.cfg.C.commentPrefix + this.cfg.Generic.briefTemplate,
+ lines.push(this.cfg.C.commentPrefix + this.getTemplatedString(this.cfg.textTemplateReplace,
+ this.cfg.Generic.briefTemplate,
this.getSmartText()));
}
- protected generateFromTemplate(lines: string[], replace: string, template: string, templateWith: string[]) {
+ protected generateFromTemplate(lines: string[],
+ replace: string,
+ template: string,
+ templateWith: string[]) {
let line: string = "";
templateWith.forEach((element: string) => {
@@ -410,8 +439,12 @@ export class CppDocGen implements IDocGen {
case "param": {
if (this.cfg.Generic.paramTemplate.trim().length !== 0 && this.params.length > 0) {
const paramNames: string[] = this.params.map((p) => p.name);
- // tslint:disable-next-line:max-line-length
- this.generateFromTemplate(lines, this.cfg.paramTemplateReplace, this.cfg.Generic.paramTemplate, paramNames);
+ this.generateFromTemplate(
+ lines,
+ this.cfg.paramTemplateReplace,
+ this.cfg.Generic.paramTemplate,
+ paramNames,
+ );
}
break;
}
diff --git a/src/test/CppTests/Config.test.ts b/src/test/CppTests/Config.test.ts
index 1ef2a4d..b218de9 100644
--- a/src/test/CppTests/Config.test.ts
+++ b/src/test/CppTests/Config.test.ts
@@ -158,4 +158,35 @@ suite("C++ - Configuration Tests", () => {
const result = testSetup.SetLines(["*/", "int foo();"]).GetResult();
assert.equal("/**\n * @brief \n * \n * @return int \n */", result);
});
+
+ test("Single alignment", () => {
+ testSetup.cfg.Generic.paramTemplate = "@param{indent:10}{param}";
+ testSetup.cfg.Cpp.tparamTemplate = "@tparam{indent:10}{param}";
+ testSetup.cfg.Generic.returnTemplate = "@return{indent:10}{type}";
+ testSetup.cfg.Generic.briefTemplate = "@brief{indent:10}Brief";
+
+ const result = testSetup.SetLines(["template<typename T>", "int foo(std::string bar, T foobar);"]).GetResult();
+ // tslint:disable-next-line:max-line-length
+ assert.equal("/**\n * @brief Brief\n * \n * @tparam T\n * @param bar\n * @param foobar\n * @return int\n */", result);
+ });
+
+ test("Multi alignment", () => {
+ testSetup.cfg.Generic.paramTemplate = "@param{indent:10}{param}{indent:30}Parameters everywhere";
+ testSetup.cfg.Cpp.tparamTemplate = "@tparam{indent:10}{param}{indent:30}I'm a template";
+ testSetup.cfg.Generic.returnTemplate = "@return{indent:10}{type}{indent:30}Returns stuff";
+ testSetup.cfg.Generic.briefTemplate = "@brief{indent:30}Short desc";
+
+ const result = testSetup.SetLines(["template<typename T>", "int foo(std::string bar, T foobar);"]).GetResult();
+ // tslint:disable-next-line:max-line-length
+ 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 * @return int Returns stuff\n */", result);
+ });
+
+ test("Negative alignment tests", () => {
+ testSetup.cfg.Generic.returnTemplate = "";
+
+ const result = testSetup.SetLines(["template<typename T>", "int foo(std::string bar, T foobar);"]).GetResult();
+ // tslint:disable-next-line:max-line-length
+ 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);
+ });
+
});