Skip to content

Commit

Permalink
Better support for ASTParser.setFocalPoint()
Browse files Browse the repository at this point in the history
Reduce the parsed AST before resolving to get a performance boost.
  • Loading branch information
mickaelistria committed Nov 5, 2024
1 parent 7491ef5 commit ef0e050
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ public Void visitClass(ClassTree node, Void p) {
if (elements.hasNext() && elements.next() instanceof JCCompilationUnit u) {
javacCompilationUnit = u;
javacCompilationUnits.add(u);
if (sourceUnits.length == 1 && focalPoint >= 0) {
JavacUtils.trimUnvisibleContent(u, focalPoint, context);
}
} else {
return Map.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@

import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.Option;
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Options;

public class JavacUtils {
Expand Down Expand Up @@ -409,4 +415,25 @@ public static boolean isTest(IJavaProject project, org.eclipse.jdt.internal.comp
return true;
}
}

/// Removes non-relevant content (eg other method blocks) for given focal position
public static void trimUnvisibleContent(JCCompilationUnit u, int focalPoint, Context context) {
TreeMaker treeMaker = TreeMaker.instance(context);
u.accept(new TreeScanner() {
@Override
public void visitMethodDef(JCMethodDecl decl) {
if (decl.getBody() != null &&
!decl.getBody().getStatements().isEmpty() &&
!(decl.getStartPosition() <= focalPoint &&
decl.getStartPosition() + TreeInfo.getEndPos(decl, u.endPositions) >= focalPoint)) {
var throwNewRuntimeExceptionOutOfFocalPositionScope =
treeMaker.Throw(
treeMaker.NewClass(null, null,
treeMaker.Ident(Names.instance(context).fromString(RuntimeException.class.getSimpleName())),
com.sun.tools.javac.util.List.of(treeMaker.Literal("Out of focalPosition scope")), null)); //$NON-NLS-1$
decl.body.stats = com.sun.tools.javac.util.List.of(throwNewRuntimeExceptionOutOfFocalPositionScope);
}
}
});
}
}

0 comments on commit ef0e050

Please sign in to comment.