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 pathNamespaceProvider.coffee
157 lines (122 loc) · 4.45 KB
/
NamespaceProvider.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
AbstractProvider = require "./AbstractProvider"
module.exports =
##*
# Provides autocompletion for namespaces after the namespace keyword.
##
class NamespaceProvider extends AbstractProvider
###*
* @inheritdoc
###
regex: /namespace\s+(\\?[a-zA-Z_][a-zA-Z0-9_]*(?:\\[a-zA-Z_][a-zA-Z0-9_]*)*\\?)?$/
###*
# 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
@timeoutHandle = setTimeout ( =>
@timeoutHandle = null
@refreshCache()
), @config.get('largeListRefreshTimeout')
###*
* Refreshes the internal cache. Returns a promise that resolves with the cache once it has been refreshed.
*
* @return {Promise}
###
refreshCache: () ->
successHandler = (namespaces) =>
@pendingPromise = null
return unless namespaces
@listCache = namespaces
return @listCache
failureHandler = () =>
@pendingPromise = null
return []
if not @pendingPromise?
@pendingPromise = @service.getNamespaceList().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 = (namespaces) =>
return [] unless namespaces
characterAfterPrefix = editor.getTextInRange([bufferPosition, [bufferPosition.row, bufferPosition.column + 1]])
insertParameterList = if characterAfterPrefix == '(' then false else true
return @addSuggestions(namespaces, prefix.trim(), insertParameterList)
failureHandler = () =>
return []
return @fetchResults().then(successHandler, failureHandler)
###*
* Returns available suggestions.
*
* @param {array} namespaces
* @param {string} prefix
* @param {bool} insertParameterList Whether to insert a list of parameters or not.
*
* @return {array}
###
addSuggestions: (namespaces, prefix, insertParameterList = true) ->
suggestions = []
for namespace in namespaces
continue if namespace.namespace.length == 0
fqcnWithoutLeadingSlash = namespace.namespace
if fqcnWithoutLeadingSlash[0] == '\\'
fqcnWithoutLeadingSlash = fqcnWithoutLeadingSlash.substring(1)
# NOTE: The description must not be empty for the 'More' button to show up.
suggestions.push
text : fqcnWithoutLeadingSlash
type : 'import'
leftLabel : 'namespace'
displayText : fqcnWithoutLeadingSlash
return suggestions