summaryrefslogtreecommitdiffstats
path: root/src/Lang/Cpp/CppDocGen.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/Lang/Cpp/CppDocGen.ts')
-rw-r--r--src/Lang/Cpp/CppDocGen.ts134
1 files changed, 31 insertions, 103 deletions
diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts
index 05c8883..c0113ad 100644
--- a/src/Lang/Cpp/CppDocGen.ts
+++ b/src/Lang/Cpp/CppDocGen.ts
@@ -1,9 +1,10 @@
-import * as env from "env-var";
import * as moment from "moment";
import { Position, Range, Selection, TextEditor } from "vscode";
import { IDocGen } from "../../Common/IDocGen";
import { Config } from "../../Config";
import GitConfig from "../../GitConfig";
+import * as templates from "../../templatedString";
+import { getIndentation } from "../../util";
import { CppArgument } from "./CppArgument";
import * as CppParser from "./CppParser";
import { CppToken, CppTokenType } from "./CppToken";
@@ -111,79 +112,16 @@ export class CppDocGen implements IDocGen {
});
// Set cursor to first DoxyGen command.
- this.moveCursurToFirstDoxyCommand(comment,
- modifiedRangeToReplace.start.line,
- modifiedRangeToReplace.start.character);
+ this.moveCursurToFirstDoxyCommand(
+ comment,
+ modifiedRangeToReplace.start.line,
+ modifiedRangeToReplace.start.character,
+ );
}
/***************************************************************************
Implementation
***************************************************************************/
- protected getIndentation(): string {
- 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 getEnvVars(replace: string): string {
- let replacement = replace;
- const regex = /\$\{env\:([\w|\d|_]+)\}/m;
- let match: RegExpExecArray;
-
- // tslint:disable-next-line:no-conditional-assignment
- while ((match = regex.exec(replacement)) !== null) {
- if (match.index === regex.lastIndex) {
- regex.lastIndex++;
- }
-
- const m = match[1];
-
- const envVar: string = env.get(m, m).asString();
-
- replacement = replacement.replace("${env:" + m + "}", envVar);
- }
-
- return replacement;
- }
-
- protected getTemplatedString(replace: string, template: string, param: string): string {
- const replacedTemplate = template.replace(replace, param);
- const replacedWithEnv = this.getEnvVars(replacedTemplate);
- return this.getIndentedTemplate(replacedWithEnv);
- }
-
- protected getMultiTemplatedString(replace: string[], template: string, param: string[]): string {
- // For each replace entry, attempt to replace it with the corresponding param in the template
- for (let i = 0; i < replace.length; i++) {
- if (i < param.length) {
- template = template.replace(replace[i], param[i]);
- }
- }
- return this.getEnvVars(template);
- }
protected getSmartText(): string {
if (!this.cfg.Generic.generateSmartText) {
@@ -234,35 +172,20 @@ export class CppDocGen implements IDocGen {
return "";
}
}
- const str = this.getTemplatedString(this.cfg.nameTemplateReplace,
- text,
- val);
+ const str = templates.getTemplatedString(text, { toReplace: this.cfg.nameTemplateReplace, with: val });
this.smartTextLength = str.length;
return str;
}
protected generateBrief(lines: string[]) {
lines.push(
- ...this.getTemplatedString(
- this.cfg.textTemplateReplace,
+ ...templates.getTemplatedString(
this.cfg.Generic.briefTemplate,
- this.getSmartText(),
+ { toReplace: this.cfg.textTemplateReplace, with: this.getSmartText() },
).split("\n"),
);
}
- protected generateFromTemplate(lines: string[],
- replace: string,
- template: string,
- templateWith: string[]) {
- templateWith.forEach((element: string) => {
- // Ignore null values
- if (element !== null) {
- lines.push(...this.getTemplatedString(replace, template, element).split("\n"));
- }
- });
- }
-
protected generateReturnParams(): string[] {
const params: string[] = [];
@@ -303,10 +226,12 @@ export class CppDocGen implements IDocGen {
const authorInfo = this.getAuthorInfo();
// Allow substitution of {author} and {email} only
lines.push(
- ...this.getMultiTemplatedString(
- [this.cfg.authorTemplateReplace, this.cfg.emailTemplateReplace],
+ ...templates.getMultiTemplatedString(
this.cfg.Generic.authorTag,
- [authorInfo.authorName, authorInfo.authorEmail],
+ [
+ { toReplace: this.cfg.authorTemplateReplace, with: authorInfo.authorName },
+ { toReplace: this.cfg.emailTemplateReplace, with: authorInfo.authorEmail },
+ ],
).split("\n"),
);
}
@@ -314,7 +239,7 @@ export class CppDocGen implements IDocGen {
protected generateFilenameFromTemplate(lines: string[]) {
if (this.cfg.File.fileTemplate.trim().length !== 0) {
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.nameTemplateReplace,
this.cfg.File.fileTemplate,
@@ -332,7 +257,7 @@ export class CppDocGen implements IDocGen {
protected generateCopyrightTag(lines: string[]) {
// This currently only supports year substitution
this.cfg.File.copyrightTag.forEach((element) => {
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.yearTemplateReplace,
element,
@@ -392,14 +317,17 @@ export class CppDocGen implements IDocGen {
if (element !== "custom") { // Prevent recursive expansion
// Allow any of date, year, author, email to be replaced
lines.push(
- ...this.getMultiTemplatedString(
- [this.cfg.authorTemplateReplace, this.cfg.emailTemplateReplace,
- this.cfg.dateTemplateReplace, this.cfg.yearTemplateReplace],
+ ...templates.getMultiTemplatedString(
element,
- [authorInfo.authorName, authorInfo.authorEmail,
- moment().format(dateFormat), moment().format("YYYY")],
+ [
+ { toReplace: this.cfg.authorTemplateReplace, with: authorInfo.authorName },
+ { toReplace: this.cfg.emailTemplateReplace, with: authorInfo.authorEmail },
+ { toReplace: this.cfg.dateTemplateReplace, with: moment().format(dateFormat) },
+ { toReplace: this.cfg.yearTemplateReplace, with: moment().format("YYYY") },
+ { toReplace: "{file}", with: this.activeEditor.document.fileName.replace(/^.*[\\\/]/, "")},
+ ],
).split("\n"),
- ); // TODO: clean up this
+ );
}
});
}
@@ -407,7 +335,7 @@ export class CppDocGen implements IDocGen {
protected generateDateFromTemplate(lines: string[]) {
if (this.cfg.Generic.dateTemplate.trim().length !== 0 &&
this.cfg.Generic.dateFormat.trim().length !== 0) {
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.dateTemplateReplace,
this.cfg.Generic.dateTemplate,
@@ -467,7 +395,7 @@ export class CppDocGen implements IDocGen {
switch (element) {
case "tparam": {
if (this.cfg.Cpp.tparamTemplate.trim().length !== 0 && this.templateParams.length > 0) {
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.paramTemplateReplace,
this.cfg.Cpp.tparamTemplate,
@@ -479,7 +407,7 @@ 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);
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.paramTemplateReplace,
this.cfg.Generic.paramTemplate,
@@ -491,7 +419,7 @@ export class CppDocGen implements IDocGen {
case "return": {
if (this.cfg.Generic.returnTemplate.trim().length !== 0 && this.func.type !== null) {
const returnParams = this.generateReturnParams();
- this.generateFromTemplate(
+ templates.generateFromTemplate(
lines,
this.cfg.typeTemplateReplace,
this.cfg.Generic.returnTemplate,
@@ -514,7 +442,7 @@ export class CppDocGen implements IDocGen {
this.insertFirstLine(lines);
this.insertLastLine(lines);
- return lines.join(`\n${this.getIndentation()}`);
+ return lines.join(`\n${getIndentation(this.activeEditor)}`);
}
protected moveCursurToFirstDoxyCommand(comment: string, baseLine: number, baseCharacter) {