summaryrefslogtreecommitdiffstats
path: root/src/Lang
diff options
context:
space:
mode:
authorChristoph Schlosser <christoph@linux.com>2018-03-02 17:02:02 +0100
committerChristoph Schlosser <christophschlosser@users.noreply.github.com>2018-03-02 20:24:28 +0100
commitf062b00a6ca2a5f42eb376b178735b766832bbcb (patch)
tree2879fb8b19e8b6573a680498ab9cfe3d4edb7af5 /src/Lang
parent5368c8151df632ba0ebd863cbc7ff105dc4027bf (diff)
downloaddoxdocgen-f062b00a6ca2a5f42eb376b178735b766832bbcb.tar.gz
Add more smart text
- Getter - Setter - Factory method
Diffstat (limited to 'src/Lang')
-rw-r--r--src/Lang/Cpp/CppDocGen.ts88
-rw-r--r--src/Lang/Cpp/CppParser.ts45
2 files changed, 121 insertions, 12 deletions
diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts
index eb60c9a..d93c858 100644
--- a/src/Lang/Cpp/CppDocGen.ts
+++ b/src/Lang/Cpp/CppDocGen.ts
@@ -10,6 +10,9 @@ export enum SpecialCase {
none,
constructor,
destructor,
+ getter,
+ setter,
+ factoryMethod,
}
export enum CommentType {
@@ -17,6 +20,15 @@ export enum CommentType {
file,
}
+export enum CasingType {
+ Pascal,
+ camel,
+ snake,
+ SCREAMING_SNAKE,
+ UPPER,
+ uncertain,
+}
+
export class CppDocGen implements IDocGen {
protected activeEditor: TextEditor;
@@ -28,6 +40,7 @@ export class CppDocGen implements IDocGen {
protected specialCase: SpecialCase;
protected commentType: CommentType;
+ protected casingType: CasingType;
protected smartTextLength: number;
@@ -48,6 +61,7 @@ export class CppDocGen implements IDocGen {
params: CppArgument[],
specialCase: SpecialCase,
commentType: CommentType,
+ casingType: CasingType,
) {
this.activeEditor = actEdit;
this.cfg = cfg;
@@ -57,6 +71,7 @@ export class CppDocGen implements IDocGen {
this.specialCase = specialCase;
this.commentType = commentType;
this.smartTextLength = 0;
+ this.casingType = casingType;
}
/**
@@ -93,35 +108,52 @@ export class CppDocGen implements IDocGen {
if (!this.cfg.Generic.generateSmartText) {
return "";
}
+ let val: string = "";
+ let text: string = "";
switch (this.specialCase) {
case SpecialCase.constructor: {
if (this.func.name === null) {
return "";
} else {
- const str = this.getTemplatedString(this.cfg.nameTemplateReplace,
- this.cfg.Cpp.ctorText,
- this.func.name.trim());
- this.smartTextLength = str.length;
- return str;
+ val = this.splitCasing(this.func.name.trim());
+ text = this.cfg.Cpp.ctorText;
+ break;
}
}
case SpecialCase.destructor: {
if (this.func.name === null) {
return "";
} else {
- const str = this.getTemplatedString(this.cfg.nameTemplateReplace,
- this.cfg.Cpp.dtorText,
- this.func.name.replace("~", "").trim());
- this.smartTextLength = str.length;
- return str;
+ val = this.splitCasing(this.func.name.replace("~", "").trim());
+ text = this.cfg.Cpp.dtorText;
+ break;
}
}
- // tslint:disable-next-line:no-empty
+ case SpecialCase.getter: {
+ val = this.splitCasing(this.func.name.trim()).trim().substr(3).trim();
+ text = this.cfg.C.getterText;
+ break;
+ }
+ case SpecialCase.setter: {
+ val = this.splitCasing(this.func.name.trim()).trim().substr(3).trim();
+ text = this.cfg.C.setterText;
+ break;
+ }
+ case SpecialCase.factoryMethod: {
+ val = this.splitCasing(this.func.name.trim()).trim().substr(6).trim();
+ text = this.cfg.C.factoryMethodText;
+ break;
+ }
case SpecialCase.none:
default: {
return "";
}
}
+ const str = this.getTemplatedString(this.cfg.nameTemplateReplace,
+ text,
+ val);
+ this.smartTextLength = str.length;
+ return str;
}
protected generateBrief(lines: string[]) {
@@ -317,4 +349,38 @@ export class CppDocGen implements IDocGen {
const to: Position = new Position(line, character);
this.activeEditor.selection = new Selection(from, to);
}
+
+ protected splitCasing(text: string): string {
+ if (!this.cfg.Generic.splitCasingSmartText) {
+ return text;
+ }
+ let txt = text;
+ let vals: string[] = [];
+ switch (this.casingType) {
+ case CasingType.SCREAMING_SNAKE: {
+ txt = txt.toLowerCase();
+ }
+ case CasingType.snake: {
+ vals = txt.split("_");
+ break;
+ }
+ case CasingType.Pascal: {
+ txt = txt.replace(/([A-Z0-9])/g, " $1");
+ vals.push(txt);
+ break;
+ }
+ case CasingType.camel: {
+ txt = txt.replace(/([a-zA-Z0-9])(?=[A-Z])/g, "$1 ");
+ vals.push(txt);
+ break;
+ }
+ case CasingType.UPPER:
+ case CasingType.uncertain:
+ default: {
+ return text;
+ }
+ }
+
+ return vals.join(" ");
+ }
}
diff --git a/src/Lang/Cpp/CppParser.ts b/src/Lang/Cpp/CppParser.ts
index 00d14c3..ef73e83 100644
--- a/src/Lang/Cpp/CppParser.ts
+++ b/src/Lang/Cpp/CppParser.ts
@@ -3,7 +3,7 @@ import ICodeParser from "../../Common/ICodeParser";
import { IDocGen } from "../../Common/IDocGen";
import { Config } from "../../Config";
import { CppArgument } from "./CppArgument";
-import { CommentType, CppDocGen, SpecialCase } from "./CppDocGen";
+import { CasingType, CommentType, CppDocGen, SpecialCase } from "./CppDocGen";
import { CppParseTree } from "./CppParseTree";
import { CppToken, CppTokenType } from "./CppToken";
@@ -29,6 +29,8 @@ export default class CppParser implements ICodeParser {
private specialCase: SpecialCase;
private commentType: CommentType;
+ private casingType: CasingType;
+
constructor(cfg: Config) {
this.cfg = cfg;
@@ -255,6 +257,27 @@ export default class CppParser implements ICodeParser {
}
}
+ if (args[0].name !== null) {
+ const methodName = args[0].name;
+
+ if (methodName.toLowerCase().startsWith("get")) {
+ this.casingType = this.checkCasing(methodName);
+ if (this.casingType !== CasingType.uncertain) {
+ this.specialCase = SpecialCase.getter;
+ }
+ } else if (methodName.toLowerCase().startsWith("set")) {
+ this.casingType = this.checkCasing(methodName);
+ if (this.casingType !== CasingType.uncertain) {
+ this.specialCase = SpecialCase.setter;
+ }
+ } else if (methodName.toLowerCase().startsWith("create")) {
+ this.casingType = this.checkCasing(methodName);
+ if (this.casingType !== CasingType.uncertain) {
+ this.specialCase = SpecialCase.factoryMethod;
+ }
+ }
+ }
+
return new CppDocGen(
this.activeEditor,
this.activeSelection,
@@ -264,6 +287,7 @@ export default class CppParser implements ICodeParser {
args[1],
this.specialCase,
this.commentType,
+ this.casingType,
);
}
@@ -683,4 +707,23 @@ export default class CppParser implements ICodeParser {
return args;
}
+
+ private checkCasing(name: string): CasingType {
+ let containsUnderscores = name.indexOf("_") !== -1;
+ if (containsUnderscores) {
+ containsUnderscores = name.indexOf("_") !== name.length - 1; // last character _ may be Google style
+ }
+ // first letter upper case
+ let match = name.match("^([_|\\d|]*[A-Z]).+");
+ if (match !== null) {
+ match = name.match("^([A-Z|_|\\d]{2,})");
+ if (match !== null) {
+ return containsUnderscores ? CasingType.SCREAMING_SNAKE : CasingType.UPPER;
+ } else {
+ return containsUnderscores ? CasingType.uncertain : CasingType.Pascal;
+ }
+ } else {
+ return containsUnderscores ? CasingType.snake : CasingType.camel;
+ }
+ }
}