1111import java .util .ArrayList ;
1212import java .util .List ;
1313import java .util .Objects ;
14+ import java .util .Optional ;
1415import org .jspecify .annotations .NullMarked ;
1516
1617@ NullMarked
@@ -40,12 +41,13 @@ private String renderTree(TreeEntry entry, Depth depth) {
4041
4142 private String renderDirectory (Depth depth , DirectoryEntry dirEntry , List <Path > compactPaths ) {
4243
44+ Optional <String > extension = null ;
4345 if (options .areCompactDirectoriesUsed ()
4446 && !depth .isRoot ()
4547 && dirEntry .getEntries ().size () == 1
4648 && dirEntry .getEntries ().get (0 ) instanceof DirectoryEntry childDirEntry ) {
4749
48- var extension = computeLineExtension (dirEntry .getDir ());
50+ extension = computeLineExtension (dirEntry .getDir ());
4951 if (extension .isEmpty ()) {
5052 var newCompactPaths = new ArrayList <>(compactPaths );
5153 newCompactPaths .add (childDirEntry .getDir ());
@@ -54,7 +56,10 @@ private String renderDirectory(Depth depth, DirectoryEntry dirEntry, List<Path>
5456 }
5557
5658 var line = lineRenderer .renderDirectoryBegin (depth , dirEntry , compactPaths );
57- line += computeLineExtension (dirEntry .getDir ());
59+ if (extension == null ) {
60+ extension = computeLineExtension (dirEntry .getDir ());
61+ }
62+ line += extension .orElse ("" );
5863
5964 var childIt = dirEntry .getEntries ().iterator ();
6065
@@ -76,17 +81,16 @@ private String renderDirectory(Depth depth, DirectoryEntry dirEntry, List<Path>
7681 return line + childLines .toString ();
7782 }
7883
79- private String computeLineExtension (Path path ) {
84+ private Optional < String > computeLineExtension (Path path ) {
8085 if (options .getLineExtension () == null ) {
81- return "" ;
86+ return Optional . empty () ;
8287 }
83- var extension = options .getLineExtension ().apply (path );
84- return extension == null ? "" : extension ;
88+ return Optional .ofNullable (options .getLineExtension ().apply (path ));
8589 }
8690
8791 private String renderFile (Depth depth , FileEntry fileEntry ) {
8892 var line = lineRenderer .renderFile (depth , fileEntry );
89- line += computeLineExtension (fileEntry .getFile ());
93+ line += computeLineExtension (fileEntry .getFile ()). orElse ( "" ) ;
9094 return line ;
9195 }
9296
0 commit comments