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 pathAbstractProvider.coffee
205 lines (169 loc) · 5.73 KB
/
AbstractProvider.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
module.exports =
##*
# Base class for providers.
##
class AbstractProvider
###*
* The regular expression that is used for the prefix.
###
regex: ''
###*
* The class selectors for which autocompletion triggers.
###
scopeSelector: '.source.php'
###*
* The inclusion priority of the provider.
###
inclusionPriority: 1
###*
* Let base autocomplete-plus handle the actual filtering, that way we don't need to manually filter (e.g. using
* fuzzaldrin) ourselves and the user can configure filtering settings on the base package.
###
filterSuggestions: true
###*
* The class selectors autocompletion is explicitly disabled for (overrules the {@see scopeSelector}).
###
disableForScopeSelector: '.source.php .comment, .source.php .string'
###*
* The service (that can be used to query the source code and contains utility methods).
###
service: null
###*
* Contains global package settings.
###
config: null
###*
* Constructor.
*
* @param {Config} config
###
constructor: (@config) ->
@excludeLowerPriority = @config.get('disableBuiltinAutocompletion')
@config.onDidChange 'disableBuiltinAutocompletion', (newValue) =>
@excludeLowerPriority = newValue
###*
* Initializes this provider.
*
* @param {mixed} service
###
activate: (@service) ->
###*
* Deactives the provider.
###
deactivate: () ->
###*
* Entry point for all requests from autocomplete-plus.
*
* @param {TextEditor} editor
* @param {Point} bufferPosition
* @param {string} scopeDescriptor
* @param {string} prefix
*
* @return {Promise|array}
###
getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix}) ->
throw new Error("This method is abstract and must be implemented!")
###*
* Builds the signature for a PHP function or method.
*
* @param {array} info Information about the function or method.
*
* @return {string}
###
getFunctionParameterList: (info) ->
body = "("
isInOptionalList = false
for param, index in info.parameters
description = ''
description += '[' if param.isOptional and not isInOptionalList
description += ', ' if index != 0
description += '...' if param.isVariadic
description += '&' if param.isReference
description += '$' + param.name
description += ' = ' + param.defaultValue if param.defaultValue?
description += ']' if param.isOptional and index == (info.parameters.length - 1)
isInOptionalList = param.isOptional
if not param.isOptional
body += description
else
body += description
body += ")"
return body
###*
* Builds the right label for a PHP function or method.
*
* @param {array} info Information about the function or method.
*
* @return {string}
###
getSuggestionRightLabel: (info) ->
# Determine the short name of the location where this item is defined.
declaringStructureShortName = ''
if info.declaringStructure and info.declaringStructure.name
return @getClassShortName(info.declaringStructure.name)
return declaringStructureShortName
###*
* Builds the snippet for a PHP function or method.
*
* @param {string} name The name of the function or method.
* @param {array} info Information about the function or method.
*
* @return {string}
###
getFunctionSnippet: (name, info) ->
if info.parameters.length > 0
return name + '($0)'
return name + '()$0'
###*
* Retrieves the short name for the specified class name (i.e. the last segment, without the class namespace).
*
* @param {string} className
*
* @return {string}
###
getClassShortName: (className) ->
return null if not className
parts = className.split('\\')
return parts.pop()
###*
* @param {Array} typeArray
*
* @return {String}
###
getTypeSpecificationFromTypeArray: (typeArray) ->
typeNames = typeArray.map (type) =>
return @getClassShortName(type.type)
return typeNames.join('|')
###*
* Retrieves the prefix matches using the specified buffer position and the specified regular expression.
*
* @param {TextEditor} editor
* @param {Point} bufferPosition
* @param {String} regex
*
* @return {Array|null}
###
getPrefixMatchesByRegex: (editor, bufferPosition, regex) ->
# Unfortunately the regex $ doesn't seem to match the end when using backwardsScanInRange, so we match the regex
# manually.
line = editor.getBuffer().getTextInRange([[bufferPosition.row, 0], bufferPosition])
matches = regex.exec(line)
return matches if matches
return null
###*
* Retrieves the prefix using the specified buffer position and the current class' configured regular expression.
*
* @param {TextEditor} editor
* @param {Point} bufferPosition
*
* @return {String|null}
###
getPrefix: (editor, bufferPosition) ->
matches = @getPrefixMatchesByRegex(editor, bufferPosition, @regex)
if matches
# We always want the last match, as that's closest to the cursor itself.
match = matches[matches.length - 1]
# Turn undefined, which happens if the capture group has nothing to catch, into a valid string.
return '' if not match?
return match
return null