11import type { Node } from "web-tree-sitter" ;
2+ import type { EndOfLine } from "../types.js" ;
3+ import { getEndOfLine } from "../util/getEndOfLine.js" ;
24import { getIndentation } from "../util/getIndentation.js" ;
35
46interface Options {
7+ readonly endOfLine ?: EndOfLine ;
58 readonly indentTabs ?: boolean ;
69 readonly indentWidth ?: number ;
710}
811
912export function treeSitterFormatter ( node : Node , options : Options = { } ) : string {
1013 const indentation = getIndentation ( options . indentTabs , options . indentWidth ) ;
11- const formatter = new TreeSitterFormatter ( indentation ) ;
14+ const eol = getEndOfLine ( options . endOfLine ) ;
15+ const formatter = new TreeSitterFormatter ( indentation , eol ) ;
1216 return formatter . getText ( node ) ;
1317}
1418
1519export class TreeSitterFormatter {
1620 private lastRow = 0 ;
1721
18- constructor ( private indentation : string ) { }
22+ constructor (
23+ private indentation : string ,
24+ private eol : string ,
25+ ) { }
1926
2027 getText ( node : Node ) : string {
21- return this . getNodeText ( node , 0 ) + "\n" ;
28+ return this . getNodeText ( node , 0 ) + this . eol ;
2229 }
2330
2431 private getNodeText ( node : Node , numIndents : number ) : string {
25- const nl = node . startPosition . row > this . lastRow + 1 ? "\n" : "" ;
32+ const nl = node . startPosition . row > this . lastRow + 1 ? this . eol : "" ;
2633 this . lastRow = node . endPosition . row ;
2734 const text = this . getNodeTextInternal ( node , numIndents ) ;
2835 this . lastRow = node . endPosition . row ;
@@ -51,7 +58,7 @@ export class TreeSitterFormatter {
5158 `${ this . getIndent ( numIndents ) } ${ first } ` ,
5259 ...interior ,
5360 `${ this . getIndent ( numIndents ) } ${ last } ` ,
54- ] . join ( "\n" ) ;
61+ ] . join ( this . eol ) ;
5562 }
5663
5764 private getListText ( node : Node , numIndents : number ) : string {
@@ -68,7 +75,7 @@ export class TreeSitterFormatter {
6875 . map ( ( n ) => this . getNodeText ( n , numIndents + 1 ) ) ,
6976 `${ this . getIndent ( numIndents ) } ${ last } ` ,
7077 ] ;
71- return parts . join ( "\n" ) ;
78+ return parts . join ( this . eol ) ;
7279 }
7380
7481 private getPredicateText ( node : Node , numIndents : number ) : string {
@@ -95,13 +102,15 @@ export class TreeSitterFormatter {
95102 . slice ( 1 )
96103 . map ( ( s ) => `${ this . getIndent ( numIndents + 1 ) } ${ s } ` ) ,
97104 `${ this . getIndent ( numIndents ) } ${ last } ` ,
98- ] . join ( "\n" ) ;
105+ ] . join ( this . eol ) ;
99106 }
100107
101108 private getFieldDefinitionText ( node : Node , numIndents : number ) : string {
102109 // Field definition directly in document root
103110 if ( numIndents === 0 ) {
104- return [ "(_" , this . getFieldDefinitionText ( node , 1 ) , ")" ] . join ( "\n" ) ;
111+ return [ "(_" , this . getFieldDefinitionText ( node , 1 ) , ")" ] . join (
112+ this . eol ,
113+ ) ;
105114 }
106115 // [lhs, ":", rhs]
107116 return [
@@ -182,7 +191,7 @@ export class TreeSitterFormatter {
182191 const nodesToUse = lastIsQuantifier ? nodes . slice ( 0 , - 1 ) : nodes ;
183192 const text = nodesToUse
184193 . map ( ( n ) => this . getNodeText ( n , numIndents ) )
185- . join ( "\n" ) ;
194+ . join ( this . eol ) ;
186195 return lastIsQuantifier
187196 ? `${ text } ${ nodes [ nodes . length - 1 ] . text } `
188197 : text ;
0 commit comments