summaryrefslogtreecommitdiffstats
path: root/src/Lang/Cpp/CppDocGen.ts
blob: ebfa5f5dc374d681870b182bfb2c7fc5e7ba2f9c (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import * as moment from "moment";
import { Position, Range, Selection, TextEditor, TextLine, WorkspaceEdit } from "vscode";
import { IDocGen } from "../../Common/IDocGen";
import { Config } from "../../Config";
import { CppArgument } from "./CppArgument";
import { CppParseTree } from "./CppParseTree";
import { CppToken, CppTokenType } from "./CppToken";

export default class CppDocGen implements IDocGen {
    protected activeEditor: TextEditor;

    protected readonly cfg: Config;

    protected func: CppArgument;
    protected templateParams: string[];
    protected params: CppArgument[];

    /**
     * @param  {TextEditor} actEdit Active editor window
     * @param  {Position} cursorPosition Where the cursor of the user currently is
     * @param  {string[]} templateParams The template parameters of the declaration.
     * @param  {CppArgument} func The type and name of the function to generate doxygen.
     *                          Doesn't contain anything if it is not a function.
     * @param  {CppArgument[]} params The parameters of the function. Doesn't contain anything if it is not a function.
     */
    public constructor(
        actEdit: TextEditor,
        cursorPosition: Position,
        cfg: Config,
        templateParams: string[],
        func: CppArgument,
        params: CppArgument[],
    ) {
        this.activeEditor = actEdit;
        this.cfg = cfg;
        this.templateParams = templateParams;
        this.func = func;
        this.params = params;
    }

    /**
     * @inheritdoc
     */
    public GenerateDoc(rangeToReplace: Range) {
        let comment: string;
        if (this.func.name === "#include") {
            comment = this.generateFileDescription();
        } else {
            comment = 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 getIndentation(): string {
        return this.activeEditor.document.lineAt(this.activeEditor.selection.start.line).text.match("^\\s*")[0];
    }

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

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

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

        templateWith.forEach((element: string) => {
            // Ignore null values
            if (element !== null) {
                line = this.cfg.commentPrefix;
                line += this.getTemplatedString(replace, template, element);
                lines.push(line);
            }
        });
    }

    protected generateReturnParams(): string[] {
        if (this.cfg.includeTypeAtReturn === false) {
            return [""];
        }

        const params: string[] = [];

        // Check if return type is a pointer
        const ptrReturnIndex = this.func.type.nodes
            .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Pointer);

        // Special case for void functions.
        const voidReturnIndex = this.func.type.nodes
            .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Symbol && n.value === "void");

        // Special case for bool return type.
        const boolReturnIndex: number = this.func.type.nodes
            .findIndex((n) => n instanceof CppToken && n.type === CppTokenType.Symbol && n.value === "bool");

        if (boolReturnIndex !== -1 && this.cfg.boolReturnsTrueFalse === true) {
            params.push("true");
            params.push("false");
        } else if (voidReturnIndex !== -1 && ptrReturnIndex !== -1) {
            params.push(this.cfg.includeTypeAtReturn === true ? this.func.type.Yield() : "");
        } else if (voidReturnIndex === -1 && this.func.type.nodes.length > 0) {
            params.push(this.cfg.includeTypeAtReturn === true ? this.func.type.Yield() : "");
        }

        return params;
    }

    protected generateAuthorTag(lines: string[]) {
        if (this.cfg.authorTag.trim().length !== 0) {
            lines.push(this.cfg.commentPrefix + this.cfg.authorTag);
        }
    }

    protected generateFilenameFromTemplate(lines: string[]) {
        if (this.cfg.fileTemplate.trim().length !== 0) {
            this.generateFromTemplate(
                lines,
                this.cfg.nameTemplateReplace,
                this.cfg.fileTemplate,
                [this.activeEditor.document.fileName.replace(/^.*[\\\/]/, "")],
            );
        }
    }

    protected generateDateFromTemplate(lines: string[]) {
        if (this.cfg.dateTemplate.trim().length !== 0 &&
            this.cfg.dateFormat.trim().length !== 0) {
            this.generateFromTemplate(
                lines,
                this.cfg.dateTemplateReplace,
                this.cfg.dateTemplate,
                [moment().format(this.cfg.dateFormat)],
            );
        }
    }

    protected insertFirstLine(lines: string[]) {
        if (this.cfg.firstLine.trim().length !== 0) {
            lines.push(this.cfg.firstLine);
        }
    }

    protected insertBrief(lines: string[]) {
        if (this.cfg.briefTemplate.trim().length !== 0) {
            this.generateBrief(lines);
            if (this.cfg.newLineAfterBrief === true) {
                lines.push(this.cfg.commentPrefix);
            }
        }
    }

    protected insertLastLine(lines: string[]) {
        if (this.cfg.lastLine.trim().length !== 0) {
            lines.push(this.cfg.lastLine);
        }
    }

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

        this.insertFirstLine(lines);

        this.cfg.fileOrder.forEach((element) => {
            switch (element) {
                case "brief": {
                    this.insertBrief(lines);
                    break;
                }
                case "file": {
                    this.generateFilenameFromTemplate(lines);
                    break;
                }
                case "author": {
                    this.generateAuthorTag(lines);
                    break;
                }
                case "date": {
                    this.generateDateFromTemplate(lines);
                    break;
                }
                default: {
                    break;
                }
            }
        });

        this.insertLastLine(lines);

        return lines.join("\n");
    }

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

        this.insertFirstLine(lines);

        this.insertBrief(lines);

        if (this.cfg.tparamTemplate.trim().length !== 0 && this.templateParams.length > 0) {
            this.generateFromTemplate(
                lines,
                this.cfg.paramTemplateReplace,
                this.cfg.tparamTemplate,
                this.templateParams,
            );
            if (this.cfg.newLineAfterTParams === true) {
                lines.push(this.cfg.commentPrefix);
            }
        }

        if (this.cfg.paramTemplate.trim().length !== 0 && this.params.length > 0) {
            const paramNames: string[] = this.params.map((p) => p.name);
            this.generateFromTemplate(lines, this.cfg.paramTemplateReplace, this.cfg.paramTemplate, paramNames);
            if (this.cfg.newLineAfterParams === true) {
                lines.push(this.cfg.commentPrefix);
            }
        }

        if (this.cfg.returnTemplate.trim().length !== 0 && this.func.type !== null) {
            const returnParams = this.generateReturnParams();
            this.generateFromTemplate(lines, this.cfg.typeTemplateReplace, this.cfg.returnTemplate, returnParams);
        }

        this.insertLastLine(lines);

        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.cfg.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);
    }
}