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 8
/
Copy pathGlobalFunctionProvider.coffee
167 lines (132 loc) · 5.16 KB
/
GlobalFunctionProvider.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
AbstractProvider = require "./AbstractProvider"
module.exports =
##*
# Provides autocompletion for global PHP functions.
##
class GlobalFunctionProvider extends AbstractProvider
###*
* @inheritdoc
*
* These can appear pretty much everywhere, but not in variable names or as class members. Note that functions can
* also appear inside namespaces, hence the middle part.
###
regex: /(?:^|[^\$:>\w])((?:[a-zA-Z_][a-zA-Z0-9_]*\\)*[a-zA-Z_]+)$/
###*
# Cache object to help improve responsiveness of autocompletion.
###
listCache: null
###*
# A list of disposables to dispose on deactivation.
###
disposables: null
###*
# Keeps track of a currently pending promise to ensure only one is active at any given time.
###
pendingPromise: null
###*
# Keeps track of a currently pending timeout to ensure only one is active at any given time..
###
timeoutHandle: null
###*
* @inheritdoc
###
activate: (@service) ->
{CompositeDisposable} = require 'atom'
@disposables = new CompositeDisposable()
@disposables.add(@service.onDidFinishIndexing(@onDidFinishIndexing.bind(this)))
###*
* @inheritdoc
###
deactivate: () ->
if @disposables?
@disposables.dispose()
@disposables = null
###*
* Called when reindexing successfully finishes.
*
* @param {Object} info
###
onDidFinishIndexing: (info) ->
# Only reindex a couple of seconds after the last reindex. This prevents constant refreshes being scheduled
# while the user is still modifying the file. This is acceptable as this provider's data rarely changes and
# it is fairly expensive to refresh the cache.
if @timeoutHandle?
clearTimeout(@timeoutHandle)
@timeoutHandle = null
timeoutTime = @config.get('largeListRefreshTimeout')
timeoutTime += Math.random() * @config.get('largeListRefreshTimeoutJitter')
@timeoutHandle = setTimeout ( =>
@timeoutHandle = null
@refreshCache()
), timeoutTime
###*
* Refreshes the internal cache. Returns a promise that resolves with the cache once it has been refreshed.
*
* @return {Promise}
###
refreshCache: () ->
successHandler = (functions) =>
@pendingPromise = null
return unless functions
@listCache = functions
return @listCache
failureHandler = () =>
@pendingPromise = null
return []
if not @pendingPromise?
@pendingPromise = @service.getGlobalFunctions().then(successHandler, failureHandler)
return @pendingPromise
###*
* Fetches a list of results that can be fed to the addSuggestions method.
*
* @return {Promise}
###
fetchResults: () ->
return new Promise (resolve, reject) =>
if @listCache?
resolve(@listCache)
return
return @refreshCache()
###*
* @inheritdoc
###
getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix}) ->
return [] if not @service
prefix = @getPrefix(editor, bufferPosition)
return [] unless prefix != null
successHandler = (functions) =>
return [] unless functions
characterAfterPrefix = editor.getTextInRange([bufferPosition, [bufferPosition.row, bufferPosition.column + 1]])
insertParameterList = if characterAfterPrefix == '(' then false else true
return @addSuggestions(functions, prefix.trim(), insertParameterList)
failureHandler = () =>
return []
return @fetchResults().then(successHandler, failureHandler)
###*
* Returns available suggestions.
*
* @param {array} functions
* @param {string} prefix
* @param {bool} insertParameterList Whether to insert a list of parameters or not.
*
* @return {array}
###
addSuggestions: (functions, prefix, insertParameterList = true) ->
suggestions = []
for fqcn, func of functions
shortDescription = ''
if func.shortDescription? and func.shortDescription.length > 0
shortDescription = func.shortDescription
# NOTE: The description must not be empty for the 'More' button to show up.
suggestions.push
text : func.name
type : 'function'
snippet : if insertParameterList then @getFunctionSnippet(func.name, func) else null
displayText : func.name + @getFunctionParameterList(func)
replacementPrefix : prefix
leftLabel : @getTypeSpecificationFromTypeArray(func.returnTypes)
rightLabelHTML : @getSuggestionRightLabel(func)
description : shortDescription
descriptionMoreURL : null
className : 'php-integrator-autocomplete-plus-suggestion' + if func.isDeprecated then ' php-integrator-autocomplete-plus-strike' else ''
return suggestions