@@ -7,14 +7,16 @@ type formattingStyle = {
77 tabType : ' ' | '\t' ;
88 tabSize : number ;
99 wsBrackets : string ;
10+ eol : '\r\n' | '\n' ;
1011} ;
1112
12- function getFormattingStyle ( options ?: vscode . FormattingOptions ) : formattingStyle {
13+ function getFormattingStyle ( options ?: vscode . FormattingOptions , document ?: vscode . TextDocument ) : formattingStyle {
1314 const bracketStyle = < 'tight' | 'default' > vscode . workspace . getConfiguration ( 'json.textmate' ) . get ( 'formattingStyle' ) ;
1415 const style : formattingStyle = {
1516 tabType : options ?. insertSpaces ? ' ' : '\t' ,
1617 tabSize : options ?. insertSpaces ? options ?. tabSize : 1 ,
1718 wsBrackets : bracketStyle == 'tight' ? '' : ' ' ,
19+ eol : document ?. eol == vscode . EndOfLine . CRLF ? '\r\n' : '\n' ,
1820 } ;
1921 return style ;
2022}
@@ -27,7 +29,7 @@ export const DocumentFormattingEditProvider: vscode.DocumentFormattingEditProvid
2729 const jsonTree = trees . jsonTree ;
2830 const textEdits : vscode . TextEdit [ ] = [ ] ;
2931
30- const style = getFormattingStyle ( options ) ;
32+ const style = getFormattingStyle ( options , document ) ;
3133
3234 // const start = performance.now();
3335 formatChildren ( jsonTree . rootNode , textEdits , 0 , style ) ;
@@ -45,7 +47,7 @@ export const DocumentRangeFormattingEditProvider: vscode.DocumentRangeFormatting
4547 const jsonTree = trees . jsonTree ;
4648 const textEdits : vscode . TextEdit [ ] = [ ] ;
4749
48- const style = getFormattingStyle ( options ) ;
50+ const style = getFormattingStyle ( options , document ) ;
4951
5052 const startPoint = toPoint ( range . start ) ;
5153 const endPoint = toPoint ( range . end ) ;
@@ -114,7 +116,7 @@ export const OnTypeFormattingEditProvider: vscode.OnTypeFormattingEditProvider =
114116 return ;
115117 }
116118
117- const style = getFormattingStyle ( options ) ;
119+ const style = getFormattingStyle ( options , document ) ;
118120
119121 let level = 0 ;
120122 let parent = node . parent ;
@@ -224,7 +226,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
224226 nextSibling ?. startPosition ?? parentNode . endPosition ,
225227 ) ,
226228 expand ?
227- node . type + '\n '. padEnd ( indent + 1 , style . tabType ) :
229+ node . type + style . eol + ' '. padEnd ( indent , style . tabType ) :
228230 node . type + style . wsBrackets ,
229231 ) ,
230232 ) ;
@@ -241,7 +243,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
241243 node . endPosition ,
242244 ) ,
243245 expand ?
244- '\n '. padEnd ( indent + 1 , style . tabType ) + node . type :
246+ style . eol + ' '. padEnd ( indent , style . tabType ) + node . type :
245247 style . wsBrackets + node . type ,
246248 ) ,
247249 ) ;
@@ -264,7 +266,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
264266 nextSibling ?. startPosition ?? parentNode . endPosition ,
265267 ) ,
266268 expand ?
267- ',\n' . padEnd ( indent + 2 , style . tabType ) :
269+ ',' + style . eol + '' . padEnd ( indent , style . tabType ) :
268270 ', ' ,
269271 ) ,
270272 ) ;
0 commit comments