@@ -124,6 +124,9 @@ private newtype TPrintAstNode =
124124 TJavadocNode ( Javadoc jd ) { exists ( Documentable d | d .getJavadoc ( ) = jd | shouldPrint ( d , _) ) } or
125125 TJavadocElementNode ( JavadocElement jd ) {
126126 exists ( Documentable d | d .getJavadoc ( ) = jd .getParent * ( ) | shouldPrint ( d , _) )
127+ } or
128+ TImportsNode ( CompilationUnit cu ) {
129+ shouldPrint ( cu , _) and exists ( Import i | i .getCompilationUnit ( ) = cu )
127130 }
128131
129132/**
@@ -395,13 +398,12 @@ final class CompilationUnitNode extends ElementNode {
395398
396399 CompilationUnitNode ( ) { cu = element }
397400
398- private Element getADeclaration ( ) {
399- cu .hasChildElement ( result )
400- or
401- result .( Import ) .getCompilationUnit ( ) = cu
402- }
401+ private Element getADeclaration ( ) { cu .hasChildElement ( result ) }
403402
404403 override PrintAstNode getChild ( int childIndex ) {
404+ childIndex = - 1 and
405+ result .( ImportsNode ) .getCompilationUnit ( ) = cu
406+ or
405407 childIndex >= 0 and
406408 result .( ElementNode ) .getElement ( ) =
407409 rank [ childIndex ] ( Element e , string file , int line , int column |
@@ -622,6 +624,32 @@ final class JavadocElementNode extends PrintAstNode, TJavadocElementNode {
622624 JavadocElement getJavadocElement ( ) { result = jd }
623625}
624626
627+ /**
628+ * A node representing the `Import`s of a `CompilationUnit`.
629+ * Only rendered if there is at least one import.
630+ */
631+ final class ImportsNode extends PrintAstNode , TImportsNode {
632+ CompilationUnit cu ;
633+
634+ ImportsNode ( ) { this = TImportsNode ( cu ) }
635+
636+ override string toString ( ) { result = "(Imports)" }
637+
638+ override ElementNode getChild ( int childIndex ) {
639+ result .getElement ( ) =
640+ rank [ childIndex ] ( Import im , string file , int line , int column |
641+ im .getCompilationUnit ( ) = cu and locationSortKeys ( im , file , line , column )
642+ |
643+ im order by file , line , column
644+ )
645+ }
646+
647+ /**
648+ * Gets the underlying CompilationUnit.
649+ */
650+ CompilationUnit getCompilationUnit ( ) { result = cu }
651+ }
652+
625653/** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
626654query predicate nodes ( PrintAstNode node , string key , string value ) { value = node .getProperty ( key ) }
627655
0 commit comments