From c30115cc597f975a751142d530563c52d0779b74 Mon Sep 17 00:00:00 2001 From: flashios09 Date: Sun, 22 Dec 2019 02:42:48 +0100 Subject: [PATCH 1/5] [fix] Multiline getter/setter description block --- src/Property.ts | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Property.ts b/src/Property.ts index 3014d83..8e4dc53 100644 --- a/src/Property.ts +++ b/src/Property.ts @@ -49,16 +49,16 @@ export default class Property { return property; } + let multilineDescription = []; for (let line = previousLineNumber - 1; line > 0; line--) { - // Everything found - if (property.name && property.type && property.description) { - break; - } - const text = editor.document.lineAt(line).text; // Reached the end of the doc block if (text.includes('/**') || !text.includes('*')) { + if (!property.description && multilineDescription.length > 0) { + property.description = property.stringifyMultilineDescription(multilineDescription); + } + break; } @@ -70,7 +70,7 @@ export default class Property { const varPosition = lineParts.indexOf('@var'); // Found @var line - if (-1 !== varPosition) { + if (varPosition !== -1) { property.setType(lineParts[varPosition + 1]); var descriptionParts = lineParts.slice(varPosition + 2); @@ -82,10 +82,9 @@ export default class Property { continue; } - const posibleDescription = lineParts.join(` `); - - if (posibleDescription[0] !== '@') { - property.description = posibleDescription; + let possibleDescription = lineParts.join(` `); + if (possibleDescription[0] !== '@') { + multilineDescription = [text, ...multilineDescription]; } } @@ -155,4 +154,23 @@ export default class Property { this.typeHint = type; } } + + stringifyMultilineDescription(multilineDescription: string[]): string { + const leftTrimLine = (line: string) => line.replace(/^\s*\*\s*/, ''); + // remove the ` * ` from the first description line. + multilineDescription[0] = leftTrimLine(multilineDescription[0]); + + // it can be a single line description too ! + if (multilineDescription.length === 1) { + return multilineDescription[0]; + } + + // remove the last line if it's empty + const lastLine = leftTrimLine(multilineDescription[multilineDescription.length -1]); + if (lastLine === '') { + multilineDescription = multilineDescription.slice(0, -1); + } + + return multilineDescription.join("\n"); + } } From cbd957d8d818ec19307cdb1facc8840430688502 Mon Sep 17 00:00:00 2001 From: flashios09 Date: Sun, 22 Dec 2019 02:49:58 +0100 Subject: [PATCH 2/5] [fix] `generatedMethodName` for properties that starts by `_` --- src/Property.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Property.ts b/src/Property.ts index 8e4dc53..d1bf16d 100644 --- a/src/Property.ts +++ b/src/Property.ts @@ -104,7 +104,11 @@ export default class Property { } generateMethodName(prefix : string) : string { - return prefix + this.name.charAt(0).toUpperCase() + this.name.substring(1); + const inflectedPropertyName = (this.name.charAt(0) === '_') + ? this.name.charAt(1).toUpperCase() + this.name.substring(2) + : this.name.charAt(0).toUpperCase() + this.name.substring(1); + + return prefix + inflectedPropertyName; } getDescription() : string { From 19c5bb997ba3d3796689a8781a2665c4329f98b5 Mon Sep 17 00:00:00 2001 From: flashios09 Date: Sun, 22 Dec 2019 03:04:25 +0100 Subject: [PATCH 3/5] [fix] setter argument name for properties that starts by `_` --- src/Property.ts | 4 ++++ src/extension.ts | 7 ++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Property.ts b/src/Property.ts index d1bf16d..530064c 100644 --- a/src/Property.ts +++ b/src/Property.ts @@ -123,6 +123,10 @@ export default class Property { return this.name; } + getArgumentName(): string { + return this.name.charAt(0) === '_' ? this.name.slice(1) : this.name; + } + getterDescription() : string { return this.generateMethodDescription('Get '); } diff --git a/src/extension.ts b/src/extension.ts index ed66c6a..d82d091 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -140,6 +140,7 @@ class Resolver { setterTemplate(prop: Property) { const name = prop.getName(); + const argumentName = prop.getArgumentName(); const description = prop.getDescription(); const tab = prop.getIndentation(); const type = prop.getType(); @@ -161,13 +162,13 @@ class Resolver { + tab + `/**\n` + tab + ` * ` + prop.setterDescription() + `\n` + (type ? tab + ` *\n` : ``) - + (type ? tab + ` * @param` + spacesAfterParam + type + spacesAfterParamVar + `$` + name + (description ? ` ` + description : ``) + `\n` : ``) + + (type ? tab + ` * @param` + spacesAfterParam + type + spacesAfterParamVar + `$` + argumentName + (description ? ` ` + description : ``) + `\n` : ``) + tab + ` *\n` + tab + ` * @return` + spacesAfterReturn + `self\n` + tab + ` */\n` - + tab + `public function ` + prop.setterName() + `(` + (typeHint ? typeHint + ` ` : ``) + `$` + name + `)\n` + + tab + `public function ` + prop.setterName() + `(` + (typeHint ? typeHint + ` ` : ``) + `$` + argumentName + `)\n` + tab+ `{\n` - + tab + tab + `$this->` + name + ` = $` + name + `;\n` + + tab + tab + `$this->` + name + ` = $` + argumentName + `;\n` + `\n` + tab + tab + `return $this;\n` + tab + `}\n` From 064e679a939db475a8e5e811524e5a533c198024 Mon Sep 17 00:00:00 2001 From: flashios09 Date: Sun, 22 Dec 2019 03:14:02 +0100 Subject: [PATCH 4/5] Add vscode `launch.json` file --- .vscode/launch.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a5f550d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Run Extension", + "type": "extensionHost", + "request": "launch", + "runtimeExecutable": "${execPath}", + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}" + ], + "outFiles": [ + "${workspaceFolder}/out/**/*.js" + ] + } + ] +} From 2039a1d86b1c5eae6a7bcf7827760557d8b46af7 Mon Sep 17 00:00:00 2001 From: flashios09 Date: Sun, 22 Dec 2019 05:12:23 +0100 Subject: [PATCH 5/5] [fix] setter argument description for multiline version --- src/Property.ts | 59 ++++++++++++++++++++++++++++++++++++------------ src/extension.ts | 4 ++-- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Property.ts b/src/Property.ts index 530064c..718dea5 100644 --- a/src/Property.ts +++ b/src/Property.ts @@ -3,8 +3,12 @@ import * as vscode from 'vscode'; +// remove the ` * ` at the begining of the line +const leftTrimLine = (line: string) => line.replace(/^\s*\*\s*/, ''); + export default class Property { private description: string = null; + private descriptionLines: string[] = []; private indentation: string; private name: string; private type: string = null; @@ -49,14 +53,13 @@ export default class Property { return property; } - let multilineDescription = []; for (let line = previousLineNumber - 1; line > 0; line--) { const text = editor.document.lineAt(line).text; // Reached the end of the doc block if (text.includes('/**') || !text.includes('*')) { - if (!property.description && multilineDescription.length > 0) { - property.description = property.stringifyMultilineDescription(multilineDescription); + if (!property.description && property.descriptionLines.length > 0) { + property.description = property.stringifyDescriptionLines(); } break; @@ -76,7 +79,8 @@ export default class Property { var descriptionParts = lineParts.slice(varPosition + 2); if (descriptionParts.length) { - property.description = descriptionParts.join(` `); + // sync the `description` string with the `descriptionLines` array + property.description = property.descriptionLines[0] = descriptionParts.join(` `); } continue; @@ -84,7 +88,7 @@ export default class Property { let possibleDescription = lineParts.join(` `); if (possibleDescription[0] !== '@') { - multilineDescription = [text, ...multilineDescription]; + property.descriptionLines = [text, ...property.descriptionLines]; } } @@ -115,6 +119,28 @@ export default class Property { return this.description; } + getArgumentDescription(): string|null { + const linesCount = this.descriptionLines.length; + if (linesCount === 0) { + return null; + } + + const firstLine = leftTrimLine(this.descriptionLines[0]); + + // single line description + if (linesCount === 1) { + return firstLine; + } + + // multiline description, check if the second line isn't empty + const secondLine = leftTrimLine(this.descriptionLines[1]); + if (secondLine !== '') { + return firstLine + '\n' + this.descriptionLines[1]; + } + + return firstLine; + } + getIndentation() : string { return this.indentation; } @@ -163,22 +189,25 @@ export default class Property { } } - stringifyMultilineDescription(multilineDescription: string[]): string { - const leftTrimLine = (line: string) => line.replace(/^\s*\*\s*/, ''); + stringifyDescriptionLines(): string|null { + if (this.descriptionLines.length === 0) { + return null; + } + // remove the ` * ` from the first description line. - multilineDescription[0] = leftTrimLine(multilineDescription[0]); + const firstLine = leftTrimLine(this.descriptionLines[0]); // it can be a single line description too ! - if (multilineDescription.length === 1) { - return multilineDescription[0]; + if (this.descriptionLines.length === 1) { + return firstLine; } // remove the last line if it's empty - const lastLine = leftTrimLine(multilineDescription[multilineDescription.length -1]); - if (lastLine === '') { - multilineDescription = multilineDescription.slice(0, -1); - } + const lastLine = leftTrimLine(this.descriptionLines[this.descriptionLines.length -1]); + const descriptionLines = (lastLine === '') ? this.descriptionLines.slice(0, -1) : this.descriptionLines; + // skip the first line, we will use the left trimmed version(without ` * `) + const [head, ...tail] = descriptionLines; - return multilineDescription.join("\n"); + return firstLine + '\n' + tail.join('\n'); } } diff --git a/src/extension.ts b/src/extension.ts index d82d091..eda13ac 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -141,7 +141,7 @@ class Resolver { setterTemplate(prop: Property) { const name = prop.getName(); const argumentName = prop.getArgumentName(); - const description = prop.getDescription(); + const argumentDescription = prop.getArgumentDescription(); const tab = prop.getIndentation(); const type = prop.getType(); const typeHint = prop.getTypeHint(); @@ -162,7 +162,7 @@ class Resolver { + tab + `/**\n` + tab + ` * ` + prop.setterDescription() + `\n` + (type ? tab + ` *\n` : ``) - + (type ? tab + ` * @param` + spacesAfterParam + type + spacesAfterParamVar + `$` + argumentName + (description ? ` ` + description : ``) + `\n` : ``) + + (type ? tab + ` * @param` + spacesAfterParam + type + spacesAfterParamVar + `$` + argumentName + (argumentDescription ? ` ` + argumentDescription : ``) + `\n` : ``) + tab + ` *\n` + tab + ` * @return` + spacesAfterReturn + `self\n` + tab + ` */\n`