1- nova . commands . register ( "padraig.swiftformat.formatDocument" , formatDocument ) ;
1+ 'use strict' ;
22
3+ nova . commands . register ( "padraig.swiftformat.formatDocument" , formatDocument ) ;
34const compositeDisposable = new CompositeDisposable ( ) ;
4-
5- exports . activate = function ( ) {
5+ function activate ( ) {
66 compositeDisposable . add ( nova . workspace . onDidAddTextEditor ( watchEditor ) ) ;
77}
8-
9- exports . deactivate = function ( ) {
8+ function deactivate ( ) {
109 compositeDisposable . dispose ( ) ;
1110}
12-
1311async function swiftFormat ( text ) {
14- let args = [ "--quiet" ]
15-
16- const version = swiftVersion ( )
17- const config = configurationPath ( )
18-
19- if ( version ) {
20- args . push ( "--swiftversion" , version )
21- }
22-
23- if ( config ) {
24- args . push ( "--config" , config )
25- }
26-
27- const process = new Process ( executablePath ( ) , {
28- "args" : args ,
29- "shell" : true ,
30- "stdin" : "pipe"
31- } )
32-
33- let lines = [ ] ;
34- let errorLines = [ ] ;
35-
36- process . onStdout ( ( data ) => {
37- if ( data ) {
38- lines . push ( data ) ;
39- }
40- } ) ;
41-
42- process . onStderr ( ( data ) => {
43- if ( data ) {
44- errorLines . push ( data ) ;
45- }
46- } ) ;
47-
48- const exitPromise = new Promise ( ( resolve ) => {
49- process . onDidExit ( ( status ) => {
50- resolve ( status ) ;
51- } ) ;
52- } )
53-
54- const writer = process . stdin . getWriter ( ) ;
55-
56- process . start ( ) ;
57-
58- await writer . ready ;
59- writer . write ( text ) ;
60- writer . close ( ) ;
61-
62- const status = await exitPromise ;
63-
64- switch ( status ) {
65- case 0 :
66- break ;
67- case 70 :
68- console . log ( errorLines . join ( "" ) ) ;
69- throw new Error ( `Program Error: ${ errorLines . join ( "" ) } ` )
70- break ;
71- case 127 :
72- console . log ( errorLines . join ( "" ) ) ;
73- throw new Error ( `Couldn't find the swiftformat executable at ${ executablePath ( ) } ` ) ;
74- break ;
75- default :
76- console . log ( errorLines . join ( "" ) ) ;
77- throw new Error ( `${ status } - ${ errorLines . join ( "" ) } ` )
78- }
79-
80- return lines . join ( "" ) ;
12+ let args = [ "--quiet" ] ;
13+ const version = swiftVersion ( ) ;
14+ const config = configurationPath ( ) ;
15+ if ( version ) {
16+ args . push ( "--swiftversion" , version ) ;
17+ }
18+ if ( config ) {
19+ args . push ( "--config" , config ) ;
20+ }
21+ const process = new Process ( executablePath ( ) , {
22+ args,
23+ shell : true ,
24+ stdio : "pipe"
25+ } ) ;
26+ let lines = [ ] ;
27+ let errorLines = [ ] ;
28+ process . onStdout ( ( data ) => {
29+ if ( data ) {
30+ lines . push ( data ) ;
31+ }
32+ } ) ;
33+ process . onStderr ( ( data ) => {
34+ if ( data ) {
35+ errorLines . push ( data ) ;
36+ }
37+ } ) ;
38+ const exitPromise = new Promise ( ( resolve ) => {
39+ process . onDidExit ( ( status ) => {
40+ resolve ( status ) ;
41+ } ) ;
42+ } ) ;
43+ if ( ! process . stdin ) {
44+ throw new Error ( "No stdin on process" ) ;
45+ }
46+ const writer = process . stdin . getWriter ( ) ;
47+ process . start ( ) ;
48+ await writer . ready ;
49+ writer . write ( text ) ;
50+ writer . close ( ) ;
51+ const status = await exitPromise ;
52+ switch ( status ) {
53+ case 0 :
54+ break ;
55+ case 70 :
56+ console . log ( errorLines . join ( "" ) ) ;
57+ throw new Error ( `Program Error: ${ errorLines . join ( "" ) } ` ) ;
58+ case 127 :
59+ console . log ( errorLines . join ( "" ) ) ;
60+ throw new Error ( `Couldn't find the swiftformat executable at ${ executablePath ( ) } ` ) ;
61+ default :
62+ console . log ( errorLines . join ( "" ) ) ;
63+ throw new Error ( `${ status } - ${ errorLines . join ( "" ) } ` ) ;
64+ }
65+ return lines . join ( "" ) ;
8166}
82-
83- async function formatDocument ( editor ) {
84- const fullRange = new Range ( 0 , editor . document . length )
67+ async function formatDocument ( editor ) {
68+ const fullRange = new Range ( 0 , editor . document . length ) ;
8569 const originalText = editor . document . getTextInRange ( fullRange ) ;
86-
8770 try {
88- const formattedText = await swiftFormat ( originalText ) ;
89-
90- // Clear previous notification if we tried again and succeeded.
91- nova . notifications . cancel ( "swiftformat-process-failed" ) ;
92-
93- if ( formattedText != originalText ) {
94- await editor . edit ( ( edit ) => {
95- edit . replace ( fullRange , formattedText ) ;
96- } ) ;
97- }
98- } catch ( e ) {
71+ const formattedText = await swiftFormat ( originalText ) ;
72+ // Clear previous notification if we tried again and succeeded.
73+ nova . notifications . cancel ( "swiftformat-process-failed" ) ;
74+ if ( formattedText != originalText ) {
75+ await editor . edit ( ( edit ) => {
76+ edit . replace ( fullRange , formattedText ) ;
77+ } ) ;
78+ }
79+ }
80+ catch ( e ) {
9981 let request = new NotificationRequest ( "swiftformat-process-failed" ) ;
100-
10182 request . title = "Error Running SwiftFormat" ;
102- request . body = e . message ;
103-
83+ request . body = getErrorMessage ( e ) ;
10484 request . actions = [ nova . localize ( "OK" ) ] ;
10585 nova . notifications . add ( request ) ;
10686 }
10787}
108-
10988function watchEditor ( editor ) {
11089 const document = editor . document ;
111-
11290 if ( ! [ "swift" ] . includes ( document . syntax ?? "" ) ) {
113- return ;
91+ return null ;
11492 }
115-
11693 return editor . onWillSave ( async ( editor ) => {
11794 if ( shouldFormatOnSave ( ) ) {
118- await formatDocument ( editor ) ;
95+ await formatDocument ( editor ) ;
11996 }
12097 } ) ;
12198}
122-
123- function shouldFormatOnSave ( ) {
124- const configKey = "padraig.swiftformat.config.formatOnSave"
125- const str = nova . workspace . config . get ( configKey , "string" ) ;
126-
127- switch ( str ) {
128- case "disable" :
129- return false ;
130- case "enable" :
131- return true ;
132- default :
133- return nova . config . get ( configKey , "boolean" ) ?? false
134- }
99+ function shouldFormatOnSave ( ) {
100+ const configKey = "padraig.swiftformat.config.formatOnSave" ;
101+ const str = nova . workspace . config . get ( configKey , "string" ) ;
102+ switch ( str ) {
103+ case "disable" :
104+ return false ;
105+ case "enable" :
106+ return true ;
107+ default :
108+ return nova . config . get ( configKey , "boolean" ) ?? false ;
109+ }
135110}
136-
137- function customExecutablePath ( ) {
138- const configKey = "padraig.swiftformat.config.executablePath"
139- const workspacePath = nova . workspace . config . get ( configKey , "string" ) ;
140-
141- if ( workspacePath ) {
142- return workspacePath ;
143- }
144-
145- const globalPath = nova . config . get ( configKey , "string" ) ;
146-
147- if ( globalPath ) {
148- return globalPath ;
149- }
111+ function customExecutablePath ( ) {
112+ const configKey = "padraig.swiftformat.config.executablePath" ;
113+ const workspacePath = nova . workspace . config . get ( configKey , "string" ) ;
114+ if ( workspacePath ) {
115+ return workspacePath ;
116+ }
117+ const globalPath = nova . config . get ( configKey , "string" ) ;
118+ if ( globalPath ) {
119+ return globalPath ;
120+ }
121+ return null ;
150122}
151-
152- function executablePath ( ) {
153- return customExecutablePath ( ) ?? nova . path . join ( nova . extension . path , "/bin/swiftformat" ) ;
123+ function executablePath ( ) {
124+ return customExecutablePath ( ) ?? nova . path . join ( nova . extension . path , "/bin/swiftformat" ) ;
154125}
155-
156- function configurationPath ( ) {
157- if ( nova . workspace . path ) {
158- const path = nova . path . join ( nova . workspace . path , ".swiftformat" ) ;
159-
160- if ( nova . fs . stat ( path ) ) {
161- return path
126+ function configurationPath ( ) {
127+ if ( nova . workspace . path ) {
128+ const path = nova . path . join ( nova . workspace . path , ".swiftformat" ) ;
129+ if ( nova . fs . stat ( path ) ) {
130+ return path ;
131+ }
162132 }
163- }
133+ return null ;
164134}
165-
166- function swiftVersion ( ) {
167- // If there's a `.swift-version` file, use that.
168- if ( nova . workspace . path ) {
169- const swiftVersionPath = nova . path . join ( nova . workspace . path , ".swift-version" ) ;
170-
171- if ( nova . fs . stat ( swiftVersionPath ) ) {
172- const file = nova . fs . open ( swiftVersionPath ) ;
173-
174- if ( file ) {
175- const version = file . readline ( )
176- return version
177- }
135+ function isTextMode ( file ) {
136+ return file . readline !== undefined ;
137+ }
138+ function swiftVersion ( ) {
139+ // If there's a `.swift-version` file, use that.
140+ if ( nova . workspace . path ) {
141+ const swiftVersionPath = nova . path . join ( nova . workspace . path , ".swift-version" ) ;
142+ if ( nova . fs . stat ( swiftVersionPath ) ) {
143+ const file = nova . fs . open ( swiftVersionPath , 'r' ) ;
144+ if ( isTextMode ( file ) ) {
145+ return file . readline ( ) . trim ( ) ;
146+ }
147+ file . close ( ) ;
148+ }
149+ }
150+ const configKey = "padraig.swiftformat.config.swiftVersion" ;
151+ const workspacePath = nova . workspace . config . get ( configKey , "string" ) ;
152+ if ( workspacePath ) {
153+ // If there's a workspace setting use that.
154+ return workspacePath ;
178155 }
179- }
180-
181- const configKey = "padraig.swiftformat.config.swiftVersion"
182- const workspacePath = nova . workspace . config . get ( configKey , "string" ) ;
183-
184- if ( workspacePath ) {
185- // If there's a workspace setting use that.
186- return workspacePath ;
187- }
188-
189- const globalPath = nova . config . get ( configKey , "string" ) ;
190-
191- if ( globalPath ) {
192- // If there's a workspace setting use that.
193- return globalPath ;
194- }
195-
196- // Otherwise, we won't send a version at all and swiftformat can do what it thinks is right.
197- }
156+ const globalPath = nova . config . get ( configKey , "string" ) ;
157+ if ( globalPath ) {
158+ // If there's a workspace setting use that.
159+ return globalPath ;
160+ }
161+ return null ;
162+ }
163+ function getErrorMessage ( error ) {
164+ if ( error instanceof Error )
165+ return error . message ;
166+ return String ( error ) ;
167+ }
168+
169+ exports . activate = activate ;
170+ exports . deactivate = deactivate ;
0 commit comments