EA-28843 (look for type element in this variable first); @Nullable

This commit is contained in:
Roman Shevchenko
2011-09-06 14:26:00 +02:00
parent d1a100b9e7
commit 4d4b72e375
3 changed files with 20 additions and 15 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.tree.ChildRoleBase;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.ui.RowIcon;
import com.intellij.util.CharTable;
@@ -47,6 +48,7 @@ import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Arrays;
import java.util.Set;
public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLocalVariable, PsiVariableEx, Constants {
@@ -100,13 +102,16 @@ public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLoca
@NotNull
public PsiTypeElement getTypeElement() {
final CompositeElement parent = getTreeParent();
assert parent != null : this + "; [" + getText() + "]";
final ASTNode first = parent.findChildByType(LOCAL_VARIABLE);
assert first != null : this + "; [" + getText() + "]";
final ASTNode type = first.findChildByType(TYPE);
assert type != null : this + "; [" + getText() + "]";
return SourceTreeToPsiMap.treeToPsiNotNull(type);
PsiTypeElement typeElement = PsiTreeUtil.getChildOfType(this, PsiTypeElement.class);
if (typeElement != null) return typeElement;
final PsiElement parent = getParent();
assert parent != null : "no parent; " + this + "; [" + getText() + "]";
final PsiLocalVariable localVariable = PsiTreeUtil.getChildOfType(parent, PsiLocalVariable.class);
assert localVariable != null : "no local variable in " + Arrays.toString(parent.getChildren());
typeElement = PsiTreeUtil.getChildOfType(localVariable, PsiTypeElement.class);
assert typeElement != null : "no type element in " + Arrays.toString(localVariable.getChildren());
return typeElement;
}
public PsiModifierList getModifierList() {
@@ -177,13 +182,13 @@ public class PsiLocalVariableImpl extends CompositePsiElement implements PsiLoca
CompositeElement statement1 = Factory.createCompositeElement(DECLARATION_STATEMENT, charTableByTree, getManager());
statement1.addChild(variable, null);
ASTNode space = Factory.createSingleLeafElement(JavaTokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
ASTNode space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
variable.addChild(space, variable.getFirstChildNode());
variable.addChild(typeCopy, variable.getFirstChildNode());
if (modifierListCopy.getTextLength() > 0) {
space = Factory.createSingleLeafElement(JavaTokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
space = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, treeCharTab, getManager());
variable.addChild(space, variable.getFirstChildNode());
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -142,9 +142,9 @@ public interface ASTNode extends UserDataHolder {
* Adds the specified child node at the specified position in the child list.
*
* @param child the child node to add.
* @param anchorBefore the node before which the child node is inserted.
* @param anchorBefore the node before which the child node is inserted (<code>null</code> to add a child as a last node).
*/
void addChild(@NotNull ASTNode child, ASTNode anchorBefore);
void addChild(@NotNull ASTNode child, @Nullable ASTNode anchorBefore);
/**
* Add leaf element with specified type and text in the child list.
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -612,7 +612,7 @@ public class CompositeElement extends TreeElement {
this.lastChild = lastChild;
}
public void addChild(@NotNull ASTNode child, final ASTNode anchorBefore) {
public void addChild(@NotNull ASTNode child, @Nullable final ASTNode anchorBefore) {
LOG.assertTrue(anchorBefore == null || ((TreeElement)anchorBefore).getTreeParent() == this, "anchorBefore == null || anchorBefore.getTreeParent() == parent");
TreeUtil.ensureParsed(getFirstChildNode());
TreeUtil.ensureParsed(child);