forked from autocomplete-python/autocomplete-python
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprovider.coffee
executable file
·475 lines (427 loc) · 18.1 KB
/
provider.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
log = require './log'
module.exports =
selector: '.source.python'
disableForSelector: '.source.python .comment, .source.python .string'
inclusionPriority: 2
suggestionPriority: atom.config.get('autocomplete-python-jedi.suggestionPriority')
excludeLowerPriority: false
cacheSize: 10
_addEventListener: (editor, eventName, handler) ->
editorView = atom.views.getView editor
editorView.addEventListener eventName, handler
disposable = new @Disposable ->
log.debug 'Unsubscribing from event listener ', eventName, handler
editorView.removeEventListener eventName, handler
return disposable
_noExecutableError: (error) ->
if @providerNoExecutable
return
log.warning 'No python executable found', error
atom.notifications.addWarning(
'autocomplete-python-jedi unable to find python binary.', {
detail: """Please set path to python executable manually in package
settings and restart your editor. Be sure to migrate on new settings
if everything worked on previous version.
Detailed error message: #{error}
Current config: #{atom.config.get('autocomplete-python-jedi.pythonPaths')}"""
dismissable: true})
@providerNoExecutable = true
_spawnDaemon: ->
interpreter = @InterpreterLookup.getInterpreter()
log.debug 'Using interpreter', interpreter
@provider = new @BufferedProcess
command: interpreter or 'python'
args: [__dirname + '/completion.py']
stdout: (data) =>
@_deserialize(data)
stderr: (data) =>
if data.indexOf('is not recognized as an internal or external') > -1
return @_noExecutableError(data)
log.debug "autocomplete-python-jedi traceback output: #{data}"
if data.indexOf('jedi') > -1
if atom.config.get('autocomplete-python-jedi.outputProviderErrors')
atom.notifications.addWarning(
'''Looks like this error originated from Jedi. Please report this
issue to autocomplete-python-jedi so we can help improve Jedi.
Turn off the `outputProviderErrors` setting to hide such errors
in future. Traceback output:''', {
detail: "#{data}",
dismissable: true})
else
atom.notifications.addError(
'autocomplete-python-jedi traceback output:', {
detail: "#{data}",
dismissable: true})
log.debug "Forcing to resolve #{Object.keys(@requests).length} promises"
for requestId, resolve of @requests
if typeof resolve == 'function'
resolve([])
delete @requests[requestId]
exit: (code) =>
log.warning 'Process exit with', code, @provider
@provider.onWillThrowError ({error, handle}) =>
if error.code is 'ENOENT' and error.syscall.indexOf('spawn') is 0
@_noExecutableError(error)
@dispose()
handle()
else
throw error
@provider.process?.stdin.on 'error', (err) ->
log.debug 'stdin', err
setTimeout =>
log.debug 'Killing python process after timeout...'
if @provider and @provider.process
@provider.kill()
, 60 * 10 * 1000
load: ->
if not @constructed
@constructor()
return this
constructor: () ->
{@Disposable, @CompositeDisposable, @BufferedProcess} = require 'atom'
{@selectorsMatchScopeChain} = require './scope-helpers'
{@Selector} = require 'selector-kit'
@DefinitionsView = require './definitions-view'
@UsagesView = require './usages-view'
@OverrideView = require './override-view'
@RenameView = require './rename-view'
@InterpreterLookup = require './interpreters-lookup'
@_ = require 'underscore'
@filter = require('fuzzaldrin-plus').filter
{@_showSignatureOverlay} = require './tooltips'
@requests = {}
@responses = {}
@provider = null
@disposables = new @CompositeDisposable
@subscriptions = {}
@definitionsView = null
@usagesView = null
@renameView = null
@constructed = true
@snippetsManager = null
log.debug "Init autocomplete-python-jedi with priority #{@suggestionPriority}"
try
@triggerCompletionRegex = RegExp atom.config.get(
'autocomplete-python-jedi.triggerCompletionRegex')
catch err
atom.notifications.addWarning(
'''autocomplete-python-jedi invalid regexp to trigger autocompletions.
Falling back to default value.''', {
detail: "Original exception: #{err}"
dismissable: true})
atom.config.set('autocomplete-python-jedi.triggerCompletionRegex',
'([\.\ ]|[a-zA-Z_][a-zA-Z0-9_]*)')
@triggerCompletionRegex = /([\.\ ]|[a-zA-Z_][a-zA-Z0-9_]*)/
selector = 'atom-text-editor[data-grammar~=python]'
atom.commands.add selector, 'autocomplete-python-jedi:go-to-definition', =>
@goToDefinition()
atom.commands.add selector, 'autocomplete-python-jedi:complete-arguments', =>
editor = atom.workspace.getActiveTextEditor()
@_completeArguments(editor, editor.getCursorBufferPosition(), true)
atom.commands.add selector, 'autocomplete-python-jedi:show-usages', =>
editor = atom.workspace.getActiveTextEditor()
bufferPosition = editor.getCursorBufferPosition()
if @usagesView
@usagesView.destroy()
@usagesView = new @UsagesView()
@getUsages(editor, bufferPosition).then (usages) =>
@usagesView.setItems(usages)
atom.commands.add selector, 'autocomplete-python-jedi:override-method', =>
editor = atom.workspace.getActiveTextEditor()
bufferPosition = editor.getCursorBufferPosition()
if @overrideView
@overrideView.destroy()
@overrideView = new @OverrideView()
@getMethods(editor, bufferPosition).then ({methods, indent, bufferPosition}) =>
@overrideView.indent = indent
@overrideView.bufferPosition = bufferPosition
@overrideView.setItems(methods)
atom.commands.add selector, 'autocomplete-python-jedi:rename', =>
editor = atom.workspace.getActiveTextEditor()
bufferPosition = editor.getCursorBufferPosition()
@getUsages(editor, bufferPosition).then (usages) =>
if @renameView
@renameView.destroy()
if usages.length > 0
@renameView = new @RenameView(usages)
@renameView.onInput (newName) =>
for fileName, usages of @_.groupBy(usages, 'fileName')
[project, _relative] = atom.project.relativizePath(fileName)
if project
@_updateUsagesInFile(fileName, usages, newName)
else
log.debug 'Ignoring file outside of project', fileName
else
if @usagesView
@usagesView.destroy()
@usagesView = new @UsagesView()
@usagesView.setItems(usages)
atom.workspace.observeTextEditors (editor) =>
@_handleGrammarChangeEvent(editor, editor.getGrammar())
editor.onDidChangeGrammar (grammar) =>
@_handleGrammarChangeEvent(editor, grammar)
atom.config.onDidChange 'autocomplete-plus.enableAutoActivation', =>
atom.workspace.observeTextEditors (editor) =>
@_handleGrammarChangeEvent(editor, editor.getGrammar())
_updateUsagesInFile: (fileName, usages, newName) ->
columnOffset = {}
atom.workspace.open(fileName, activateItem: false).then (editor) ->
buffer = editor.getBuffer()
for usage in usages
{name, line, column} = usage
columnOffset[line] ?= 0
log.debug 'Replacing', usage, 'with', newName, 'in', editor.id
log.debug 'Offset for line', line, 'is', columnOffset[line]
buffer.setTextInRange([
[line - 1, column + columnOffset[line]],
[line - 1, column + name.length + columnOffset[line]],
], newName)
columnOffset[line] += newName.length - name.length
buffer.save()
_handleGrammarChangeEvent: (editor, grammar) ->
eventName = 'keyup'
eventId = "#{editor.id}.#{eventName}"
if grammar.scopeName == 'source.python'
if atom.config.get('autocomplete-python-jedi.showTooltips') is true
editor.onDidChangeCursorPosition (event) =>
@_showSignatureOverlay(event, @)
if not atom.config.get('autocomplete-plus.enableAutoActivation')
log.debug 'Ignoring keyup events due to autocomplete-plus settings.'
return
disposable = @_addEventListener editor, eventName, (e) =>
if atom.keymaps.keystrokeForKeyboardEvent(e) == '^('
log.debug 'Trying to complete arguments on keyup event', e
@_completeArguments(editor, editor.getCursorBufferPosition())
@disposables.add disposable
@subscriptions[eventId] = disposable
log.debug 'Subscribed on event', eventId
else
if eventId of @subscriptions
@subscriptions[eventId].dispose()
log.debug 'Unsubscribed from event', eventId
_serialize: (request) ->
log.debug 'Serializing request to be sent to Jedi', request
return JSON.stringify(request)
_sendRequest: (data, respawned) ->
log.debug 'Pending requests:', Object.keys(@requests).length, @requests
if Object.keys(@requests).length > 10
log.debug 'Cleaning up request queue to avoid overflow, ignoring request'
@requests = {}
if @provider and @provider.process
log.debug 'Killing python process'
@provider.kill()
return
if @provider and @provider.process
process = @provider.process
if process.exitCode == null and process.signalCode == null
if @provider.process.pid
return @provider.process.stdin.write(data + '\n')
else
log.debug 'Attempt to communicate with terminated process', @provider
else if respawned
atom.notifications.addWarning(
["Failed to spawn daemon for autocomplete-python-jedi."
"Completions will not work anymore"
"unless you restart your editor."].join(' '), {
detail: ["exitCode: #{process.exitCode}"
"signalCode: #{process.signalCode}"].join('\n'),
dismissable: true})
@dispose()
else
@_spawnDaemon()
@_sendRequest(data, respawned: true)
log.debug 'Re-spawning python process...'
else
log.debug 'Spawning python process...'
@_spawnDaemon()
@_sendRequest(data)
_deserialize: (response) ->
log.debug 'Deserealizing response from Jedi', response
log.debug "Got #{response.trim().split('\n').length} lines"
for responseSource in response.trim().split('\n')
try
response = JSON.parse(responseSource)
catch e
throw new Error("""Failed to parse JSON from \"#{responseSource}\".
Original exception: #{e}""")
if response['arguments']
editor = @requests[response['id']]
if typeof editor == 'object'
bufferPosition = editor.getCursorBufferPosition()
# Compare response ID with current state to avoid stale completions
if response['id'] == @_generateRequestId('arguments', editor, bufferPosition)
@snippetsManager?.insertSnippet(response['arguments'], editor)
else
resolve = @requests[response['id']]
if typeof resolve == 'function'
resolve(response['results'])
cacheSizeDelta = Object.keys(@responses).length > @cacheSize
if cacheSizeDelta > 0
ids = Object.keys(@responses).sort (a, b) =>
return @responses[a]['timestamp'] - @responses[b]['timestamp']
for id in ids.slice(0, cacheSizeDelta)
log.debug 'Removing old item from cache with ID', id
delete @responses[id]
@responses[response['id']] =
source: responseSource
timestamp: Date.now()
log.debug 'Cached request with ID', response['id']
delete @requests[response['id']]
_generateRequestId: (type, editor, bufferPosition, text) ->
if not text
text = editor.getText()
return require('crypto').createHash('md5').update([
editor.getPath(), text, bufferPosition.row,
bufferPosition.column, type].join()).digest('hex')
_generateRequestConfig: ->
extraPaths = @InterpreterLookup.applySubstitutions(
atom.config.get('autocomplete-python-jedi.extraPaths').split(';'))
args =
'extraPaths': extraPaths
'useSnippets': atom.config.get('autocomplete-python-jedi.useSnippets')
'caseInsensitiveCompletion': atom.config.get(
'autocomplete-python-jedi.caseInsensitiveCompletion')
'showDescriptions': atom.config.get(
'autocomplete-python-jedi.showDescriptions')
'fuzzyMatcher': atom.config.get('autocomplete-python-jedi.fuzzyMatcher')
return args
setSnippetsManager: (@snippetsManager) ->
_completeArguments: (editor, bufferPosition, force) ->
useSnippets = atom.config.get('autocomplete-python-jedi.useSnippets')
if not force and useSnippets == 'none'
atom.commands.dispatch(document.querySelector('atom-text-editor'),
'autocomplete-plus:activate')
return
scopeDescriptor = editor.scopeDescriptorForBufferPosition(bufferPosition)
scopeChain = scopeDescriptor.getScopeChain()
disableForSelector = @Selector.create(@disableForSelector)
if @selectorsMatchScopeChain(disableForSelector, scopeChain)
log.debug 'Ignoring argument completion inside of', scopeChain
return
# we don't want to complete arguments inside of existing code
lines = editor.getBuffer().getLines()
line = lines[bufferPosition.row]
prefix = line.slice(bufferPosition.column - 1, bufferPosition.column)
if prefix isnt '('
log.debug 'Ignoring argument completion with prefix', prefix
return
suffix = line.slice bufferPosition.column, line.length
if not /^(\)(?:$|\s)|\s|$)/.test(suffix)
log.debug 'Ignoring argument completion with suffix', suffix
return
payload =
id: @_generateRequestId('arguments', editor, bufferPosition)
lookup: 'arguments'
path: editor.getPath()
source: editor.getText()
line: bufferPosition.row
column: bufferPosition.column
config: @_generateRequestConfig()
@_sendRequest(@_serialize(payload))
return new Promise =>
@requests[payload.id] = editor
_fuzzyFilter: (candidates, query) ->
if candidates.length isnt 0 and query not in [' ', '.', '(']
candidates = @filter(candidates, query, key: 'text')
return candidates
getSuggestions: ({editor, bufferPosition, scopeDescriptor, prefix}) ->
@load()
if not @triggerCompletionRegex.test(prefix)
return @lastSuggestions = []
bufferPosition =
row: bufferPosition.row
column: bufferPosition.column
lines = editor.getBuffer().getLines()
if atom.config.get('autocomplete-python-jedi.fuzzyMatcher')
# we want to do our own filtering, hide any existing suffix from Jedi
line = lines[bufferPosition.row]
lastIdentifier = /\.?[a-zA-Z_][a-zA-Z0-9_]*$/.exec(
line.slice 0, bufferPosition.column)
if lastIdentifier
bufferPosition.column = lastIdentifier.index + 1
lines[bufferPosition.row] = line.slice(0, bufferPosition.column)
requestId = @_generateRequestId(
'completions', editor, bufferPosition, lines.join('\n'))
if requestId of @responses
log.debug 'Using cached response with ID', requestId
# We have to parse JSON on each request here to pass only a copy
matches = JSON.parse(@responses[requestId]['source'])['results']
if atom.config.get('autocomplete-python-jedi.fuzzyMatcher')
return @lastSuggestions = @_fuzzyFilter(matches, prefix)
else
return @lastSuggestions = matches
payload =
id: requestId
prefix: prefix
lookup: 'completions'
path: editor.getPath()
source: editor.getText()
line: bufferPosition.row
column: bufferPosition.column
config: @_generateRequestConfig()
@_sendRequest(@_serialize(payload))
return new Promise (resolve) =>
if atom.config.get('autocomplete-python-jedi.fuzzyMatcher')
@requests[payload.id] = (matches) =>
resolve(@lastSuggestions = @_fuzzyFilter(matches, prefix))
else
@requests[payload.id] = (suggestions) =>
resolve(@lastSuggestions = suggestions)
getDefinitions: (editor, bufferPosition) ->
payload =
id: @_generateRequestId('definitions', editor, bufferPosition)
lookup: 'definitions'
path: editor.getPath()
source: editor.getText()
line: bufferPosition.row
column: bufferPosition.column
config: @_generateRequestConfig()
@_sendRequest(@_serialize(payload))
return new Promise (resolve) =>
@requests[payload.id] = resolve
getUsages: (editor, bufferPosition) ->
payload =
id: @_generateRequestId('usages', editor, bufferPosition)
lookup: 'usages'
path: editor.getPath()
source: editor.getText()
line: bufferPosition.row
column: bufferPosition.column
config: @_generateRequestConfig()
@_sendRequest(@_serialize(payload))
return new Promise (resolve) =>
@requests[payload.id] = resolve
getMethods: (editor, bufferPosition) ->
indent = bufferPosition.column
lines = editor.getBuffer().getLines()
lines.splice(bufferPosition.row + 1, 0, " def __autocomplete_python(s):")
lines.splice(bufferPosition.row + 2, 0, " s.")
payload =
id: @_generateRequestId('methods', editor, bufferPosition)
lookup: 'methods'
path: editor.getPath()
source: lines.join('\n')
line: bufferPosition.row + 2
column: 6
config: @_generateRequestConfig()
@_sendRequest(@_serialize(payload))
return new Promise (resolve) =>
@requests[payload.id] = (methods) ->
resolve({methods, indent, bufferPosition})
goToDefinition: (editor, bufferPosition) ->
if not editor
editor = atom.workspace.getActiveTextEditor()
if not bufferPosition
bufferPosition = editor.getCursorBufferPosition()
if @definitionsView
@definitionsView.destroy()
@definitionsView = new @DefinitionsView()
@getDefinitions(editor, bufferPosition).then (results) =>
@definitionsView.setItems(results)
if results.length == 1
@definitionsView.confirmed(results[0])
dispose: ->
if @disposables
@disposables.dispose()
if @provider
@provider.kill()