[apex] AbstractApexNode - calculateTextRegion for many nodes as union

This commit is contained in:
Andreas Dangel
2024-02-12 18:58:28 +01:00
parent e8e19a294f
commit 0a557dfcf8
3 changed files with 58 additions and 16 deletions

View File

@ -7,8 +7,13 @@ package net.sourceforge.pmd.lang.apex.ast;
import java.util.Arrays;
import java.util.Optional;
import net.sourceforge.pmd.lang.document.TextDocument;
import net.sourceforge.pmd.lang.document.TextPos2d;
import net.sourceforge.pmd.lang.document.TextRegion;
import com.google.summit.ast.Identifier;
import com.google.summit.ast.Node;
import com.google.summit.ast.SourceLocation;
import com.google.summit.ast.TypeRef;
import com.google.summit.ast.declaration.EnumDeclaration;
import com.google.summit.ast.expression.Expression;
@ -91,4 +96,25 @@ public final class ASTField extends AbstractApexNode.Many<Node> {
}
return allHaveRealLoc;
}
@Override
protected void calculateTextRegion(TextDocument sourceCode) {
if (nodes.get(0) instanceof TypeRef) {
super.calculateTextRegion(sourceCode);
}
// only special case this time: enum field. Then we need to ignore the first child node,
// which represents the complete enum declaration. So we need to only look at the identifier,
// if we have a location for that
SourceLocation loc = name.getSourceLocation();
if (loc.isUnknown()) {
return;
}
// Column+1 because Summit columns are 0-based and PMD are 1-based
setRegion(TextRegion.fromBothOffsets(
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(loc.getStartLine(), loc.getStartColumn() + 1)),
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(loc.getEndLine(), loc.getEndColumn() + 1))
));
}
}

View File

@ -69,22 +69,38 @@ abstract class AbstractApexNode extends AbstractNode<AbstractApexNode, ApexNode<
@Override
protected void calculateTextRegion(TextDocument sourceCode) {
// TODO: compute union of ranges?
// from all nodes, use the earliest location and the latest location.
// this assumes, that these nodes form a contiguous code snippet.
SourceLocation union = SourceLocation.Companion.getUNKNOWN();
for (Node node : nodes) {
SourceLocation loc = node.getSourceLocation();
if (!loc.isUnknown()) {
// Column+1 because Summit columns are 0-based and PMD are 1-based
setRegion(TextRegion.fromBothOffsets(
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(loc.getStartLine(), loc.getStartColumn() + 1)),
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(loc.getEndLine(), loc.getEndColumn() + 1))
));
if (union.getStartLine() == null
|| loc.getStartLine() < union.getStartLine()
|| loc.getStartLine().equals(union.getStartLine()) && loc.getStartColumn() < union.getStartColumn()) {
union = new SourceLocation(loc.getStartLine(), loc.getStartColumn(), union.getEndLine(), union.getEndColumn());
}
if (union.getEndLine() == null
|| loc.getEndLine() > union.getEndLine()
|| loc.getEndLine().equals(union.getEndLine()) && loc.getEndColumn() > union.getEndColumn()) {
union = new SourceLocation(union.getStartLine(), union.getStartColumn(), loc.getEndLine(), loc.getEndColumn());
}
}
}
if (!union.isUnknown()) {
// Column+1 because Summit columns are 0-based and PMD are 1-based
setRegion(TextRegion.fromBothOffsets(
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(union.getStartLine(), union.getStartColumn() + 1)),
sourceCode.offsetAtLineColumn(TextPos2d.pos2d(union.getEndLine(), union.getEndColumn() + 1))
));
}
}
@Override
public boolean hasRealLoc() {
return nodes.stream().noneMatch(n -> n.getSourceLocation().isUnknown());
return !nodes.isEmpty() && nodes.stream().noneMatch(n -> n.getSourceLocation().isUnknown());
}
}

View File

