@@ -142,6 +142,15 @@ function extendRange(doc, rng) {
142142 return r ;
143143}
144144
145+ function rangeEditByType ( type ) {
146+ return ( doc , rng ) => {
147+ rng = extendRange ( doc , rng ) ;
148+ return beautifyDoc ( doc , rng , type )
149+ . then ( newText => documentEdit ( rng , newText ) )
150+ . catch ( dumpError ) ;
151+ } ;
152+ }
153+
145154function beautifyOnSave ( doc ) {
146155
147156 if ( doc . beautified ) {
@@ -197,25 +206,17 @@ function activate(context) {
197206 //VS Code won't allow the formatters to run for json, or js. The inbuild
198207 //js-beautify runs instead
199208 context . subscriptions . push (
200- vscode . languages . registerDocumentRangeFormattingEditProvider ( {
201- language : 'html'
202- } , {
203- provideDocumentRangeFormattingEdits : ( doc , rng ) => {
204- rng = extendRange ( doc , rng ) ;
205- return beautifyDoc ( doc , rng , 'html' )
206- . then ( newText => documentEdit ( rng , newText ) )
207- . catch ( dumpError ) ;
208- }
209+ vscode . languages . registerDocumentRangeFormattingEditProvider ( 'html' , {
210+ provideDocumentRangeFormattingEdits : rangeEditByType ( 'html' )
209211 } ) ) ;
210- context . subscriptions . push ( vscode . languages . registerDocumentRangeFormattingEditProvider ( {
211- language : 'css'
212- } , {
213- provideDocumentRangeFormattingEdits : ( doc , rng ) => {
214- rng = extendRange ( doc , rng ) ;
215- return beautifyDoc ( doc , rng , 'css' )
216- . then ( newText => documentEdit ( rng , newText ) )
217- . catch ( dumpError ) ;
218- }
212+ context . subscriptions . push ( vscode . languages . registerDocumentRangeFormattingEditProvider ( 'css' , {
213+ provideDocumentRangeFormattingEdits : rangeEditByType ( 'css' )
214+ } ) ) ;
215+ context . subscriptions . push ( vscode . languages . registerDocumentRangeFormattingEditProvider ( 'javascript' , {
216+ provideDocumentRangeFormattingEdits : rangeEditByType ( 'js' )
217+ } ) ) ;
218+ context . subscriptions . push ( vscode . languages . registerDocumentRangeFormattingEditProvider ( 'json' , {
219+ provideDocumentRangeFormattingEdits : rangeEditByType ( 'js' )
219220 } ) ) ;
220221 vscode . workspace . onDidSaveTextDocument ( beautifyOnSave ) ;
221222
0 commit comments