summaryrefslogtreecommitdiffstats
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
parent5368c8151df632ba0ebd863cbc7ff105dc4027bf (diff)
downloaddoxdocgen-f062b00a6ca2a5f42eb376b178735b766832bbcb.tar.gz
Add more smart text
- Getter - Setter - Factory method
-rw-r--r--package.json20
-rw-r--r--src/Config.ts8
-rw-r--r--src/Lang/Cpp/CppDocGen.ts88
-rw-r--r--src/Lang/Cpp/CppParser.ts45
-rw-r--r--src/test/CppTests/ReturnTypes.test.ts2
5 files changed, 150 insertions, 13 deletions
diff --git a/package.json b/package.json
index a8eda70..cb5db10 100644
--- a/package.json
+++ b/package.json
@@ -56,6 +56,21 @@
"type": "string",
"default": " */"
},
+ "doxdocgen.c.setterText": {
+ "description": "Smart text snippet for setters.",
+ "type": "string",
+ "default": "Set the {name} object"
+ },
+ "doxdocgen.c.getterText": {
+ "description": "Smart text snippet for getters.",
+ "type": "string",
+ "default": "Get the {name} object"
+ },
+ "doxdocgen.c.factoryMethodText": {
+ "description": "Smart text snippet for factory methods/functions.",
+ "type": "string",
+ "default": "Create a {name} object"
+ },
"doxdocgen.cpp.newLineAfterTParams": {
"description": "Whether to insert a newline after the template params.",
"type": "boolean",
@@ -145,6 +160,11 @@
"description": "Decide if you want to get smart text for certain commands.",
"type": "boolean",
"default": true
+ },
+ "doxdocgen.generic.splitCasingSmartText": {
+ "description": "Decide if the values put into {name} should be split according to their casing.",
+ "type": "boolean",
+ "default": true
}
}
}
diff --git a/src/Config.ts b/src/Config.ts
index ed88386..aed2105 100644
--- a/src/Config.ts
+++ b/src/Config.ts
@@ -11,6 +11,9 @@ class C {
public firstLine: string = "/**";
public commentPrefix: string = " * ";
public lastLine: string = " */";
+ public getterText: string = "Get the {name} object";
+ public setterText: string = "Set the {name} object";
+ public factoryMethodText: string = "Create a {name} object";
}
class Cpp {
@@ -50,6 +53,7 @@ class Generic {
public dateTemplate: string = "@date {date}";
public dateFormat: string = "YYYY-MM-DD";
public generateSmartText: boolean = true;
+ public splitCasingSmartText: boolean = true;
}
export class Config {
@@ -60,6 +64,9 @@ export class Config {
values.C.firstLine = C.getConfiguration().get<string>("firstLine", values.C.firstLine);
values.C.commentPrefix = C.getConfiguration().get<string>("commentPrefix", values.C.commentPrefix);
values.C.lastLine = C.getConfiguration().get<string>("lastLine", values.C.lastLine);
+ values.C.getterText = C.getConfiguration().get<string>("getterText", values.C.getterText);
+ values.C.setterText = C.getConfiguration().get<string>("setterText", values.C.setterText);
+ values.C.factoryMethodText = C.getConfiguration().get<string>("factoryMethodText", values.C.factoryMethodText);
values.Cpp.newLineAfterTParams = Cpp.getConfiguration().get<boolean>("newLineAfterTParams", values.Cpp.newLineAfterTParams);
values.Cpp.tparamTemplate = Cpp.getConfiguration().get<string>("tparamTemplate", values.Cpp.tparamTemplate);
@@ -81,6 +88,7 @@ export class Config {
values.Generic.dateTemplate = Generic.getConfiguration().get<string>("dateTemplate", values.Generic.dateTemplate);
values.Generic.dateFormat = Generic.getConfiguration().get<string>("dateFormat", values.Generic.dateFormat);
values.Generic.generateSmartText = Generic.getConfiguration().get<boolean>("generateSmartText", values.Generic.generateSmartText);
+ values.Generic.splitCasingSmartText = Generic.getConfiguration().get<boolean>("splitCasingSmartText", values.Generic.splitCasingSmartText);
return values;
}
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;
+ }
+ }
}
diff --git a/src/test/CppTests/ReturnTypes.test.ts b/src/test/CppTests/ReturnTypes.test.ts
index caefa48..7ffeaae 100644
--- a/src/test/CppTests/ReturnTypes.test.ts
+++ b/src/test/CppTests/ReturnTypes.test.ts
@@ -120,7 +120,7 @@ suite("C++ - Return type Tests", () => {
});
test("Return Type in namespace", () => {
- const result = testSetup.SetLine("MyNamespace::Foo CreateFoo();").GetResult();
+ const result = testSetup.SetLine("MyNamespace::Foo MakeFoo();").GetResult();
assert.equal("/**\n * @brief \n * \n * @return MyNamespace::Foo \n */", result);
});