-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathProperty.ts
225 lines (170 loc) · 6.22 KB
/
Property.ts
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
'use strict';
import * as vscode from 'vscode';
export default class Property {
private description: null|string = null;
private indentation: null|string = null;
private name: string;
private type: null|string = null;
private typeHint: null|string = null;
private pseudoTypes = ['mixed', 'number', 'callback', 'array|object', 'void', 'null', 'integer'];
private nullable = false;
public constructor(name: string)
{
this.name = name;
}
/**
* Check if a property is defined in the provided line
* @param {vscode.TextLine} line Line of the editor.document to search for a property
* @returns {RegExpMatchArray | null} RegExpMatchArray if the line defines a property, null otherwise
*/
static isAProperty(line: vscode.TextLine): RegExpMatchArray | null {
const text = line.text;
const matches = text.match(/(private|public|protected)\s+((\S*)\s+)?\$([a-zA-Z0-9_]+)/);
if (null === matches) {
return null;
}
if ('static' === matches[3]) {
return null;
}
return matches;
}
/**
* Search for and get the property in a line of the provided document if exists
* @param editor Vscode active text editor
* @param line Line of the editor.document to search for a property
* @returns Property object if found, null in other case
*/
static fromLine(
editor: vscode.TextEditor,
line: vscode.TextLine
): Property | null {
const position = new vscode.Position(
line.lineNumber,
line.range.end.character - 1 // Avoid semicolon
);
if (Property.isAProperty(line))
return Property.fromEditorPosition(editor, position);
return null;
}
static fromEditorPosition(editor: vscode.TextEditor, activePosition: vscode.Position) {
const activeLineNumber = activePosition.line;
const activeLine = editor.document.lineAt(activeLineNumber);
const activeLineTokens = Property.isAProperty(activeLine);
if (null === activeLineTokens) {
throw new Error('Invalid property line');
}
const typehint = activeLineTokens[1];
const property = new Property(activeLineTokens[4]);
if (typehint !== 'public' && typehint !== 'private' && typehint !== 'protected') {
property.setType(typehint);
}
property.indentation = activeLine.text.substring(0, activeLine.firstNonWhitespaceCharacterIndex);
if (activeLineTokens[3]) {
property.setType(activeLineTokens[3]);
return property;
}
const previousLineNumber = activeLineNumber - 1;
if (previousLineNumber <= 0) {
return property;
}
const previousLine = editor.document.lineAt(previousLineNumber);
// No doc block found
if (!previousLine.text.endsWith('*/')) {
return property;
}
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('*')) {
break;
}
// Remove spaces & tabs
const lineParts = text.split(' ').filter(function(value){
return value !== '' && value !== "\t" && value !== "*";
});
const varPosition = lineParts.indexOf('@var');
// Found @var line
if (-1 !== varPosition) {
property.setType(lineParts[varPosition + 1]);
const descriptionParts = lineParts.slice(varPosition + 2);
if (descriptionParts.length) {
property.description = descriptionParts.join(` `);
}
continue;
}
const posibleDescription = lineParts.join(` `);
if (posibleDescription[0] !== '@') {
property.description = posibleDescription;
}
}
return property;
}
static fromEditorSelection(editor: vscode.TextEditor) {
return Property.fromEditorPosition(editor, editor.selection.active);
}
generateMethodDescription(prefix : string) : string {
if (this.description) {
return prefix + this.description.charAt(0).toLowerCase() + this.description.substring(1);
}
return prefix + `the value of ` + this.name;
}
generateMethodName(prefix : string) : string {
const name = this.name.split('_')
.map(str => str.charAt(0).toLocaleUpperCase() + str.slice(1))
.join('');
return prefix + name;
}
getDescription() : null|string {
return this.description;
}
getIndentation() : null|string {
return this.indentation;
}
getName() : string {
return this.name;
}
getterDescription() : string {
return this.generateMethodDescription('Get ');
}
getterName() : string {
return this.generateMethodName(this.type === 'bool' ? 'is' : 'get');
}
getType() : null|string {
return this.type;
}
getTypeHint() : null|string {
return this.typeHint;
}
isValidTypeHint(type : string) {
return (-1 === type.indexOf('|') && -1 === this.pseudoTypes.indexOf(type));
}
isNullable() : boolean {
return this.nullable;
}
setterDescription() : string {
return this.generateMethodDescription('Set ');
}
setterName() : string {
return this.generateMethodName('set');
}
setType(type : string) {
this.type = type;
if (/^\?/.test(type)) {
this.nullable = true;
this.type = type.replace(/\?/, '');
} else if (/\|?null\|?/.test(type)) {
this.nullable = true;
this.type = type.replace(/\|?null\|?/, '');
}
if (this.type.indexOf('[]') > 0) {
this.type = 'array';
}
if (this.isValidTypeHint(this.type)) {
this.typeHint = this.type;
}
}
}