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 pathGlobalConstantProvider.coffee
155 lines (123 loc) · 4.38 KB
/
GlobalConstantProvider.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
AbstractProvider = require "./AbstractProvider"
module.exports =
##*
# Provides autocompletion for global PHP constants.
##
class GlobalConstantProvider extends AbstractProvider
###*
* @inheritdoc
*
* These can appear pretty much everywhere, but not in variable names or as class members. We just use the regex
* here to validate, but not to filter out the correct bits, as autocomplete-plus already seems to do this
* correctly.
###
regex: /(?:^|[^\$:>\w\\])([A-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 = (constants) =>
@pendingPromise = null
return unless constants
@listCache = constants
return @listCache
failureHandler = () =>
@pendingPromise = null
return []
if not @pendingPromise?
@pendingPromise = @service.getGlobalConstants().then(successHandler, failureHandler)
return @pendingPromise
###*
* Fetches a list of results that can be fed to the getSuggestions 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
tmpPrefix = @getPrefix(editor, bufferPosition)
return [] unless tmpPrefix != null
successHandler = (constants) =>
return [] unless constants
return @addSuggestions(constants, prefix.trim())
failureHandler = () =>
return []
return @fetchResults().then(successHandler, failureHandler)
###*
* Returns available suggestions.
*
* @param {array} constants
* @param {string} prefix
*
* @return {array}
###
addSuggestions: (constants, prefix) ->
suggestions = []
for fqcn, constant of constants
suggestions.push
text : constant.name
type : 'constant'
displayText : constant.name
replacementPrefix : prefix
leftLabel : @getTypeSpecificationFromTypeArray(constant.types)
description : constant.shortDescription
className : 'php-integrator-autocomplete-plus-suggestion' + if constant.isDeprecated then ' php-integrator-autocomplete-plus-strike' else ''
return suggestions