This repository was archived by the owner on May 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParameterParser.coffee
325 lines (262 loc) · 10 KB
/
ParameterParser.coffee
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
{Point, Range} = require 'atom'
module.exports =
class ParameterParser
###*
* Service object from the php-integrator-base service
*
* @type {Service}
###
service: null
###*
* @type {Object}
###
typeHelper: null
###*
* List of all the variable declarations that have been process
*
* @type {Array}
###
variableDeclarations: []
###*
* The selected range that we are scanning for parameters in.
*
* @type {Range}
###
selectedBufferRange: null
###*
* Constructor
*
* @param {Object} typeHelper
###
constructor: (@typeHelper) ->
###*
* @param {Object} service
###
setService: (@service) ->
###*
* Takes the editor and the range and loops through finding all the
* parameters that will be needed if this code was to be moved into
* its own function
*
* @param {TextEditor} editor
* @param {Range} selectedBufferRange
*
* @return {Promise}
###
findParameters: (editor, selectedBufferRange) ->
@selectedBufferRange = selectedBufferRange
parameters = []
editor.scanInBufferRange /\$[a-zA-Z0-9_]+/g, selectedBufferRange, (element) =>
# Making sure we matched a variable and not a variable within a string
descriptions = editor.scopeDescriptorForBufferPosition(element.range.start)
indexOfDescriptor = descriptions.scopes.indexOf('variable.other.php')
if indexOfDescriptor > -1
parameters.push {
name: element.matchText,
range: element.range
}
regexFilters = [
{
name: 'Foreach loops',
regex: /as\s(\$[a-zA-Z0-9_]+)(?:\s=>\s(\$[a-zA-Z0-9_]+))?/g
},
{
name: 'For loops',
regex: /for\s*\(\s*(\$[a-zA-Z0-9_]+)\s*=/g
},
{
name: 'Try catch',
regex: /catch(?:\(|\s)+.*?(\$[a-zA-Z0-9_]+)/g
},
{
name: 'Closure'
regex: /function(?:\s)*?\((?:\$).*?\)/g
},
{
name: 'Variable declarations',
regex: /(\$[a-zA-Z0-9]+)\s*?=(?!>|=)/g
}
]
getTypePromises = []
variableDeclarations = []
for filter in regexFilters
editor.backwardsScanInBufferRange filter.regex, selectedBufferRange, (element) =>
variables = element.matchText.match /\$[a-zA-Z0-9]+/g
startPoint = new Point(element.range.end.row, 0)
scopeRange = @getRangeForCurrentScope editor, startPoint
if filter.name == 'Variable declarations'
chosenParameter = null
for parameter in parameters
if element.range.containsRange(parameter.range)
chosenParameter = parameter
break
if chosenParameter != null
getTypePromises.push (@getTypesForParameter editor, chosenParameter)
variableDeclarations.push chosenParameter
for variable in variables
parameters = parameters.filter (parameter) =>
if parameter.name != variable
return true
if scopeRange.containsRange(parameter.range)
# If variable declaration is after parameter then it's
# still needed in parameters.
if element.range.start.row > parameter.range.start.row
return true
if element.range.start.row == parameter.range.start.row &&
element.range.start.column > parameter.range.start.column
return true
return false
return true
@variableDeclarations = @makeUnique variableDeclarations
parameters = @makeUnique parameters
# Grab the variable types of the parameters.
promises = []
parameters.forEach (parameter) =>
# Removing $this from parameters as this doesn't need to be passed in.
return if parameter.name == '$this'
promises.push @getTypesForParameter(editor, parameter)
returnFirstResultHandler = (resultArray) ->
return resultArray[0]
return Promise.all([Promise.all(promises), Promise.all(getTypePromises)]).then(returnFirstResultHandler)
###*
* Takes the current buffer position and returns a range of the current
* scope that the buffer position is in.
*
* For example this could be the code within an if statement or closure.
*
* @param {TextEditor} editor
* @param {Point} bufferPosition
*
* @return {Range}
###
getRangeForCurrentScope: (editor, bufferPosition) ->
startScopePoint = null
endScopePoint = null
# Tracks any extra scopes that might exist inside the scope we are
# looking for.
childScopes = 0
# First walk back until we find the start of the current scope.
for row in [bufferPosition.row .. 0]
line = editor.lineTextForBufferRow(row)
continue if not line
lastIndex = line.length - 1
for i in [lastIndex .. 0]
descriptions = editor.scopeDescriptorForBufferPosition(
[row, i]
)
indexOfDescriptor = descriptions.scopes.indexOf('punctuation.section.scope.end.php')
if indexOfDescriptor > -1
childScopes++
indexOfDescriptor = descriptions.scopes.indexOf('punctuation.section.scope.begin.php')
if indexOfDescriptor > -1
childScopes--
if childScopes == -1
startScopePoint = new Point(row, 0)
break
break if startScopePoint?
if startScopePoint == null
startScopePoint = new Point(0, 0)
childScopes = 0
# Walk forward until we find the end of the current scope
for row in [startScopePoint.row .. editor.getLineCount()]
line = editor.lineTextForBufferRow(row)
continue if not line
startIndex = 0
if startScopePoint.row == row
startIndex = line.length - 1
for i in [startIndex .. line.length - 1]
descriptions = editor.scopeDescriptorForBufferPosition(
[row, i]
)
indexOfDescriptor = descriptions.scopes.indexOf('punctuation.section.scope.begin.php')
if indexOfDescriptor > -1
childScopes++
indexOfDescriptor = descriptions.scopes.indexOf('punctuation.section.scope.end.php')
if indexOfDescriptor > -1
if childScopes > 0
childScopes--
if childScopes == 0
endScopePoint = new Point(row, i + 1)
break
break if endScopePoint?
return new Range(startScopePoint, endScopePoint)
###*
* Takes an array of parameters and removes any parameters that appear more
* that once with the same name.
*
* @param {Array} array
*
* @return {Array}
###
makeUnique: (array) ->
return array.filter (filterItem, pos, self) ->
for i in [0 .. self.length - 1]
if self[i].name != filterItem.name
continue
return pos == i
return true
###*
* Generates the key used to store the parameters in the cache.
*
* @param {TextEditor} editor
* @param {Range} selectedBufferRange
*
* @return {String}
###
buildKey: (editor, selectedBufferRange) ->
return editor.getPath() + JSON.stringify(selectedBufferRange)
###*
* Gets the type for the parameter given.
*
* @param {TextEditor} editor
* @param {Object} parameter
*
* @return {Promise}
###
getTypesForParameter: (editor, parameter) ->
successHandler = (types) =>
parameter.types = types
typeResolutionPromises = []
path = editor.getPath()
localizeTypeSuccessHandler = (localizedType) =>
return localizedType
localizeTypeFailureHandler = () ->
return null
for fqcn in parameter.types
if @typeHelper.isClassType(fqcn)
typeResolutionPromise = @service.localizeType(
path,
@selectedBufferRange.end.row + 1,
fqcn
)
typeResolutionPromises.push typeResolutionPromise.then(
localizeTypeSuccessHandler,
localizeTypeFailureHandler
)
else
typeResolutionPromises.push Promise.resolve(fqcn)
combineResolvedTypesHandler = (processedTypeArray) ->
parameter.types = processedTypeArray
return parameter
return Promise.all(typeResolutionPromises).then(
combineResolvedTypesHandler,
combineResolvedTypesHandler
)
failureHandler = () =>
return null
return @service.deduceTypesAt(parameter.name, editor, @selectedBufferRange.end).then(
successHandler,
failureHandler
)
###*
* Returns all the variable declarations that have been parsed.
*
* @return {Array}
###
getVariableDeclarations: ->
return @variableDeclarations
###*
* Clean up any data from previous usage
###
cleanUp: ->
@variableDeclarations = []