diff --git a/platform/core-impl/src/com/intellij/extapi/psi/ASTDelegatePsiElement.java b/platform/core-impl/src/com/intellij/extapi/psi/ASTDelegatePsiElement.java index bedda72b6ae4..aa080677a16e 100644 --- a/platform/core-impl/src/com/intellij/extapi/psi/ASTDelegatePsiElement.java +++ b/platform/core-impl/src/com/intellij/extapi/psi/ASTDelegatePsiElement.java @@ -166,12 +166,12 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { return getNode().getElementType().getLanguage(); } - protected @Nullable T findChildByType(IElementType type) { + protected @Nullable T findChildByType(@NotNull IElementType type) { ASTNode node = getNode().findChildByType(type); return node == null ? null : (T)node.getPsi(); } - protected @Nullable T findLastChildByType(IElementType type) { + protected @Nullable T findLastChildByType(@NotNull IElementType type) { PsiElement child = getLastChild(); while (child != null) { final ASTNode node = child.getNode(); @@ -181,18 +181,16 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { return null; } - - - protected @NotNull T findNotNullChildByType(IElementType type) { + protected @NotNull T findNotNullChildByType(@NotNull IElementType type) { return notNullChild(findChildByType(type)); } - protected @Nullable T findChildByType(TokenSet type) { + protected @Nullable T findChildByType(@NotNull TokenSet type) { ASTNode node = getNode().findChildByType(type); return node == null ? null : (T)node.getPsi(); } - protected @NotNull T findNotNullChildByType(TokenSet type) { + protected @NotNull T findNotNullChildByType(@NotNull TokenSet type) { return notNullChild(findChildByType(type)); } @@ -200,16 +198,16 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { * @deprecated Use {@link #findChildByType(TokenSet)} instead */ @Deprecated - protected @Nullable PsiElement findChildByFilter(TokenSet tokenSet) { + protected @Nullable PsiElement findChildByFilter(@NotNull TokenSet tokenSet) { ASTNode[] nodes = getNode().getChildren(tokenSet); return nodes.length == 0 ? null : nodes[0].getPsi(); } - protected T @NotNull [] findChildrenByType(IElementType elementType, Class arrayClass) { + protected T @NotNull [] findChildrenByType(@NotNull IElementType elementType, @NotNull Class arrayClass) { return ContainerUtil.map2Array(SharedImplUtil.getChildrenOfType(getNode(), elementType), arrayClass, s -> (T)s.getPsi()); } - protected @Unmodifiable List findChildrenByType(@NotNull TokenSet elementType) { + protected @Unmodifiable @NotNull List findChildrenByType(@NotNull TokenSet elementType) { List result = Collections.emptyList(); ASTNode child = getNode().getFirstChildNode(); while (child != null) { @@ -225,7 +223,7 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { return result; } - protected @Unmodifiable List findChildrenByType(@NotNull IElementType elementType) { + protected @Unmodifiable @NotNull List findChildrenByType(@NotNull IElementType elementType) { List result = Collections.emptyList(); ASTNode child = getNode().getFirstChildNode(); while (child != null) { @@ -259,7 +257,7 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { return addInnerBefore(element, anchor); } - private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { + private PsiElement addInnerBefore(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); @@ -288,7 +286,7 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { CheckUtil.checkWritable(this); } - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, @Nullable ASTNode anchor, @Nullable Boolean before) { return CodeEditUtil.addChildren(getNode(), first, last, getAnchorNode(anchor, before)); } @@ -371,20 +369,18 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase { CodeEditUtil.replaceChild(getNode(), child.getNode(), newElement); } - private ASTNode getAnchorNode(final ASTNode anchor, final Boolean before) { - ASTNode anchorBefore; + private @Nullable ASTNode getAnchorNode(@Nullable ASTNode anchor, @Nullable Boolean before) { + assert (anchor == null) || (anchor != null && before != null); + if (anchor != null) { - anchorBefore = before.booleanValue() ? anchor : anchor.getTreeNext(); + return before.booleanValue() ? anchor : anchor.getTreeNext(); + } + else if (before != null && !before.booleanValue()) { + return getNode().getFirstChildNode(); } else { - if (before != null && !before.booleanValue()) { - anchorBefore = getNode().getFirstChildNode(); - } - else { - anchorBefore = null; - } + return null; } - return anchorBefore; } @Override diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/GrListOrMapImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/GrListOrMapImpl.java index fdcc50501b74..40f1096280f0 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/GrListOrMapImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/GrListOrMapImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary; import com.intellij.lang.ASTNode; @@ -47,7 +47,7 @@ public class GrListOrMapImpl extends GrExpressionImpl implements GrListOrMap, Ps } @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean before) { if (getInitializers().length == 0) { return super.addInternal(first, last, getNode().getFirstChildNode(), false); } diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArgumentListImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArgumentListImpl.java index 9517664da273..45a43646b13e 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArgumentListImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArgumentListImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.annotation; import com.intellij.lang.ASTNode; @@ -51,7 +51,7 @@ public class GrAnnotationArgumentListImpl extends GrStubElementBase } @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean before) { if (first.getElementType() == GroovyStubElementTypes.ANNOTATION_MEMBER_VALUE_PAIR && last.getElementType() == GroovyStubElementTypes.ANNOTATION_MEMBER_VALUE_PAIR) { ASTNode lparenth = getNode().getFirstChildNode(); diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArrayInitializerImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArrayInitializerImpl.java index ae6ab0c2056a..eea09b54fb5a 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArrayInitializerImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/annotation/GrAnnotationArrayInitializerImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.annotation; import com.intellij.lang.ASTNode; @@ -44,7 +44,7 @@ public class GrAnnotationArrayInitializerImpl extends GroovyPsiElementImpl imple } @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean before) { final GrAnnotationMemberValue[] initializers = getInitializers(); if (initializers.length == 0) { return super.addInternal(first, last, getNode().getFirstChildNode(), false); diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/modifiers/GrModifierListImpl.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/modifiers/GrModifierListImpl.java index 0941b1da8e25..3df706f74ea4 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/modifiers/GrModifierListImpl.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/auxiliary/modifiers/GrModifierListImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.modifiers; import com.intellij.lang.ASTNode; @@ -232,7 +232,7 @@ public final class GrModifierListImpl extends GrStubElementBase @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean before) { GrParameter[] params = getParameters(); ASTNode result = super.addInternal(first, last, anchor == null ? findChildByType(before ? T_RPAREN : T_LPAREN) : anchor, before); if (first == last && first.getPsi() instanceof GrParameter && params.length > 0) { diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/typedef/GrTypeDefinitionBodyBase.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/typedef/GrTypeDefinitionBodyBase.java index f5c9922fe164..00d362cd1f8c 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/typedef/GrTypeDefinitionBodyBase.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/typedef/GrTypeDefinitionBodyBase.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef; import com.intellij.lang.ASTNode; @@ -193,7 +193,7 @@ public abstract class GrTypeDefinitionBodyBase extends GrStubElementBase } @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean before) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean before) { appendParenthesesIfNeeded(); if (first == last && first.getPsi() instanceof PsiTypeParameter) { diff --git a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFromImportStatementImpl.java b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFromImportStatementImpl.java index 43deefbd1fee..2e238dc456a0 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFromImportStatementImpl.java +++ b/python/python-psi-impl/src/com/jetbrains/python/psi/impl/PyFromImportStatementImpl.java @@ -139,7 +139,7 @@ public class PyFromImportStatementImpl extends PyBaseElementImpl> } @Override - public ASTNode addInternal(ASTNode first, ASTNode last, ASTNode anchor, Boolean beforeB) { + public ASTNode addInternal(@NotNull ASTNode first, @NotNull ASTNode last, ASTNode anchor, Boolean beforeB) { if (!(first instanceof TreeElement)) return null; TreeElement firstAppended = null; boolean before = beforeB == null || beforeB.booleanValue();