@@ -38,7 +38,7 @@ export class Debug {
38
38
39
39
log ( ...args : any [ ] ) {
40
40
if ( this . active ) {
41
- console . debug ( ...args ) ;
41
+ console . log ( ...args ) ;
42
42
}
43
43
}
44
44
@@ -93,27 +93,51 @@ export class Debug {
93
93
}
94
94
95
95
const cursor = node . tree . walk ( ) ;
96
- this . printCursorLocationInfo ( cursor , 0 ) ;
97
-
98
- for ( let i = 1 ; i < ancestors . length ; ++ i ) {
99
- cursor . gotoFirstChild ( ) ;
100
- while ( cursor . currentNode ( ) . id !== ancestors [ i ] . id ) {
101
- if ( ! cursor . gotoNextSibling ( ) ) {
102
- return ;
103
- }
96
+ this . printCursorLocationInfo ( ancestors , cursor , 0 ) ;
97
+ }
98
+
99
+ private printCursorLocationInfo (
100
+ nodes : SyntaxNode [ ] ,
101
+ cursor : TreeCursor ,
102
+ index : number ,
103
+ ) {
104
+ const field = cursor . currentFieldName ( ) ;
105
+ const fieldText = field != null ? `${ field } : ` : "" ;
106
+ const indent = " " . repeat ( index ) ;
107
+ const nodeIsLast = index === nodes . length - 1 ;
108
+ const { nodeIsNamed } = cursor ;
109
+ let text = `${ indent } ${ fieldText } ` ;
110
+
111
+ if ( nodeIsNamed ) {
112
+ text += `(${ cursor . nodeType } ` ;
113
+ if ( nodeIsLast ) {
114
+ text += ")" ;
104
115
}
105
- this . printCursorLocationInfo ( cursor , i ) ;
116
+ } else {
117
+ text += `"${ cursor . nodeType } "` ;
118
+ }
119
+
120
+ console . log ( text ) ;
121
+
122
+ if (
123
+ ! nodeIsLast &&
124
+ this . cursorGoToChildWithId ( cursor , nodes [ index + 1 ] . id )
125
+ ) {
126
+ this . printCursorLocationInfo ( nodes , cursor , index + 1 ) ;
106
127
}
107
128
108
- const leafText = ancestors [ ancestors . length - 1 ] . text
109
- . replace ( / \s + / g, " " )
110
- . substring ( 0 , 100 ) ;
111
- console . debug ( ">" . repeat ( ancestors . length ) , `"${ leafText } "` ) ;
129
+ if ( nodeIsNamed && ! nodeIsLast ) {
130
+ console . log ( `${ indent } )` ) ;
131
+ }
112
132
}
113
133
114
- private printCursorLocationInfo ( cursor : TreeCursor , depth : number ) {
115
- const field = cursor . currentFieldName ( ) ;
116
- const fieldText = field != null ? `${ field } : ` : "" ;
117
- console . debug ( ">" . repeat ( depth + 1 ) , `${ fieldText } ${ cursor . nodeType } ` ) ;
134
+ private cursorGoToChildWithId ( cursor : TreeCursor , id : number ) : boolean {
135
+ cursor . gotoFirstChild ( ) ;
136
+ while ( cursor . currentNode ( ) . id !== id ) {
137
+ if ( ! cursor . gotoNextSibling ( ) ) {
138
+ return false ;
139
+ }
140
+ }
141
+ return true ;
118
142
}
119
143
}
0 commit comments