From 38e3c4c2cf3feb2a73226a295d039fe907b044af Mon Sep 17 00:00:00 2001 From: Christoph Schlosser Date: Sun, 25 Feb 2018 23:26:04 +0100 Subject: Suggest smart text for ctor and dtor --- src/Lang/Cpp/CppDocGen.ts | 49 ++++++++++++++++++++++++++++++++++++++++++++--- src/Lang/Cpp/CppParser.ts | 28 ++++++++++++++++++++------- 2 files changed, 67 insertions(+), 10 deletions(-) (limited to 'src/Lang/Cpp') diff --git a/src/Lang/Cpp/CppDocGen.ts b/src/Lang/Cpp/CppDocGen.ts index ebfa5f5..6f3ecc7 100644 --- a/src/Lang/Cpp/CppDocGen.ts +++ b/src/Lang/Cpp/CppDocGen.ts @@ -6,7 +6,18 @@ import { CppArgument } from "./CppArgument"; import { CppParseTree } from "./CppParseTree"; import { CppToken, CppTokenType } from "./CppToken"; -export default class CppDocGen implements IDocGen { +export enum SpecialCase { + none, + constructor, + destructor, +} + +export enum CommentType { + method, + file, +} + +export class CppDocGen implements IDocGen { protected activeEditor: TextEditor; protected readonly cfg: Config; @@ -15,6 +26,9 @@ export default class CppDocGen implements IDocGen { protected templateParams: string[]; protected params: CppArgument[]; + protected specialCase: SpecialCase; + protected commentType: CommentType; + /** * @param {TextEditor} actEdit Active editor window * @param {Position} cursorPosition Where the cursor of the user currently is @@ -30,12 +44,16 @@ export default class CppDocGen implements IDocGen { templateParams: string[], func: CppArgument, params: CppArgument[], + specialCase: SpecialCase, + commentType: CommentType, ) { this.activeEditor = actEdit; this.cfg = cfg; this.templateParams = templateParams; this.func = func; this.params = params; + this.specialCase = specialCase; + this.commentType = commentType; } /** @@ -43,7 +61,7 @@ export default class CppDocGen implements IDocGen { */ public GenerateDoc(rangeToReplace: Range) { let comment: string; - if (this.func.name === "#include") { + if (this.commentType === CommentType.file) { comment = this.generateFileDescription(); } else { comment = this.generateComment(); @@ -68,8 +86,33 @@ export default class CppDocGen implements IDocGen { return template.replace(replace, param); } + protected getSmartText(): string { + // todo: Make the text customizable + switch (this.specialCase) { + case SpecialCase.constructor: { + if (this.func.name === null) { + return ""; + } else { + return "Construct a new " + this.func.name.trim() + " object"; + } + } + case SpecialCase.destructor: { + if (this.func.name === null) { + return ""; + } else { + return "Destroy the " + this.func.name.replace("~", "").trim() + " object"; + } + } + // tslint:disable-next-line:no-empty + case SpecialCase.none: + default: { + return ""; + } + } + } + protected generateBrief(lines: string[]) { - lines.push(this.cfg.commentPrefix + this.cfg.briefTemplate); + lines.push(this.cfg.commentPrefix + this.cfg.briefTemplate + this.getSmartText()); } protected generateFromTemplate(lines: string[], replace: string, template: string, templateWith: string[]) { diff --git a/src/Lang/Cpp/CppParser.ts b/src/Lang/Cpp/CppParser.ts index 46f22e3..8dfa005 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 CppDocGen from "./CppDocGen"; +import { CommentType, CppDocGen, SpecialCase } from "./CppDocGen"; import { CppParseTree } from "./CppParseTree"; import { CppToken, CppTokenType } from "./CppToken"; @@ -26,6 +26,9 @@ export default class CppParser implements ICodeParser { private attributes: string[]; private lexerVocabulary; + private specialCase: SpecialCase; + private commentType: CommentType; + constructor(cfg: Config) { this.cfg = cfg; @@ -212,6 +215,9 @@ export default class CppParser implements ICodeParser { return symbol.replace(/\s+$/, ""); }, }; + + this.specialCase = SpecialCase.none; + this.commentType = CommentType.method; } /** @@ -230,9 +236,8 @@ export default class CppParser implements ICodeParser { const templateArgs: string[] = []; let args: [CppArgument, CppArgument[]] = [new CppArgument(), []]; - if (line === "#include" || - (activeEdit.selection.active.line === 0 && line.length === 0)) { // head of file - args[0].name = "#include"; + if (activeEdit.selection.active.line === 0 && line.length === 0) { // head of file + this.commentType = CommentType.file; } else { // method // template parsing is simpler by using heuristics rather then CppTokenizing first. while (line.startsWith("template")) { @@ -257,6 +262,8 @@ export default class CppParser implements ICodeParser { templateArgs, args[0], args[1], + this.specialCase, + this.commentType, ); } @@ -305,7 +312,8 @@ export default class CppParser implements ICodeParser { // Head of file probably if (nextLineTxt.startsWith("#include")) { - return "#include"; + this.commentType = CommentType.file; + return ""; } logicalLine += "\n" + nextLineTxt; @@ -353,8 +361,8 @@ export default class CppParser implements ICodeParser { // return argument. const func = this.GetArgument(tree); - // check if it is a constructor or descructor since these have no name.. - // and reverse the assignment of type and name. + // check if it is a constructor or descructor since these have no name. + // Also reverse the assignment of type and name. if (func.name === null) { if (func.type.nodes.length !== 1) { throw new Error("Too many symbols found for constructor/descructor."); @@ -362,6 +370,12 @@ export default class CppParser implements ICodeParser { throw new Error("One node found with just a CppParseTree. Malformed input."); } + if (line.includes("~")) { + this.specialCase = SpecialCase.destructor; + } else { + this.specialCase = SpecialCase.constructor; + } + func.name = (func.type.nodes[0] as CppToken).value; func.type.nodes = []; } -- cgit v1.2.3