blob: bba320dee8b09ca3f262c5624305e3652d52ece2 [file] [log] [blame]
/*
* Copyright 2000-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.impl.source.tree.java;
import com.intellij.lang.ASTNode;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.impl.source.Constants;
import com.intellij.psi.impl.source.tree.*;
import com.intellij.psi.tree.ChildRoleBase;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.CharTable;
import org.jetbrains.annotations.NotNull;
public class FieldElement extends CompositeElement{
public FieldElement() {
super(JavaElementType.FIELD);
}
protected FieldElement(@NotNull IElementType type) {
super(type);
}
@Override
public int getTextOffset() {
return findChildByRole(ChildRole.NAME).getStartOffset();
}
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
if (getChildRole(child) == ChildRole.INITIALIZER){
ASTNode eq = findChildByRole(ChildRole.INITIALIZER_EQ);
if (eq != null){
deleteChildInternal(eq);
}
}
super.deleteChildInternal(child);
}
@Override
public ASTNode findChildByRole(int role){
assert (ChildRole.isUnique(role));
switch(role){
default:
return null;
case ChildRole.DOC_COMMENT:
return PsiImplUtil.findDocComment(this);
case ChildRole.MODIFIER_LIST:
return findChildByType(JavaElementType.MODIFIER_LIST);
case ChildRole.TYPE:
return findChildByType(JavaElementType.TYPE);
case ChildRole.NAME:
return findChildByType(JavaTokenType.IDENTIFIER);
case ChildRole.INITIALIZER_EQ:
return findChildByType(JavaTokenType.EQ);
case ChildRole.INITIALIZER:
return findChildByType(ElementType.EXPRESSION_BIT_SET);
case ChildRole.CLOSING_SEMICOLON:
return TreeUtil.findChildBackward(this, JavaTokenType.SEMICOLON);
}
}
@Override
public int getChildRole(ASTNode child) {
assert (child.getTreeParent() == this);
IElementType i = child.getElementType();
if (i == JavaDocElementType.DOC_COMMENT) {
return getChildRole(child, ChildRole.DOC_COMMENT);
}
else if (i == JavaTokenType.C_STYLE_COMMENT || i == JavaTokenType.END_OF_LINE_COMMENT) {
return ChildRoleBase.NONE;
}
else if (i == JavaElementType.MODIFIER_LIST) {
return ChildRole.MODIFIER_LIST;
}
else if (i == JavaElementType.TYPE) {
return getChildRole(child, ChildRole.TYPE);
}
else if (i == JavaTokenType.IDENTIFIER) {
return getChildRole(child, ChildRole.NAME);
}
else if (i == JavaTokenType.EQ) {
return getChildRole(child, ChildRole.INITIALIZER_EQ);
}
else if (i == JavaTokenType.SEMICOLON) {
return getChildRole(child, ChildRole.CLOSING_SEMICOLON);
}
else {
if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) {
return ChildRole.INITIALIZER;
}
return ChildRoleBase.NONE;
}
}
@Override
public ASTNode copyElement() {
final CharTable table = SharedImplUtil.findCharTableByTree(this);
final PsiClass psiClass = ((PsiField)getPsi()).getContainingClass();
return psiClass != null ? ChangeUtil.copyElement(this, psiClass.getTypeParameterList(), table) : super.copyElement();
}
@Override
protected boolean isVisibilitySupported() {
return true;
}
}