From ef0e0504f3a3aa3b576d8d890e8daeb906685d40 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Mon, 4 Nov 2024 16:01:14 +0100 Subject: [PATCH] Better support for ASTParser.setFocalPoint() Reduce the parsed AST before resolving to get a performance boost. --- .../dom/JavacCompilationUnitResolver.java | 3 +++ .../jdt/internal/javac/JavacUtils.java | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java index 8f55f30dfef..478df74b8c3 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacCompilationUnitResolver.java @@ -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(); } diff --git a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java index 33e376c43e3..ec1144e9f91 100644 --- a/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java +++ b/org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/javac/JavacUtils.java @@ -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 { @@ -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); + } + } + }); + } }