Skip to content

Commit d8b4f12

Browse files
committed
Better support for ASTParser.setFocalPoint()
Reduce the parsed AST before resolving to get a performance boost.
1 parent 7491ef5 commit d8b4f12

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,9 @@ public Void visitClass(ClassTree node, Void p) {
711711
if (elements.hasNext() && elements.next() instanceof JCCompilationUnit u) {
712712
javacCompilationUnit = u;
713713
javacCompilationUnits.add(u);
714+
if (sourceUnits.length == 1 && focalPoint >= 0) {
715+
JavacUtils.trimUnvisibleContent(u, focalPoint, context);
716+
}
714717
} else {
715718
return Map.of();
716719
}

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747

4848
import com.sun.tools.javac.file.JavacFileManager;
4949
import com.sun.tools.javac.main.Option;
50+
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
51+
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
52+
import com.sun.tools.javac.tree.TreeInfo;
53+
import com.sun.tools.javac.tree.TreeMaker;
54+
import com.sun.tools.javac.tree.TreeScanner;
5055
import com.sun.tools.javac.util.Context;
56+
import com.sun.tools.javac.util.Names;
5157
import com.sun.tools.javac.util.Options;
5258

5359
public class JavacUtils {
@@ -409,4 +415,25 @@ public static boolean isTest(IJavaProject project, org.eclipse.jdt.internal.comp
409415
return true;
410416
}
411417
}
418+
419+
/// Removes non-relevant content (eg other method blocks) for given focal position
420+
public static void trimUnvisibleContent(JCCompilationUnit u, int focalPoint, Context context) {
421+
TreeMaker treeMaker = TreeMaker.instance(context);
422+
u.accept(new TreeScanner() {
423+
@Override
424+
public void visitMethodDef(JCMethodDecl decl) {
425+
if (decl.getBody() != null &&
426+
!decl.getBody().getStatements().isEmpty() &&
427+
!(decl.getStartPosition() <= focalPoint &&
428+
decl.getStartPosition() + TreeInfo.getEndPos(decl, u.endPositions) >= focalPoint)) {
429+
var throwNewRuntimeExceptionOutOfFocalPositionScope =
430+
treeMaker.Throw(
431+
treeMaker.NewClass(null, null,
432+
treeMaker.Ident(Names.instance(context).fromString(RuntimeException.class.getSimpleName())),
433+
com.sun.tools.javac.util.List.of(treeMaker.Literal("Out of focalPosition scope")), null)); //$NON-NLS-1$
434+
decl.body.stats = com.sun.tools.javac.util.List.of(throwNewRuntimeExceptionOutOfFocalPositionScope);
435+
}
436+
}
437+
});
438+
}
412439
}

0 commit comments

Comments
 (0)