This is an automated email from the ASF dual-hosted git repository. junichi11 pushed a commit to branch php-nb21-features in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/php-nb21-features by this push: new de1f2ed5d5 Improve the performance for Go to Declaration new 0b3757be9b Merge pull request #6712 from junichi11/php-gh-3933-6634-improve-gotodecl de1f2ed5d5 is described below commit de1f2ed5d55c0db545d905ea98064585566726b6 Author: Junichi Yamamoto <junich...@apache.org> AuthorDate: Fri Nov 17 12:07:07 2023 +0900 Improve the performance for Go to Declaration - https://github.com/apache/netbeans/issues/3933 - https://github.com/apache/netbeans/issues/6634 - Don't add empty type names to avoid getting types from an index using them --- .../org/netbeans/modules/php/editor/CodeUtils.java | 1 + .../php/editor/elements/ClassElementImpl.java | 48 ++++++++++++++++++---- .../php/editor/elements/IndexQueryImpl.java | 6 ++- .../php/editor/elements/TraitElementImpl.java | 15 ++++--- .../php/editor/model/impl/ClassScopeImpl.java | 22 +++++++--- .../php/editor/model/impl/OccurenceBuilder.java | 6 ++- .../modules/php/editor/parser/api/Utils.java | 17 +++++--- 7 files changed, 91 insertions(+), 24 deletions(-) diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java b/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java index 6d39d06511..cf6488e7e3 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/CodeUtils.java @@ -93,6 +93,7 @@ public final class CodeUtils { public static final Pattern WHITE_SPACES_PATTERN = Pattern.compile("\\s+"); // NOI18N public static final Pattern SPLIT_TYPES_PATTERN = Pattern.compile("[()|&]+"); // NOI18N public static final Pattern TYPE_NAMES_IN_TYPE_DECLARATION_PATTERN = Pattern.compile("[^?()|&]+"); // NOI18N + public static final Pattern COMMA_PATTERN = Pattern.compile(","); // NOI18N private static final Logger LOGGER = Logger.getLogger(CodeUtils.class.getName()); diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/elements/ClassElementImpl.java b/php/php.editor/src/org/netbeans/modules/php/editor/elements/ClassElementImpl.java index f2ea7fcf98..cc76f0c792 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/elements/ClassElementImpl.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/elements/ClassElementImpl.java @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; import org.netbeans.api.annotations.common.CheckForNull; +import org.netbeans.api.annotations.common.NullAllowed; import org.netbeans.modules.parsing.spi.indexing.support.IndexResult; import org.netbeans.modules.php.api.editor.PhpClass; import org.netbeans.modules.php.editor.CodeUtils; @@ -47,8 +48,10 @@ import org.openide.util.Parameters; * @author Radek Matous */ public final class ClassElementImpl extends TypeElementImpl implements ClassElement { + public static final String IDX_FIELD = PHPIndexer.FIELD_CLASS; + @NullAllowed private final QualifiedName superClass; private final Collection<QualifiedName> possibleFQSuperClassNames; private final Collection<QualifiedName> fqMixinClassNames; @@ -72,6 +75,28 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem this.possibleFQSuperClassNames = possibleFQSuperClassNames; this.usedTraits = usedTraits; this.fqMixinClassNames = fqMixinClassNames; + checkTypeNames(); + } + + private void checkTypeNames() { + // GH-6634 + // avoid getting types from the index with an empty string + boolean checkEnabled = false; + assert checkEnabled = true; + if (checkEnabled) { + if (superClass != null) { + assert !superClass.getName().isEmpty(); + } + for (QualifiedName name : possibleFQSuperClassNames) { + assert !name.getName().isEmpty(); + } + for (QualifiedName usedTrait : usedTraits) { + assert !usedTrait.getName().isEmpty(); + } + for (QualifiedName className : fqMixinClassNames) { + assert !className.getName().isEmpty(); + } + } } public static Set<ClassElement> fromSignature(final IndexQueryImpl indexScopeQuery, final IndexResult indexResult) { @@ -81,7 +106,7 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem public static Set<ClassElement> fromSignature(final NameKind query, final IndexQueryImpl indexScopeQuery, final IndexResult indexResult) { String[] values = indexResult.getValues(IDX_FIELD); - Set<ClassElement> retval = values.length > 0 ? new HashSet<ClassElement>() : Collections.<ClassElement>emptySet(); + Set<ClassElement> retval = values.length > 0 ? new HashSet<>() : Collections.<ClassElement>emptySet(); for (String val : values) { final ClassElement clz = fromSignature(query, indexScopeQuery, Signature.get(val)); if (clz != null) { @@ -142,6 +167,7 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem return KIND; } + @CheckForNull @Override public QualifiedName getSuperClassName() { return superClass; @@ -149,7 +175,7 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem @Override public Collection<QualifiedName> getPossibleFQSuperClassNames() { - return this.possibleFQSuperClassNames; + return Collections.unmodifiableCollection(possibleFQSuperClassNames); } @Override @@ -293,7 +319,7 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem @Override public Collection<QualifiedName> getUsedTraits() { - return usedTraits; + return Collections.unmodifiableCollection(usedTraits); } private static class ClassSignatureParser { @@ -382,9 +408,13 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem public Collection<QualifiedName> getUsedTraits() { Collection<QualifiedName> retval = new HashSet<>(); String traits = signature.string(7); - final String[] traitNames = traits.split(Separator.COMMA.toString()); + final String[] traitNames = CodeUtils.COMMA_PATTERN.split(traits); for (String trait : traitNames) { - retval.add(QualifiedName.create(trait)); + if (!trait.isEmpty()) { + // GH-6634 + // avoid getting traits from the index with an empty string + retval.add(QualifiedName.create(trait)); + } } return retval; } @@ -400,9 +430,13 @@ public final class ClassElementImpl extends TypeElementImpl implements ClassElem public Collection<QualifiedName> getFQMixinClassNames() { Collection<QualifiedName> retval = new HashSet<>(); String mixins = signature.string(10); - final String[] mixinNames = mixins.split(Separator.COMMA.toString()); + final String[] mixinNames = CodeUtils.COMMA_PATTERN.split(mixins); for (String mixinName : mixinNames) { - retval.add(QualifiedName.create(mixinName)); + if (!mixinName.isEmpty()) { + // GH-6634 + // avoid getting mixins from the index with an empty string + retval.add(QualifiedName.create(mixinName)); + } } return retval; } diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java b/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java index ce32a25622..961dc19c63 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/elements/IndexQueryImpl.java @@ -42,6 +42,7 @@ import org.netbeans.modules.php.api.editor.PhpBaseElement; import org.netbeans.modules.php.api.editor.PhpClass; import org.netbeans.modules.php.api.editor.PhpType; import org.netbeans.modules.php.api.editor.PhpVariable; +import org.netbeans.modules.php.api.util.StringUtils; import org.netbeans.modules.php.editor.api.AbstractElementQuery; import org.netbeans.modules.php.editor.api.AliasedName; import org.netbeans.modules.php.editor.api.ElementQuery; @@ -1254,6 +1255,9 @@ public final class IndexQueryImpl implements ElementQuery.Index { TraitedElement traitedElement = (TraitedElement) typeElement; Collection<QualifiedName> usedTraits = traitedElement.getUsedTraits(); for (QualifiedName trait : usedTraits) { + if (StringUtils.isEmpty(trait.getName())) { + continue; + } final Set<TypeMemberElement> traitTypes = new LinkedHashSet<>(); if (memberKinds.size() != 1) { traitTypes.addAll(ElementFilter.forFiles(typeElement.getFileObject()).prefer(getTypeMembers(NameKind.exact(trait), NameKind.empty()))); @@ -1298,7 +1302,7 @@ public final class IndexQueryImpl implements ElementQuery.Index { private Set<TypeMemberElement> getDirectInheritedClassTypes(QualifiedName superClassName, EnumSet<PhpElementKind> memberKinds, final TypeElement typeElement) { final Set<TypeMemberElement> classTypes = new LinkedHashSet<>(); - if (superClassName != null) { + if (superClassName != null && !StringUtils.isEmpty(superClassName.getName())) { classTypes.addAll(extendedQuery.getFields(NameKind.exact(superClassName), NameKind.empty())); classTypes.addAll(extendedQuery.getMethods(NameKind.exact(superClassName), NameKind.empty())); classTypes.addAll(extendedQuery.getTypeConstants(NameKind.exact(superClassName), NameKind.empty())); diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/elements/TraitElementImpl.java b/php/php.editor/src/org/netbeans/modules/php/editor/elements/TraitElementImpl.java index 41e918fb77..aa42a1e24e 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/elements/TraitElementImpl.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/elements/TraitElementImpl.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.netbeans.modules.parsing.spi.indexing.support.IndexResult; +import org.netbeans.modules.php.editor.CodeUtils; import org.netbeans.modules.php.editor.api.ElementQuery; import org.netbeans.modules.php.editor.api.NameKind; import org.netbeans.modules.php.editor.api.PhpElementKind; @@ -43,7 +44,7 @@ import org.openide.util.Parameters; */ public final class TraitElementImpl extends TypeElementImpl implements TraitElement { public static final String IDX_FIELD = PHPIndexer.FIELD_TRAIT; - private Collection<QualifiedName> usedTraits; + private final Collection<QualifiedName> usedTraits; private TraitElementImpl( final QualifiedName qualifiedName, @@ -86,7 +87,7 @@ public final class TraitElementImpl extends TypeElementImpl implements TraitElem public static Set<TraitElement> fromSignature(final NameKind query, final IndexQueryImpl indexScopeQuery, final IndexResult indexResult) { String[] values = indexResult.getValues(IDX_FIELD); - Set<TraitElement> retval = values.length > 0 ? new HashSet<TraitElement>() : Collections.<TraitElement>emptySet(); + Set<TraitElement> retval = values.length > 0 ? new HashSet<>() : Collections.<TraitElement>emptySet(); for (String val : values) { final TraitElement trait = fromSignature(query, indexScopeQuery, Signature.get(val)); if (trait != null) { @@ -161,7 +162,7 @@ public final class TraitElementImpl extends TypeElementImpl implements TraitElem @Override public Collection<QualifiedName> getUsedTraits() { - return usedTraits; + return Collections.unmodifiableCollection(usedTraits); } private static class TraitSignatureParser { @@ -183,9 +184,13 @@ public final class TraitElementImpl extends TypeElementImpl implements TraitElem private Collection<QualifiedName> getUsedTraits() { Collection<QualifiedName> retval = new HashSet<>(); String traits = signature.string(4); - final String[] traitNames = traits.split(Separator.COMMA.toString()); + final String[] traitNames = CodeUtils.COMMA_PATTERN.split(traits); for (String trait : traitNames) { - retval.add(QualifiedName.create(trait)); + if (!trait.isEmpty()) { + // GH-6634 + // avoid getting traits from the index with an empty string + retval.add(QualifiedName.create(trait)); + } } return retval; } diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java index 3ae5417edd..d5fde9f8fe 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/ClassScopeImpl.java @@ -114,7 +114,11 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa this.superClass = Union2.<String, List<ClassScopeImpl>>createFirst(null); } for (QualifiedName usedTrait : nodeInfo.getUsedTraits()) { - usedTraits.add(VariousUtils.getFullyQualifiedName(usedTrait, nodeInfo.getOriginalNode().getStartOffset(), inScope)); + if (!usedTrait.getName().isEmpty()) { + // GH-6634 + // avoid getting traits from the index with an empty string + usedTraits.add(VariousUtils.getFullyQualifiedName(usedTrait, nodeInfo.getOriginalNode().getStartOffset(), inScope)); + } } } @@ -139,7 +143,12 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa this.superClass = Union2.<String, List<ClassScopeImpl>>createFirst(null); } for (QualifiedName usedTrait : nodeInfo.getUsedTraits()) { - usedTraits.add(VariousUtils.getFullyQualifiedName(usedTrait, nodeInfo.getOriginalNode().getStartOffset(), inScope)); + QualifiedName fullyQualifiedName = VariousUtils.getFullyQualifiedName(usedTrait, nodeInfo.getOriginalNode().getStartOffset(), inScope); + if (!fullyQualifiedName.getName().isEmpty()) { + // GH-6634 + // avoid getting traits from the index with an empty string + usedTraits.add(fullyQualifiedName); + } } } @@ -493,6 +502,7 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa } else { first = true; } + assert !qualifiedName.getName().isEmpty(); sb.append(qualifiedName.toString()); } } @@ -529,6 +539,7 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa if (traitSb.length() > 0) { traitSb.append(","); //NOI18N } + assert !usedTrait.getName().isEmpty(); traitSb.append(usedTrait.toString()); } sb.append(traitSb); @@ -542,6 +553,7 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa if (mixinSb.length() > 0) { mixinSb.append(","); // NOI18N } + assert !mixinClassName.getName().isEmpty(); mixinSb.append(mixinClassName.toString()); } sb.append(mixinSb); @@ -654,7 +666,7 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa @Override public Collection<QualifiedName> getUsedTraits() { - return usedTraits; + return Collections.unmodifiableCollection(usedTraits); } @Override @@ -748,14 +760,14 @@ class ClassScopeImpl extends TypeScopeImpl implements ClassScope, VariableNameFa sb.append(" extends ").append(extClass.getName()); //NOI18N } List<? extends InterfaceScope> implementedInterfaces = getSuperInterfaceScopes(); - if (implementedInterfaces.size() > 0) { + if (!implementedInterfaces.isEmpty()) { sb.append(" implements "); //NOI18N for (InterfaceScope interfaceScope : implementedInterfaces) { sb.append(interfaceScope.getName()).append(" "); } } Collection<? extends TraitScope> traits = getTraits(); - if (traits.size() > 0) { + if (!traits.isEmpty()) { sb.append(" uses "); //NOI18N for (TraitScope traitScope : traits) { sb.append(traitScope.getName()).append(" "); diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java index 67839defc6..4f7d427a2e 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/model/impl/OccurenceBuilder.java @@ -2460,7 +2460,11 @@ class OccurenceBuilder { } ASTNodeInfo<Variable> nodeInfo = entry.getKey(); boolean addOccurence = false; - if (NameKind.exact(nodeInfo.getName()).matchesName(PhpElementKind.VARIABLE, nodeName)) { + String name = nodeInfo.getName(); + if (!StringUtils.hasText(name)) { + continue; + } + if (NameKind.exact(name).matchesName(PhpElementKind.VARIABLE, nodeName)) { if (!var.isGloballyVisible()) { Scope nextScope = entry.getValue(); if (var.representsThis() && nextScope.getInScope() instanceof TypeScope) { diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/parser/api/Utils.java b/php/php.editor/src/org/netbeans/modules/php/editor/parser/api/Utils.java index 22aa07b897..296a372adc 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/parser/api/Utils.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/parser/api/Utils.java @@ -75,12 +75,19 @@ public final class Utils { } } if (possible != null && (possible.getEndOffset() + 1 < node.getStartOffset())) { - List<ASTNode> nodes = (new NodeRangeLocator()).locate(root, new OffsetRange(possible.getEndOffset() + 1, getNodeRangeLocatorEndOffset(node))); - if (!nodes.isEmpty()) { - if (!isConstantDeclaration(nodes, node) - && !isFieldDeclaration(nodes, node)) { - possible = null; + int start = possible.getEndOffset() + 1; + int end = getNodeRangeLocatorEndOffset(node); + if (start <= end) { + List<ASTNode> nodes = (new NodeRangeLocator()).locate(root, new OffsetRange(start, end)); + if (!nodes.isEmpty()) { + if (!isConstantDeclaration(nodes, node) + && !isFieldDeclaration(nodes, node)) { + possible = null; + } } + } else { + // e.g. public self|A /* comment */ |null $unionType; (start > end) + possible = null; } } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists