22// See LICENSE in the project root for license information.
33
44import { Token } from './Tokenizer' ;
5+ import { TextRange } from './TextRange' ;
56
67export enum AstKind {
78 None ,
@@ -18,27 +19,29 @@ export enum AstKind {
1819 */
1920export abstract class AstBaseNode {
2021 public readonly kind : AstKind = AstKind . None ;
22+ public range : TextRange | undefined ;
2123
2224 /**
2325 * Returns a diagnostic dump of the tree, showing the prefix/suffix/separator for
2426 * each node.
2527 */
2628 public getDump ( indent : string = '' ) : string {
27- let result : string = indent + AstKind [ this . kind ] ;
29+ const nestedIndent : string = indent + ' ' ;
30+ let result : string = indent + '- ' + AstKind [ this . kind ] + ':\n' ;
2831
2932 const dumpText : string | undefined = this . getDumpText ( ) ;
3033 if ( dumpText ) {
31- result += '=' + JSON . stringify ( dumpText ) ;
34+ result += nestedIndent + 'Value=' + JSON . stringify ( dumpText ) + '\n' ;
35+ }
36+
37+ const fullRange : TextRange = this . getFullRange ( ) ;
38+ if ( ! fullRange . isEmpty ( ) ) {
39+ result += nestedIndent + 'Range=' + JSON . stringify ( fullRange . toString ( ) ) + '\n' ;
3240 }
3341
3442 const childNodes : AstBaseNode [ ] = this . getChildNodes ( ) ;
35- if ( childNodes . length === 0 ) {
36- result += '\n' ;
37- } else {
38- result += ':\n' ;
39- for ( const child of this . getChildNodes ( ) ) {
40- result += child . getDump ( indent + ' ' ) ;
41- }
43+ for ( const child of childNodes ) {
44+ result += child . getDump ( nestedIndent ) ;
4245 }
4346
4447 return result ;
@@ -50,6 +53,20 @@ export abstract class AstBaseNode {
5053 return nodes ;
5154 }
5255
56+ public getFullRange ( ) : TextRange {
57+ if ( this . range ) {
58+ return this . range ;
59+ }
60+
61+ let encompassingRange : TextRange = TextRange . empty ;
62+
63+ for ( const child of this . getChildNodes ( ) ) {
64+ encompassingRange = encompassingRange . getEncompassingRange ( child . getFullRange ( ) ) ;
65+ }
66+
67+ return encompassingRange ;
68+ }
69+
5370 protected abstract collectChildNodesInto ( nodes : AstBaseNode [ ] ) : void ;
5471
5572 protected getDumpText ( ) : string | undefined {
0 commit comments