@ -2,11 +2,11 @@
+- UserClass[@DefiningType = "c__Foo", @Image = "Foo", @Namespace = null, @RealLoc = true, @SimpleName = "Foo", @SuperClassName = ""]
+- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 1, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = true, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
+- Field[@DefiningType = "c__Foo", @Image = "x", @Name = "x", @Namespace = null, @RealLoc = true, @Type = "Integer", @Value = null]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
+- Field[@DefiningType = "c__Foo", @Image = "profileUrl", @Name = "profileUrl", @Namespace = null, @RealLoc = true, @Type = "String", @Value = null]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
+- FieldDeclarationStatements[@DefiningType = "c__Foo", @Namespace = null, @RealLoc = true, @TypeName = "Integer"]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- FieldDeclaration[@DefiningType = "c__Foo", @Image = "x", @Name = "x", @Namespace = null, @RealLoc = true]
| +- VariableExpression[@DefiningType = "c__Foo", @Image = "anIntegerField", @Namespace = null, @RealLoc = true]
| | +- ReferenceExpression[@DefiningType = "c__Foo", @Image = "", @Namespace = null, @RealLoc = false, @ReferenceType = ReferenceType.LOAD, @SObjectType = false, @SafeNav = true]
@ -15,7 +15,7 @@
| +- VariableExpression[@DefiningType = "c__Foo", @Image = "x", @Namespace = null, @RealLoc = true]
| +- EmptyReferenceExpression[@DefiningType = null, @Namespace = null, @RealLoc = false]
+- FieldDeclarationStatements[@DefiningType = "c__Foo", @Namespace = null, @RealLoc = true, @TypeName = "String"]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- FieldDeclaration[@DefiningType = "c__Foo", @Image = "profileUrl", @Name = "profileUrl", @Namespace = null, @RealLoc = true]
| +- MethodCallExpression[@DefiningType = "c__Foo", @FullMethodName = "toExternalForm", @InputParametersSize = 0, @MethodName = "toExternalForm", @Namespace = null, @RealLoc = true]
| | +- ReferenceExpression[@DefiningType = "c__Foo", @Image = "", @Namespace = null, @RealLoc = false, @ReferenceType = ReferenceType.METHOD, @SObjectType = false, @SafeNav = true]
@ -26,7 +26,7 @@
+- Method[@Arity = 1, @CanonicalName = "bar1", @Constructor = false, @DefiningType = "c__Foo", @Image = "bar1", @Namespace = null, @RealLoc = true, @ReturnType = "void", @StaticInitializer = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 1, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = true, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- Parameter[@DefiningType = "c__Foo", @Image = "a", @Namespace = null, @RealLoc = true, @Type = "Object"]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- BlockStatement[@CurlyBrace = true, @DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
| +- ExpressionStatement[@DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
| | +- VariableExpression[@DefiningType = "c__Foo", @Image = "b", @Namespace = null, @RealLoc = true]
@ -44,9 +44,9 @@
+- Method[@Arity = 2, @CanonicalName = "bar2", @Constructor = false, @DefiningType = "c__Foo", @Image = "bar2", @Namespace = null, @RealLoc = true, @ReturnType = "void", @StaticInitializer = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 1, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = true, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- Parameter[@DefiningType = "c__Foo", @Image = "a", @Namespace = null, @RealLoc = true, @Type = "Object[]"]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- Parameter[@DefiningType = "c__Foo", @Image = "x", @Namespace = null, @RealLoc = true, @Type = "int"]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| | +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- BlockStatement[@CurlyBrace = true, @DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
| +- ExpressionStatement[@DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
| | +- VariableExpression[@DefiningType = "c__Foo", @Image = "aField", @Namespace = null, @RealLoc = true]
@ -71,10 +71,10 @@
+- Method[@Arity = 1, @CanonicalName = "getName", @Constructor = false, @DefiningType = "c__Foo", @Image = "getName", @Namespace = null, @RealLoc = true, @ReturnType = "String", @StaticInitializer = false]
+- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 1, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = true, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
+- Parameter[@DefiningType = "c__Foo", @Image = "accId", @Namespace = null, @RealLoc = true, @Type = "int"]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
+- BlockStatement[@CurlyBrace = true, @DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
+- VariableDeclarationStatements[@DefiningType = "c__Foo", @Namespace = null, @RealLoc = true]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = true, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- ModifierNode[@Abstract = false, @DefiningType = "c__Foo", @DeprecatedTestMethod = false, @Final = false, @Global = false, @InheritedSharing = false, @Modifiers = 0, @Namespace = null, @Override = false, @Private = false, @Protected = false, @Public = false, @RealLoc = false, @Static = false, @Test = false, @TestOrTestSetup = false, @Transient = false, @Virtual = false, @WebService = false, @WithSharing = false, @WithoutSharing = false]
| +- VariableDeclaration[@DefiningType = "c__Foo", @Image = "s", @Namespace = null, @RealLoc = true, @Type = "String"]
| +- VariableExpression[@DefiningType = "c__Foo", @Image = "BillingCity", @Namespace = null, @RealLoc = true]
| | +- ReferenceExpression[@DefiningType = "c__Foo", @Image = "", @Namespace = null, @RealLoc = false, @ReferenceType = ReferenceType.LOAD, @SObjectType = false, @SafeNav = true]