@@ -6,32 +6,73 @@ import { spawn } from 'child_process';
6
6
export default {
7
7
8
8
subscriptions : null ,
9
+ config : {
10
+ formatOnSave : {
11
+ order : 1 ,
12
+ title : 'Format on save' ,
13
+ description : "Automatically format when saving" ,
14
+ type : 'boolean' ,
15
+ default : false
16
+ } ,
17
+ rubyExecutable : {
18
+ order : 2 ,
19
+ type : 'string' ,
20
+ default : 'ruby' ,
21
+ title : "Ruby executable" ,
22
+ description : "Can be overriden with an absolute path"
23
+ } ,
24
+ rubyfmtExecutable : {
25
+ order : 3 ,
26
+ type : 'string' ,
27
+ default : 'rubyfmt' ,
28
+ title : "Rubyfmt executable" ,
29
+ description : "Can be overriden with an absolute path"
30
+ } ,
31
+ } ,
9
32
10
33
activate ( state ) {
11
34
this . subscriptions = new CompositeDisposable ( ) ;
12
35
this . subscriptions . add ( atom . commands . add ( 'atom-text-editor' , {
13
- 'rubyfmt:formatBuffer' : ( ) => this . formatBuffer ( )
36
+ 'rubyfmt:formatBuffer' : ( ) => this . handleManualFormat ( )
14
37
} ) ) ;
38
+ this . subscriptions . add ( atom . workspace . observeTextEditors ( textEditor => {
39
+ buffer = textEditor . getBuffer ( )
40
+ this . subscriptions . add ( buffer . onWillSave ( ( ) => this . handleSave ( textEditor ) ) )
41
+ } ) )
15
42
} ,
16
43
17
44
deactivate ( ) {
18
45
this . subscriptions . dispose ( ) ;
19
46
} ,
20
47
21
- formatBuffer ( ) {
22
- activePaneItem = atom . workspace . getActivePaneItem ( ) ;
23
- if ( activePaneItem . getText ) {
24
- text = activePaneItem . getText ( )
25
- this . format ( text ) . then ( ( formattedText ) => {
26
- activePaneItem . setText ( formattedText ) ;
27
- } ) ;
48
+ handleManualFormat ( ) {
49
+ item = atom . workspace . getActivePaneItem ( )
50
+ if ( item . getBuffer ) {
51
+ this . formatEditor ( item ) ;
28
52
}
29
53
} ,
30
54
55
+ handleSave ( textEditor ) {
56
+ if ( this . isRuby ( textEditor ) && atom . config . get ( 'rubyfmt.formatOnSave' ) ) { this . formatEditor ( textEditor ) }
57
+ } ,
58
+
59
+ isRuby ( textEditor ) {
60
+ return textEditor . getRootScopeDescriptor ( ) . getScopesArray ( ) . some ( scope => {
61
+ return scope . startsWith ( "source.ruby" )
62
+ } )
63
+ } ,
64
+
65
+ formatEditor ( textEditor ) {
66
+ return this . format ( textEditor . getText ( ) ) . then ( ( formattedText ) => {
67
+ textEditor . setText ( formattedText ) ;
68
+ } ) ;
69
+ } ,
70
+
31
71
format ( text ) {
32
72
var promise = new Promise ( function ( resolve , reject ) {
33
73
var toReturn
34
- rubyfmt = spawn ( 'rubyfmt' , [ ] , { windowsHide : true } )
74
+ executable = atom . config . get ( 'rubyfmt.rubyfmtExecutable' )
75
+ rubyfmt = spawn ( executable , [ ] , { windowsHide : true } )
35
76
rubyfmt . stdout . on ( 'data' , ( data ) => {
36
77
toReturn = data . toString ( 'utf8' ) ;
37
78
} ) ;
@@ -42,8 +83,12 @@ export default {
42
83
43
84
rubyfmt . stdin . write ( text ) ;
44
85
rubyfmt . stdin . end ( ) ;
45
- rubyfmt . on ( 'close' , ( ) => {
46
- resolve ( toReturn ) ;
86
+ rubyfmt . on ( 'close' , ( status ) => {
87
+ if ( status == 0 ) {
88
+ resolve ( toReturn ) ;
89
+ } else {
90
+ reject ( status )
91
+ }
47
92
} ) ;
48
93
} ) ;
49
94
return promise ;
0 commit comments