@NotNull/Nullable for ASTDelegatePsiElement

GitOrigin-RevId: 48e11f869faf0565db1aeedfb8544fdb04876c61
This commit is contained in:
Max Medvedev
2024-07-23 13:27:20 +00:00
committed by intellij-monorepo-bot
parent 09d8d7b099
commit 9f930f7d18
12 changed files with 40 additions and 44 deletions
@@ -166,12 +166,12 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase {
return getNode().getElementType().getLanguage();
}
protected @Nullable <T extends PsiElement> T findChildByType(IElementType type) {
protected @Nullable <T extends PsiElement> T findChildByType(@NotNull IElementType type) {
ASTNode node = getNode().findChildByType(type);
return node == null ? null : (T)node.getPsi();
}
protected @Nullable <T extends PsiElement> T findLastChildByType(IElementType type) {
protected @Nullable <T extends PsiElement> 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 extends PsiElement> T findNotNullChildByType(IElementType type) {
protected @NotNull <T extends PsiElement> T findNotNullChildByType(@NotNull IElementType type) {
return notNullChild(findChildByType(type));
}
protected @Nullable <T extends PsiElement> T findChildByType(TokenSet type) {
protected @Nullable <T extends PsiElement> T findChildByType(@NotNull TokenSet type) {
ASTNode node = getNode().findChildByType(type);
return node == null ? null : (T)node.getPsi();
}
protected @NotNull <T extends PsiElement> T findNotNullChildByType(TokenSet type) {
protected @NotNull <T extends PsiElement> 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 extends PsiElement> T @NotNull [] findChildrenByType(IElementType elementType, Class<T> arrayClass) {
protected <T extends PsiElement> T @NotNull [] findChildrenByType(@NotNull IElementType elementType, @NotNull Class<T> arrayClass) {
return ContainerUtil.map2Array(SharedImplUtil.getChildrenOfType(getNode(), elementType), arrayClass, s -> (T)s.getPsi());
}
protected @Unmodifiable <T extends PsiElement> List<T> findChildrenByType(@NotNull TokenSet elementType) {
protected @Unmodifiable <T extends PsiElement> @NotNull List<T> findChildrenByType(@NotNull TokenSet elementType) {
List<T> result = Collections.emptyList();
ASTNode child = getNode().getFirstChildNode();
while (child != null) {
@@ -225,7 +223,7 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase {
return result;
}
protected @Unmodifiable <T extends PsiElement> List<T> findChildrenByType(@NotNull IElementType elementType) {
protected @Unmodifiable <T extends PsiElement> @NotNull List<T> findChildrenByType(@NotNull IElementType elementType) {
List<T> 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
@@ -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);
}
@@ -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<EmptyStub>
}
@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();
@@ -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);
@@ -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<GrModifierListSt
}
@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 ASTNode node = super.addInternal(first, last, anchor, before);
final PsiElement sibling = getNextSibling();
if (sibling != null && sibling.getText().contains("\n")) {
@@ -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.params;
import com.intellij.lang.ASTNode;
@@ -145,7 +145,7 @@ public class GrParameterListImpl extends GrStubElementBase<EmptyStub>
@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) {
@@ -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<EmptySt
}
@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) {
ASTNode afterLast = last.getTreeNext();
ASTNode next;
for (ASTNode child = first; child != afterLast; child = next) {
@@ -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.members;
import com.intellij.lang.ASTNode;
@@ -23,7 +23,7 @@ public class GrMethodImpl extends GrMethodBaseImpl implements GrMethod {
}
@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 == last && first.getPsi() instanceof GrTypeParameterList) {
if (!getModifierList().hasExplicitVisibilityModifiers()) {
getModifierList().setModifierProperty(GrModifier.DEF, true);
@@ -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.types;
import com.intellij.lang.ASTNode;
@@ -64,7 +64,7 @@ public class GrTypeArgumentListImpl extends GroovyPsiElementImpl implements GrTy
}
@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 == last && first.getPsi() instanceof GrTypeElement) {
if (anchor == null) {
anchor = getLastChild().getNode();
@@ -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.types;
import com.intellij.lang.ASTNode;
@@ -68,7 +68,7 @@ public class GrTypeParameterListImpl extends GrStubElementBase<EmptyStub>
}
@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) {
@@ -139,7 +139,7 @@ public class PyFromImportStatementImpl extends PyBaseElementImpl<PyFromImportSta
}
@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) {
boolean addingNewName = first == last &&
first.getElementType() == PyElementTypes.IMPORT_ELEMENT &&
(anchor == null || anchor.getElementType() == PyElementTypes.IMPORT_ELEMENT);
@@ -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 com.intellij.psi.impl.source.xml;
import com.intellij.lang.ASTNode;
@@ -293,7 +293,7 @@ public class XmlStubBasedTagBase<StubT extends XmlTagStub<?>>
}
@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();