This repository was archived by the owner on Feb 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassProvider.coffee
252 lines (182 loc) · 9.74 KB
/
ClassProvider.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
shell = require 'shell'
{Point, Range} = require 'atom'
AbstractProvider = require './AbstractProvider'
module.exports =
##*
# Provides code navigation for classes (i.e. being able to click class, interface and trait names to navigate to them).
##
class ClassProvider extends AbstractProvider
###*
* A list of all markers that have been placed inside comments to allow code navigation there as well.
*
* @var {Object}
###
markers: null
###*
* @inheritdoc
###
canProvideForBufferPosition: (editor, bufferPosition) ->
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition)
return false if 'php' not in classList
climbCount = 1
if 'punctuation' in classList and 'inheritance' in classList
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition, 2)
climbCount = 2
return true if 'class' in classList and 'support' in classList
return true if 'inherited-class' in classList
return true if 'namespace' in classList and 'use' in classList
return true if 'phpdoc' in classList
return true if 'comment' in classList # See also https://github.com/atom/language-php/issues/135
if 'namespace' in classList
classListFollowingBufferPosition = @scopeDescriptorHelper.getClassListFollowingBufferPosition(editor, bufferPosition, climbCount)
return true if ('class' in classListFollowingBufferPosition and 'support' in classListFollowingBufferPosition) or 'inherited-class' in classListFollowingBufferPosition
return false
###*
* @param {TextEditor} editor
* @param {Point} bufferPosition
###
getRangeForBufferPosition: (editor, bufferPosition) ->
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition)
climbCount = 1
if 'punctuation' in classList and 'inheritance' in classList
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, bufferPosition, 2)
climbCount = 2
range = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, classList, bufferPosition, 0)
# Atom's consistency regarding the namespace separator splitting a namespace prefix and an actual class name
# leaves something to be desired: sometimes it's part of the namespace, other times it's in its own class,
# in even other cases it has no class at all. For some reason fetching the range for the scope also returns
# "undefined". This entire if-block exists only to handle this corner case.
if not range?
newBufferPosition = bufferPosition.copy()
--newBufferPosition.column
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, newBufferPosition)
if 'punctuation' in classList and 'inheritance' in classList
classList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, newBufferPosition, 2)
range = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, classList, newBufferPosition)
++bufferPosition.column
if ('class' in classList and 'support' in classList) or 'inherited-class' in classList
prefixRange = new Range(
new Point(range.start.row, range.start.column - 1),
new Point(range.start.row, range.start.column - 0)
)
prefixText = editor.getTextInBufferRange(prefixRange)
if prefixText == "\\"
prefixClassList = @scopeDescriptorHelper.getClassListForBufferPosition(editor, prefixRange.start, 2)
if "namespace" in prefixClassList
namespaceRange = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, prefixClassList, prefixRange.start, 0)
else
namespaceRange = range
namespaceRange.start.column--
if namespaceRange?
range = namespaceRange.union(range)
else if 'namespace' in classList
suffixClassList = @scopeDescriptorHelper.getClassListFollowingBufferPosition(editor, bufferPosition, climbCount)
# Expand the range to include the constant name, if present.
if ('class' in suffixClassList and 'support' in suffixClassList) or 'inherited-class' in suffixClassList
classNameRange = @scopeDescriptorHelper.getBufferRangeForClassListAtPosition(editor, suffixClassList, new Point(range.end.row, range.end.column + 1))
if classNameRange?
range = range.union(classNameRange)
else if 'phpdoc' in classList or 'comment' in classList
# Docblocks are seen as one entire region of text as they don't have syntax highlighting. Use regular
# expressions instead to find interesting parts containing class names.
lineText = editor.lineTextForBufferRow(bufferPosition.row)
ranges = []
if /@param|@var|@return|@throws|@see/g.test(lineText)
ranges = @getRangesForDocblockLine(lineText.split(' '), parseInt(bufferPosition.row), editor, true, 0, 0, false)
else if /@\\?([A-Za-z0-9_]+)\\?([A-Za-zA-Z_\\]*)?/g.test(lineText)
ranges = @getRangesForDocblockLine(lineText.split(' '), parseInt(bufferPosition.row), editor, true, 0, 0, true)
for range in ranges
if range.containsPoint(bufferPosition)
return range
return null
return range
###*
* @param {Array} words The array of words to check.
* @param {Number} rowIndex The current row the words are on within the editor.
* @param {TextEditor} editor The editor the words are from.
* @param {bool} shouldBreak Flag to say whether the search should break after finding 1 class.
* @param {Number} currentIndex The current column index the search is on.
* @param {Number} offset Any offset that should be applied when creating the marker.
###
getRangesForDocblockLine: (words, rowIndex, editor, shouldBreak, currentIndex = 0, offset = 0, isAnnotation = false) ->
if isAnnotation
regex = /^@(\\?(?:[A-Za-z0-9_]+)\\?(?:[A-Za-zA-Z_\\]*)?)/g
else
regex = /^(\\?(?:[A-Za-z0-9_]+)\\?(?:[A-Za-zA-Z_\\]*)?)/g
ranges = []
for key,value of words
continue if value.length == 0
newValue = value.match(regex)
if newValue? && @service.isBasicType(value) == false
newValue = newValue[0]
if value.includes('|')
ranges = ranges.concat(@getRangesForDocblockLine(value.split('|'), rowIndex, editor, false, currentIndex, parseInt(key)))
else
if isAnnotation
newValue = newValue.substr(1)
currentIndex += 1
range = new Range(
new Point(rowIndex, currentIndex + parseInt(key) + offset),
new Point(rowIndex, currentIndex + parseInt(key) + newValue.length + offset)
)
ranges.push(range)
if shouldBreak == true
break
currentIndex += value.length;
return ranges
###*
* Convenience method that returns information for the specified term.
*
* @param {TextEditor} editor
* @param {Point} bufferPosition
* @param {String} term
*
* @return {Promise}
###
getInfoFor: (editor, bufferPosition, term) ->
if not term
return new Promise (resolve, reject) ->
resolve(null)
failureHandler = () ->
# Do nothing.
scopeChain = editor.scopeDescriptorForBufferPosition(bufferPosition).getScopeChain()
# Don't attempt to resolve class names in use statements.
if scopeChain.indexOf('.support.other.namespace.use') != -1
successHandler = (currentClassName) =>
# Scope descriptors for trait use statements and actual "import" use statements are the same, so we
# have no choice but to use class information for this.
if not currentClassName?
return false
return true
firstPromise = @service.determineCurrentClassName(editor, bufferPosition).then(successHandler, failureHandler)
else
firstPromise = new Promise (resolve, reject) ->
resolve(true)
successHandler = (doResolve) =>
promise = null
className = term
if doResolve
promise = @service.resolveTypeAt(editor, bufferPosition, className, 'classlike')
else
promise = new Promise (resolve, reject) ->
resolve(className)
nestedSuccessHandler = (className) =>
return @service.getClassInfo(className)
return promise.then(nestedSuccessHandler, failureHandler)
return firstPromise.then(successHandler, failureHandler)
###*
* @inheritdoc
###
handleSpecificNavigation: (editor, range, text) ->
successHandler = (info) =>
return if not info?
if info.filename?
atom.workspace.open(info.filename, {
initialLine : (info.startLine - 1),
searchAllPanes : true
})
else
shell.openExternal(@service.getDocumentationUrlForClass(info.name))
failureHandler = () ->
# Do nothing.
@getInfoFor(editor, range.start, text).then(successHandler, failureHandler)