This repository has been archived by the owner on May 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
/
dispatch.coffee
411 lines (349 loc) · 15.6 KB
/
dispatch.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
{Subscriber, Emitter} = require('emissary')
Gofmt = require('./gofmt')
Govet = require('./govet')
Golint = require('./golint')
Gopath = require('./gopath')
Gobuild = require('./gobuild')
Gocover = require('./gocover')
Executor = require('./executor')
Environment = require('./environment')
GoExecutable = require('./goexecutable')
Godef = require('./godef')
SplicerSplitter = require('./util/splicersplitter')
_ = require('underscore-plus')
{MessagePanelView, LineMessageView, PlainMessageView} = require('atom-message-panel')
path = require('path')
os = require('os')
async = require('async')
module.exports =
class Dispatch
Subscriber.includeInto(this)
Emitter.includeInto(this)
constructor: ->
# Manage Save Pipeline
@activated = false
@dispatching = false
@ready = false
@messages = []
@items = []
@environment = new Environment(process.env)
@executor = new Executor(@environment.Clone())
@splicersplitter = new SplicerSplitter()
@goexecutable = new GoExecutable(@env())
@gofmt = new Gofmt(this)
@govet = new Govet(this)
@golint = new Golint(this)
@gopath = new Gopath(this)
@gobuild = new Gobuild(this)
@gocover = new Gocover(this)
@godef = new Godef(this)
@messagepanel = new MessagePanelView({title: '<span class="icon-diff-added"></span> go-plus', rawTitle: true}) unless @messagepanel?
# Reset State If Requested
gofmtsubscription = @gofmt.on('reset', (editor) => @resetState(editor))
golintsubscription = @golint.on('reset', (editor) => @resetState(editor))
govetsubscription = @govet.on('reset', (editor) => @resetState(editor))
gopathsubscription = @gopath.on('reset', (editor) => @resetState(editor))
gobuildsubscription = @gobuild.on('reset', (editor) => @resetState(editor))
gocoversubscription = @gocover.on('reset', (editor) => @resetState(editor))
@subscribe(gofmtsubscription)
@subscribe(golintsubscription)
@subscribe(govetsubscription)
@subscribe(gopathsubscription)
@subscribe(gobuildsubscription)
@subscribe(gocoversubscription)
@on('dispatch-complete', (editor) => @displayMessages(editor))
@subscribeToAtomEvents()
@detect()
destroy: =>
@destroyItems()
@unsubscribe()
@resetPanel()
@messagepanel?.remove()
@messagepanel = null
@gocover.destroy()
@gobuild.destroy()
@golint.destroy()
@govet.destroy()
@gopath.destroy()
@gofmt.destroy()
@godef.destroy()
@gocover = null
@gobuild = null
@golint = null
@govet = null
@gopath = null
@gofmt = null
@godef = null
@ready = false
@activated = false
@emit('destroyed')
addItem: (item) ->
return if item in @items
if typeof item.on is 'function'
@subscribe(item, 'destroyed', => @removeItem(item))
@items.splice(0, 0, item)
removeItem: (item) ->
index = @items.indexOf(item)
return if index is -1
if typeof item.on is 'function'
@unsubscribe(item)
@items.splice(index, 1)
destroyItems: ->
return unless @items and _.size(@items) > 0
for item in @items
item.dispose()
subscribeToAtomEvents: =>
@addItem(atom.workspace.observeTextEditors((editor) => @handleEvents(editor)))
@addItem(atom.workspace.onDidChangeActivePaneItem((event) => @resetPanel()))
@addItem(atom.config.observe('go-plus.getMissingTools', => @gettools(false) if atom.config.get('go-plus.getMissingTools')? and atom.config.get('go-plus.getMissingTools') and @ready))
@addItem(atom.config.observe('go-plus.formatTool', => @displayGoInfo(true) if @ready))
@addItem(atom.config.observe('go-plus.goPath', => @displayGoInfo(true) if @ready))
@addItem(atom.config.observe('go-plus.environmentOverridesConfiguration', => @displayGoInfo(true) if @ready))
@addItem(atom.config.observe('go-plus.goInstallation', => @detect() if @ready))
atom.commands.add 'atom-workspace',
'golang:goinfo': => @displayGoInfo(true) if @ready and @activated
atom.commands.add 'atom-workspace',
'golang:getmissingtools': => @gettools(false) if @activated
atom.commands.add 'atom-workspace',
'golang:updatetools': => @gettools(true) if @activated
@activated = true
handleEvents: (editor) =>
buffer = editor?.getBuffer()
return unless buffer?
@updateGutter(editor, @messages)
modifiedsubscription = buffer.onDidStopChanging =>
return unless @activated
@handleBufferChanged(editor)
savedsubscription = buffer.onDidSave =>
return unless @activated
return unless not @dispatching
@handleBufferSave(editor, true)
destroyedsubscription = buffer.onDidDestroy =>
savedsubscription?.dispose()
@removeItem(savedsubscription) if savedsubscription?
modifiedsubscription?.dispose()
@removeItem(modifiedsubscription) if modifiedsubscription?
@addItem(modifiedsubscription)
@addItem(savedsubscription)
@addItem(destroyedsubscription)
detect: =>
@ready = false
@goexecutable.detect().then (gos) =>
@gettools(false) if atom.config.get('go-plus.getMissingTools')? and atom.config.get('go-plus.getMissingTools')
@displayGoInfo(false)
@emitReady()
resetAndDisplayMessages: (editor, msgs) =>
return unless @isValidEditor(editor)
@resetState(editor)
@collectMessages(msgs)
@displayMessages(editor)
displayMessages: (editor) =>
@updatePane(editor, @messages)
@updateGutter(editor, @messages)
@dispatching = false
@emit('display-complete')
emitReady: =>
@ready = true
@emit('ready')
displayGoInfo: (force) =>
editor = atom.workspace?.getActiveTextEditor()
unless force
return unless @isValidEditor(editor)
@resetPanel()
go = @goexecutable.current()
if go? and go.executable? and go.executable.trim() isnt ''
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Go:</b> ' + go.name + ' (@' + go.executable + ')', className: 'text-info'}))
# gopath
gopath = go.buildgopath()
if gopath? and gopath.trim() isnt ''
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>GOPATH:</b> ' + gopath, className: 'text-highlight'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>GOPATH:</b> Not Set (You Should Try Launching Atom Using The Shell Commands...)', className: 'text-error'}))
# cover
if go.cover()? and go.cover() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Cover Tool:</b> ' + go.cover(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Cover Tool:</b> Not Found (Is Mercurial Installed?)', className: 'text-error'}))
# vet
if go.vet()? and go.vet() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Vet Tool:</b> ' + go.vet(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Vet Tool:</b> Not Found (Is Mercurial Installed?)', className: 'text-error'}))
# gofmt / goimports
if go.format()? and go.format() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Format Tool:</b> ' + go.format(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Format Tool (' + atom.config.get('go-plus.formatTool') + '):</b> Not Found', className: 'text-error'}))
# golint
if go.golint()? and go.golint() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Lint Tool:</b> ' + go.golint(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Lint Tool:</b> Not Found', className: 'text-error'}))
# gocode
if go.gocode()? and go.gocode() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Gocode Tool:</b> ' + go.gocode(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Gocode Tool:</b> Not Found', className: 'text-error'}))
# godef
if go.godef()? and go.godef() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Godef Tool:</b> ' + go.godef(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Godef Tool:</b> Not Found', className: 'text-error'}))
# gocode active
if _.contains(atom.packages.getAvailablePackageNames(), 'autocomplete-plus')
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Gocode Status:</b> Enabled', className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Gocode Status:</b> Not Enabled (autocomplete-plus needs to be installed and active; install it and restart)', className: 'text-warning'}))
# oracle
if go.oracle()? and go.oracle() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Oracle Tool: ' + go.oracle(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Oracle Tool: Not Found', className: 'text-error'}))
# git
if go.git()? and go.git() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Git:</b> ' + go.git(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Git:</b> Not Found', className: 'text-warning'}))
# hg
if go.hg()? and go.hg() isnt false
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Mercurial:</b> ' + go.hg(), className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Mercurial:</b> Not Found', className: 'text-warning'}))
# PATH
thepath = if os.platform() is 'win32' then @env()?.Path else @env()?.PATH
if thepath? and thepath.trim() isnt ''
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>PATH:</b> ' + thepath, className: 'text-subtle'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>PATH:</b> Not Set', className: 'text-error'}))
else
@messagepanel.add(new PlainMessageView({raw: true, message: 'No Go Installations Were Found', className: 'text-error'}))
@messagepanel.add(new PlainMessageView({raw: true, message: '<b>Atom:</b> ' + atom.appVersion + ' (' + os.platform() + ' ' + os.arch() + ' ' + os.release() + ')', className: 'text-info'}))
@messagepanel.attach()
collectMessages: (messages) ->
messages = _.flatten(messages) if messages? and _.size(messages) > 0
messages = _.filter(messages, (element, index, list) -> return element?)
return unless messages?
messages = _.filter(messages, (message) -> message?)
@messages = _.union(@messages, messages)
@messages = _.uniq @messages, (element, index, list) ->
return element?.line + ':' + element?.column + ':' + element?.msg
@emit('messages-collected', _.size(@messages))
triggerPipeline: (editor, saving) ->
@dispatching = true
go = @goexecutable.current()
unless go? and go.executable? and go.executable.trim() isnt ''
@displayGoInfo(false)
@dispatching = false
return
async.series([
(callback) =>
@gofmt.formatBuffer(editor, saving, callback)
], (err, modifymessages) =>
@collectMessages(modifymessages)
async.parallel([
(callback) =>
@govet.checkBuffer(editor, saving, callback)
(callback) =>
@golint.checkBuffer(editor, saving, callback)
(callback) =>
@gopath.check(editor, saving, callback)
(callback) =>
@gobuild.checkBuffer(editor, saving, callback)
], (err, checkmessages) =>
@collectMessages(checkmessages)
@emit('dispatch-complete', editor)
)
)
async.series([
(callback) =>
@gocover.runCoverage(editor, saving, callback)
], (err, modifymessages) =>
@emit('coverage-complete')
)
handleBufferSave: (editor, saving) ->
return unless @ready and @activated
return unless @isValidEditor(editor)
@resetState(editor)
@triggerPipeline(editor, saving)
handleBufferChanged: (editor) ->
return unless @ready and @activated
return unless @isValidEditor(editor)
resetState: (editor) ->
@messages = []
@resetGutter(editor)
@resetPanel()
resetGutter: (editor) ->
return unless @isValidEditor(editor)
# Find current markers
markers = editor?.getBuffer()?.findMarkers({class: 'go-plus'})
return unless markers? and _.size(markers) > 0
# Remove markers
marker.destroy() for marker in markers
updateGutter: (editor, messages) ->
@resetGutter(editor)
return unless @isValidEditor(editor)
return unless messages? and messages.length > 0
buffer = editor?.getBuffer()
return unless buffer?
for message in messages
skip = false
if message?.file? and message.file isnt ''
skip = message.file isnt buffer?.getPath()
unless skip
if message?.line? and message.line isnt false and message.line >= 0
try
marker = buffer?.markPosition([message.line - 1, 0], {class: 'go-plus', invalidate: 'touch'})
editor?.decorateMarker(marker, {type: 'line-number', class: 'goplus-' + message.type})
catch error
console.log(error)
resetPanel: ->
@messagepanel?.close()
@messagepanel?.clear()
updatePane: (editor, messages) ->
@resetPanel
return unless messages?
if messages.length <= 0 and atom.config.get('go-plus.showPanelWhenNoIssuesExist')
@messagepanel.add(new PlainMessageView({message: 'No Issues', className: 'text-success'}))
@messagepanel.attach()
return
return unless messages.length > 0
return unless atom.config.get('go-plus.showPanel')
sortedMessages = _.sortBy @messages, (element, index, list) ->
return parseInt(element.line, 10)
for message in sortedMessages
className = switch message.type
when 'error' then 'text-error'
when 'warning' then 'text-warning'
else 'text-info'
file = if message.file? and message.file.trim() isnt '' then message.file else null
file = atom.project.relativize(file) if file? and file isnt '' and atom?.project?
column = if message.column? and message.column isnt '' and message.column isnt false then message.column else null
line = if message.line? and message.line isnt '' and message.line isnt false then message.line else null
if file is null and column is null and line is null
# PlainMessageView
@messagepanel.add(new PlainMessageView({message: message.msg, className: className}))
else
# LineMessageView
@messagepanel.add(new LineMessageView({file: file, line: line, character: column, message: message.msg, className: className}))
@messagepanel.attach() if atom?.workspace?
isValidEditor: (editor) ->
editor?.getGrammar()?.scopeName is 'source.go'
env: ->
@environment.Clone()
gettools: (updateExistingTools) =>
updateExistingTools = updateExistingTools? and updateExistingTools
@ready = false
thego = @goexecutable.current()
unless thego? and thego.executable? and thego.executable.trim() isnt ''
@displayGoInfo(false)
return
unless thego.toolsAreMissing() or updateExistingTools
@emitReady()
return
@resetPanel()
@messagepanel.add(new PlainMessageView({message: 'Running `go get -u` to get required tools...', className: 'text-success'}))
@messagepanel.attach()
@goexecutable.on 'gettools-complete', =>
@displayGoInfo(true)
@emitReady()
@goexecutable.gettools(thego, updateExistingTools)