1- import * as path from ' path'
1+ import * as path from " path" ;
22import {
33 Disposable ,
44 TextDocument ,
@@ -7,201 +7,186 @@ import {
77 TextEditorOptions ,
88 window ,
99 workspace ,
10- } from ' vscode'
11- import { KnownProps } from ' editorconfig'
10+ } from " vscode" ;
11+ import { KnownProps } from " editorconfig" ;
1212
1313import {
1414 InsertFinalNewline ,
1515 PreSaveTransformation ,
1616 SetEndOfLine ,
1717 TrimTrailingWhitespace ,
18- } from ' ./transformations'
18+ } from " ./transformations" ;
1919import {
2020 applyTextEditorOptions ,
2121 resolveCoreConfig ,
2222 resolveFile ,
2323 resolveTextEditorOptions ,
24- } from ' ./api'
24+ } from " ./api" ;
2525
26- type Charset = Exclude < KnownProps [ ' charset' ] , undefined | ' unset' >
27- type EncodingMap = Record < Charset , TextDocument [ ' encoding' ] >
26+ type Charset = Exclude < KnownProps [ " charset" ] , undefined | " unset" > ;
27+ type EncodingMap = Record < Charset , TextDocument [ " encoding" ] > ;
2828const encodingMap = {
29- ' utf-8' : ' utf8' ,
30- ' utf-8-bom' : ' utf8bom' ,
31- ' utf-16le' : ' utf16le' ,
32- ' utf-16be' : ' utf16be' ,
33- latin1 : ' iso88591' ,
34- } as const satisfies EncodingMap
29+ " utf-8" : " utf8" ,
30+ " utf-8-bom" : " utf8bom" ,
31+ " utf-16le" : " utf16le" ,
32+ " utf-16be" : " utf16be" ,
33+ " latin1" : " iso88591" ,
34+ } as const satisfies EncodingMap ;
3535
3636export default class DocumentWatcher {
37- private disposable : Disposable
37+ private disposable : Disposable ;
3838 private preSaveTransformations : PreSaveTransformation [ ] = [
3939 new SetEndOfLine ( ) ,
4040 new TrimTrailingWhitespace ( ) ,
4141 new InsertFinalNewline ( ) ,
42- ]
43- private doc ?: TextDocument
42+ ] ;
43+ private doc ?: TextDocument ;
4444
45- public constructor (
46- private outputChannel = window . createOutputChannel ( 'EditorConfig' ) ,
47- ) {
48- this . log ( 'Initializing document watcher...' )
45+ public constructor ( private outputChannel = window . createOutputChannel ( "EditorConfig" ) ) {
46+ this . log ( "Initializing document watcher..." ) ;
4947
50- const subscriptions : Disposable [ ] = [ ]
48+ const subscriptions : Disposable [ ] = [ ] ;
5149
52- this . handleTextEditorChange ( window . activeTextEditor )
50+ this . handleTextEditorChange ( window . activeTextEditor ) ;
5351
5452 subscriptions . push (
55- window . onDidChangeActiveTextEditor ( async editor => {
56- this . handleTextEditorChange ( editor )
53+ window . onDidChangeActiveTextEditor ( async ( editor ) => {
54+ this . handleTextEditorChange ( editor ) ;
5755 } ) ,
58- )
56+ ) ;
5957
6058 subscriptions . push (
61- window . onDidChangeWindowState ( async state => {
59+ window . onDidChangeWindowState ( async ( state ) => {
6260 if ( state . focused && this . doc ) {
6361 const newOptions = await resolveTextEditorOptions ( this . doc , {
6462 onEmptyConfig : this . onEmptyConfig ,
65- } )
63+ } ) ;
6664 applyTextEditorOptions ( newOptions , {
6765 onNoActiveTextEditor : this . onNoActiveTextEditor ,
6866 onSuccess : this . onSuccess ,
69- } )
67+ } ) ;
7068 }
7169 } ) ,
72- )
70+ ) ;
7371
7472 subscriptions . push (
75- workspace . onDidSaveTextDocument ( doc => {
76- if ( path . basename ( doc . fileName ) === ' .editorconfig' ) {
77- this . log ( ' .editorconfig file saved.' )
73+ workspace . onDidSaveTextDocument ( ( doc ) => {
74+ if ( path . basename ( doc . fileName ) === " .editorconfig" ) {
75+ this . log ( " .editorconfig file saved." ) ;
7876 }
7977 // in case document was dirty on text editor change
80- this . handleDocumentEncoding ( doc )
78+ this . handleDocumentEncoding ( doc ) ;
8179 } ) ,
82- )
80+ ) ;
8381
8482 subscriptions . push (
85- workspace . onWillSaveTextDocument ( async e => {
86- const transformations = this . calculatePreSaveTransformations (
87- e . document ,
88- e . reason ,
89- )
90- e . waitUntil ( transformations )
83+ workspace . onWillSaveTextDocument ( async ( e ) => {
84+ const transformations = this . calculatePreSaveTransformations ( e . document , e . reason ) ;
85+ e . waitUntil ( transformations ) ;
9186 } ) ,
92- )
87+ ) ;
9388
94- this . disposable = Disposable . from . apply ( this , subscriptions )
95- this . log ( ' Document watcher initialized' )
89+ this . disposable = Disposable . from . apply ( this , subscriptions ) ;
90+ this . log ( " Document watcher initialized" ) ;
9691 }
9792
9893 public onEmptyConfig = ( relativePath : string ) => {
99- this . log ( `${ relativePath } : No configuration.` )
100- }
94+ this . log ( `${ relativePath } : No configuration.` ) ;
95+ } ;
10196
10297 public onBeforeResolve = ( relativePath : string ) => {
103- this . log ( `${ relativePath } : Using EditorConfig core...` )
104- }
98+ this . log ( `${ relativePath } : Using EditorConfig core...` ) ;
99+ } ;
105100
106101 public onNoActiveTextEditor = ( ) => {
107- this . log ( ' No more open editors.' )
108- }
102+ this . log ( " No more open editors." ) ;
103+ } ;
109104
110105 public onSuccess = ( newOptions : TextEditorOptions ) => {
111106 if ( ! this . doc ) {
112- this . log ( `[no file]: ${ JSON . stringify ( newOptions ) } ` )
113- return
107+ this . log ( `[no file]: ${ JSON . stringify ( newOptions ) } ` ) ;
108+ return ;
114109 }
115- const { relativePath } = resolveFile ( this . doc )
116- this . log ( `${ relativePath } : ${ JSON . stringify ( newOptions ) } ` )
117- }
110+ const { relativePath } = resolveFile ( this . doc ) ;
111+ this . log ( `${ relativePath } : ${ JSON . stringify ( newOptions ) } ` ) ;
112+ } ;
118113
119114 public log ( ...messages : string [ ] ) {
120- this . outputChannel . appendLine ( messages . join ( ' ' ) )
115+ this . outputChannel . appendLine ( messages . join ( " " ) ) ;
121116 }
122117
123118 public dispose ( ) {
124- this . disposable . dispose ( )
119+ this . disposable . dispose ( ) ;
125120 }
126121
127- private async calculatePreSaveTransformations (
128- doc : TextDocument ,
129- reason : TextDocumentSaveReason ,
130- ) {
122+ private async calculatePreSaveTransformations ( doc : TextDocument , reason : TextDocumentSaveReason ) {
131123 const editorconfigSettings = await resolveCoreConfig ( doc , {
132124 onBeforeResolve : this . onBeforeResolve ,
133- } )
134- const relativePath = workspace . asRelativePath ( doc . fileName )
125+ } ) ;
126+ const relativePath = workspace . asRelativePath ( doc . fileName ) ;
135127
136128 if ( ! editorconfigSettings ) {
137- this . log ( `${ relativePath } : No configuration found for pre-save.` )
138- return [ ]
129+ this . log ( `${ relativePath } : No configuration found for pre-save.` ) ;
130+ return [ ] ;
139131 }
140132
141133 return [
142- ...this . preSaveTransformations . flatMap ( transformer => {
143- const { edits, message } = transformer . transform (
144- editorconfigSettings ,
145- doc ,
146- reason ,
147- )
134+ ...this . preSaveTransformations . flatMap ( ( transformer ) => {
135+ const { edits, message } = transformer . transform ( editorconfigSettings , doc , reason ) ;
148136 if ( edits instanceof Error ) {
149- this . log ( `${ relativePath } : ${ edits . message } ` )
150- return [ ]
137+ this . log ( `${ relativePath } : ${ edits . message } ` ) ;
138+ return [ ] ;
151139 }
152140 if ( message ) {
153- this . log ( `${ relativePath } : ${ message } ` )
141+ this . log ( `${ relativePath } : ${ message } ` ) ;
154142 }
155- return edits
143+ return edits ;
156144 } ) ,
157- ]
145+ ] ;
158146 }
159147
160148 private async handleTextEditorChange ( editor ?: TextEditor ) {
161149 if ( editor ?. document ) {
162- const newOptions = await resolveTextEditorOptions (
163- ( this . doc = editor . document ) ,
164- {
165- onEmptyConfig : this . onEmptyConfig ,
166- } ,
167- )
150+ const newOptions = await resolveTextEditorOptions ( ( this . doc = editor . document ) , {
151+ onEmptyConfig : this . onEmptyConfig ,
152+ } ) ;
168153 applyTextEditorOptions ( newOptions , {
169154 onNoActiveTextEditor : this . onNoActiveTextEditor ,
170155 onSuccess : this . onSuccess ,
171- } )
172- this . handleDocumentEncoding ( editor . document )
156+ } ) ;
157+ this . handleDocumentEncoding ( editor . document ) ;
173158 }
174159 }
175160
176161 private async handleDocumentEncoding ( document : TextDocument ) {
177- const relativePath = workspace . asRelativePath ( document . fileName )
162+ const relativePath = workspace . asRelativePath ( document . fileName ) ;
178163 const editorconfigSettings = await resolveCoreConfig ( document , {
179164 onBeforeResolve : this . onBeforeResolve ,
180- } )
165+ } ) ;
181166
182- const { charset } = editorconfigSettings
183- this . log ( `${ relativePath } : Target charset is` , charset ?? ' not set' )
167+ const { charset } = editorconfigSettings ;
168+ this . log ( `${ relativePath } : Target charset is` , charset ?? " not set" ) ;
184169 if ( ! charset ) {
185- return
170+ return ;
186171 }
187172 if ( ! ( charset in encodingMap ) ) {
188- this . log ( `${ relativePath } : Unsupported charset` )
189- return
173+ this . log ( `${ relativePath } : Unsupported charset` ) ;
174+ return ;
190175 }
191176
192- const targetEncoding = encodingMap [ charset as keyof typeof encodingMap ]
177+ const targetEncoding = encodingMap [ charset as keyof typeof encodingMap ] ;
193178 if ( targetEncoding === document . encoding ) {
194- return
179+ return ;
195180 }
196181
197182 if ( document . isDirty ) {
198- this . log ( `${ relativePath } : Cannot change encoding, document is dirty` )
199- return
183+ this . log ( `${ relativePath } : Cannot change encoding, document is dirty` ) ;
184+ return ;
200185 }
201186
202- this . log ( `${ relativePath } : Re-opening document with ${ targetEncoding } encoding...` )
187+ this . log ( `${ relativePath } : Re-opening document with ${ targetEncoding } encoding...` ) ;
203188 await workspace . openTextDocument ( document . uri , {
204189 encoding : targetEncoding ,
205- } )
190+ } ) ;
206191 }
207192}
0 commit comments