-
Notifications
You must be signed in to change notification settings - Fork 0
/
hbformat.coffee
119 lines (108 loc) · 3.75 KB
/
hbformat.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
{spawn} = require ('child_process')
{Subscriber, Emitter} = require ('emissary')
_ = require ('underscore-plus')
path = require ('path')
{exec, tempFile} = helpers = require('atom-linter')
{MessagePanelView, LineMessageView, PlainMessageView} =
require 'atom-message-panel'
{CompositeDisposable} = require('atom')
fs = require 'fs-plus'
module.exports =
class HbFormat
Subscriber.includeInto(this)
Emitter.includeInto(this)
constructor: (dispatch) ->
atom.commands.add 'atom-workspace',
'harbourlang:hbformat': => @formatCurrentBuffer()
@dispatch = dispatch
@name = 'hbformat'
@filePath = null
@subEdit = new CompositeDisposable
destroy: ->
@unsubscribe()
@subEdit.dispose()
@dispatch = null
reset: (editor) ->
@emit 'reset', editor
formatCurrentBuffer: ->
editor = atom?.workspace?.getActiveTextEditor()
try
editor.save()
catch e
console.log "editor.save", e
#console.log 'editor current buffer', editor
return unless @dispatch?.isValidEditor(editor)
@reset editor
done = (err, messages) =>
@dispatch.resetAndDisplayMessages(editor, messages)
@formatBuffer(editor, false, done)
formatBuffer: (editor, saving, callback = ->) ->
# console.log 'format buffer'
unless @dispatch.isValidEditor(editor)
@emit @name + '-complete', editor, saving
callback(null)
return
if saving and not atom.config.get('harbour-plus.formatOnSave')
@emit @name + '-complete', editor, saving
callback(null)
return
buffer = editor?.getBuffer()
unless buffer?
@emit @name + '-complete', editor, saving
callback(null)
return
@filePath = buffer.getPath()
cwd = path.dirname(@filePath)
# console.log( "cwd:", cwd)
args = []
configArgs = @dispatch.splicersplitter.splitAndSquashToArray(' ', \
atom.config.get('harbour-plus.hbformatArgs'))
args = _.union(args, configArgs) if configArgs? and _.size(configArgs) > 0
# hbformat bug fix
# hbformat <full path> eg /Users/hernad/github/harbour-plus/test.prg
# DON'T WORK, we need:
# hbformat test.prg
currentFile = buffer.getPath().split('\\').pop().split('/').pop()
args = _.union(args, [currentFile])
# console.log( "formatBuffer args:", args)
cmd = @dispatch.harbour.hbformat()
# console.log( "hbformat cmd:", cmd )
if cmd is false
message =
line: false
column: false
msg: 'Harbour Format Tool Missing'
type: 'error'
source: @name
callback(null, [message])
return
done = (exitcode, stdout, stderr, messages) =>
#console.log "done callback:", @name, 'stdout: ', stdout, 'stderr:', stderr
messages = @mapMessages(editor, stderr, cwd)
# emituje se hbformat-complete event
@emit @name + '-complete', editor, saving
@dispatch.resetPanel()
buffer.setPath(@filePath)
editor.setText fs.readFileSync(@filePath, encoding: buffer.getEncoding())
callback(null, messages)
@dispatch.messagepanel.add new PlainMessageView
message: 'formatting ' + currentFile, className: 'text-success'
@dispatch.messagepanel.attach()
#buffer.setPath('hbformat-test')
#@subEdit.add editor.onDidChange (change) =>
# for n in [1..5]
# console.log "editor onDidChange", change, n
@dispatch.executor.exec(cmd, cwd, @dispatch?.env(), done, args)
mapMessages: (editor, data, cwd) ->
#console.log 'map error messages:', data
# <...Error 3 on line 1924: END PRITN
regex = /Error (\d+) on line (\d+)\: (.*)/g
messages = []
while((match = regex.exec(data)) isnt null)
messages.push
type: 'error'
file: editor.getPath()
line: match[2]
column: "1"
msg: match[1] + ': ' + match[3]
messages