This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
forked from mihaimaruseac/hindent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hindent.coffee
80 lines (71 loc) · 2.43 KB
/
hindent.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
{CompositeDisposable} = require 'atom'
{BufferedProcess} = require 'atom'
{dirname} = require 'path'
{statSync} = require 'fs'
prettify = (style, text, workingDirectory, {onComplete, onFailure}) ->
lines = []
proc = new BufferedProcess
command: 'hindent'
args: ['--style', style]
options:
cwd: workingDirectory
stdout: (line) -> lines.push(line)
exit: -> onComplete?(lines.join(''))
proc.onWillThrowError ({error, handle}) ->
atom.notifications.addError "Hindent could not spawn",
detail: "#{error}"
onFailure?()
handle()
proc.process.stdin.write(text)
proc.process.stdin.end()
prettifyFile = (style, editor, format = 'haskell') ->
[firstCursor, cursors...] = editor.getCursors().map (cursor) ->
cursor.getBufferPosition()
try
workDir = dirname(editor.getPath())
if not statSync(workDir).isDirectory()
workDir = '.'
catch
workDir = '.'
prettify style, editor.getText(), workDir,
onComplete: (text) ->
editor.setText(text)
if editor.getLastCursor()?
editor.getLastCursor().setBufferPosition firstCursor,
autoscroll: false
cursors.forEach (cursor) ->
editor.addCursorAtBufferPosition cursor,
autoscroll: false
module.exports = Hindent =
disposables: null
menu: null
activate: (state) ->
@disposables = new CompositeDisposable
@menu = new CompositeDisposable
@disposables.add \
atom.commands.add 'atom-text-editor[data-grammar~="haskell"]',
'hindent:prettify-fundamental': ({target}) =>
prettifyFile 'fundamental', target.getModel()
'hindent:prettify-chris-done': ({target}) =>
prettifyFile 'chris-done', target.getModel()
'hindent:prettify-johan-tibell': ({target}) =>
prettifyFile 'johan-tibell', target.getModel()
'hindent:prettify-gibiansky': ({target}) =>
prettifyFile 'gibiansky', target.getModel()
@menu.add atom.menu.add [
label: 'hindent'
submenu : [
{label: 'Fundamental', command: 'hindent:prettify-fundamental'}
{label: 'Chris Done', command: 'hindent:prettify-chris-done'}
{label: 'Johan Tibell', command: 'hindent:prettify-johan-tibell'}
{label: 'Gibiansky', command: 'hindent:prettify-gibiansky'}
]
]
deactivate: ->
@disposables.dispose()
@disposables = null
@clearMenu()
clearMenu: ->
@menu.dispose()
@menu = null
atom.menu.update()