summaryrefslogtreecommitdiffstats
path: root/src/DocGen/CGen.ts
blob: 26d57541601aa78968010fb5f83fa6f41f430e7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Position, Range, Selection, TextEditor, TextLine, workspace, WorkspaceEdit } from "vscode";
import { Config, ConfigType } from "../Config";
import { IDocGen } from "./DocGen";

export default class CGen implements IDocGen {
    protected firstLine: string;
    protected prefix: string;
    protected lastLine: string;
    protected newLineAfterBrief: boolean;
    protected newLineAfterParams: boolean;
    protected newLineAfterTParams: boolean;
    protected includeTypeAtReturn: boolean;
    protected briefTemplate: string;
    protected paramTemplate: string;
    protected tparamTemplate: string;
    protected returnTemplate: string;

    protected templateParamReplace: string;
    protected templateTypeReplace: string;

    protected activeEditor: TextEditor;

    protected retVals: string[];
    protected params: string[];
    protected tparams: string[];

    /**
     * @param  {TextEditor} actEdit Active editor window
     * @param  {Position} cursorPosition Where the cursor of the user currently is
     * @param  {string[]} param The parameter names of the method extracted by the parser
     * @param  {string[]} tparam The template parameter names of the method extracted by the parser.
     * @param  {string[]} returnVals The return values extracted by the parser
     */
    public constructor(
        actEdit: TextEditor,
        cursorPosition: Position,
        param: string[],
        tparam: string[],
        returnVals: string[],
    ) {
        this.activeEditor = actEdit;
        this.templateParamReplace = "{param}";
        this.templateTypeReplace = "{type}";
        this.params = param;
        this.tparams = tparam;
        this.retVals = returnVals;
    }

    /**
     * @inheritdoc
     */
    public GenerateDoc(rangeToReplace: Range) {
        this.readConfig();
        const comment: string = this.generateComment();

        this.activeEditor.edit((editBuilder) => {
            editBuilder.replace(rangeToReplace, comment); // Insert the comment
        });

        // Set cursor to first DoxyGen command.
        this.moveCursurToFirstDoxyCommand(comment, rangeToReplace.start.line, rangeToReplace.start.character);
    }

    /***************************************************************************
                                    Implementation
     ***************************************************************************/

    protected readConfig() {
        const getCfg = workspace.getConfiguration;

        this.firstLine = getCfg(ConfigType.generic).get<string>(Config.firstLine, "/**");
        this.prefix = getCfg(ConfigType.generic).get<string>(Config.prefix, " * ");
        this.lastLine = getCfg(ConfigType.generic).get<string>(Config.lastLine, " */");
        this.newLineAfterBrief = getCfg(ConfigType.generic).get<boolean>(Config.newLineAfterBrief, true);
        this.newLineAfterParams = getCfg(ConfigType.generic).get<boolean>(Config.newLineAfterParams, false);
        this.newLineAfterTParams = getCfg(ConfigType.generic).get<boolean>(Config.newLineAfterTParams, false);
        this.includeTypeAtReturn = getCfg(ConfigType.generic).get<boolean>(Config.includeTypeAtReturn, false);
        this.briefTemplate = getCfg(ConfigType.generic).get<string>(Config.briefTemplate, "@brief ");
        this.paramTemplate = getCfg(ConfigType.generic).get<string>(Config.paramTemplate, "@param {param} ");
        this.tparamTemplate = getCfg(ConfigType.generic).get<string>(Config.tparamTemplate, "@tparam {param} ");
        this.returnTemplate = getCfg(ConfigType.generic).get<string>(Config.returnTemplate, "@return {type} ");
    }

    protected getIndentation(): string {
        const line: TextLine = this.activeEditor.document.lineAt(this.activeEditor.selection.start.line);
        const lineTxt: string = line.text;
        let stringToIndent: string = "";
        // Find indentation from previous line
        for (let i = 0; i < line.firstNonWhitespaceCharacterIndex; i++) {
            if (lineTxt.charAt(i) === "\t") {
                stringToIndent = stringToIndent + "\t";
            } else if (lineTxt.charAt(i) === " ") {
                stringToIndent = stringToIndent + " ";
            }
        }
        return stringToIndent;
    }

    protected getTemplatedString(replace: string, template: string, param: string): string {
        return template.replace(replace, param);
    }

    protected generateBrief(lines: string[]) {
        lines.push(this.prefix + this.briefTemplate);
    }

    protected generateFromTemplate(lines: string[], replace: string, template: string, templateWith: string[]) {
        let line: string = "";

        templateWith.forEach((element: string) => {
            line = this.prefix;
            line += this.getTemplatedString(replace, template, element);
            lines.push(line);
        });
    }

    protected generateComment(): string {
        const lines: string[] = [];

        if (this.firstLine.trim().length !== 0) {
            lines.push(this.firstLine);
        }

        if (this.briefTemplate.trim().length !== 0) {
            this.generateBrief(lines);
            if (this.newLineAfterBrief === true) {
                lines.push(this.prefix);
            }
        }

        if (this.tparamTemplate.trim().length !== 0 && this.tparams.length > 0) {
            this.generateFromTemplate(lines, this.templateParamReplace, this.tparamTemplate, this.tparams);
            if (this.newLineAfterTParams === true) {
                lines.push(this.prefix);
            }
        }

        if (this.paramTemplate.trim().length !== 0 && this.params.length > 0) {
            this.generateFromTemplate(lines, this.templateParamReplace, this.paramTemplate, this.params);
            if (this.newLineAfterParams === true) {
                lines.push(this.prefix);
            }
        }

        if (this.returnTemplate.trim().length !== 0 && this.retVals.length > 0) {
            if (this.includeTypeAtReturn === false) {
                this.retVals = this.retVals.map((t) => t === "true" || t === "false" ? t : "");
            }

            this.generateFromTemplate(lines, this.templateTypeReplace, this.returnTemplate, this.retVals);
        }

        if (this.lastLine.trim().length !== 0) {
            lines.push(this.lastLine);
        }

        const comment: string = lines.join("\n" + this.getIndentation());
        return comment;
    }

    protected moveCursurToFirstDoxyCommand(comment: string, baseLine: number, baseCharacter) {
        // Find first offset of a new line in the comment. Since that's when the line where the first param starts.
        let line: number = baseLine;
        let character: number = comment.indexOf("\n");

        // If a first line is included find the 2nd line with a newline.
        if (this.firstLine.trim().length !== 0) {
            line++;
            const oldCharacter: number = character;
            character = comment.indexOf("\n", oldCharacter + 1) - oldCharacter;
        }

        // If newline is not found means no first param was found so Set to base line before the newline.
        if (character < 0) {
            line = baseLine;
            character = baseCharacter;
        }

        const moveTo: Position = new Position(line, character);
        this.activeEditor.selection = new Selection(moveTo, moveTo);
    }
}