2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.psi
.impl
.source
.tree
.java
;
18 import com
.intellij
.lang
.ASTNode
;
19 import com
.intellij
.openapi
.diagnostic
.Logger
;
20 import com
.intellij
.psi
.JavaTokenType
;
21 import com
.intellij
.psi
.TokenType
;
22 import com
.intellij
.psi
.impl
.PsiImplUtil
;
23 import com
.intellij
.psi
.impl
.source
.Constants
;
24 import com
.intellij
.psi
.impl
.source
.tree
.*;
25 import com
.intellij
.psi
.tree
.ChildRoleBase
;
26 import com
.intellij
.psi
.tree
.IElementType
;
27 import com
.intellij
.util
.CharTable
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 public class MethodElement
extends CompositeElement
implements Constants
{
31 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.MethodElement");
33 public MethodElement() {
37 protected MethodElement(IElementType type
) {
41 public int getTextOffset() {
42 return findChildByRole(ChildRole
.NAME
).getStartOffset();
45 public TreeElement
addInternal(TreeElement first
, ASTNode last
, ASTNode anchor
, Boolean before
) {
46 if (first
== last
&& first
.getElementType() == ElementType
.CODE_BLOCK
){
47 ASTNode semicolon
= findChildByRole(ChildRole
.CLOSING_SEMICOLON
);
48 if (semicolon
!= null){
49 deleteChildInternal(semicolon
);
52 return super.addInternal(first
, last
, anchor
, before
);
55 public void deleteChildInternal(@NotNull ASTNode child
) {
56 if (child
.getElementType() == CODE_BLOCK
){
57 final ASTNode prevWS
= TreeUtil
.prevLeaf(child
);
58 if (prevWS
!= null && prevWS
.getElementType() == TokenType
.WHITE_SPACE
) {
61 super.deleteChildInternal(child
);
62 final CharTable treeCharTab
= SharedImplUtil
.findCharTableByTree(this);
63 LeafElement semicolon
= Factory
.createSingleLeafElement(SEMICOLON
, ";", 0, 1, treeCharTab
, getManager());
64 addInternal(semicolon
, semicolon
, null, Boolean
.TRUE
);
67 super.deleteChildInternal(child
);
71 public ASTNode
findChildByRole(int role
){
72 LOG
.assertTrue(ChildRole
.isUnique(role
));
77 case ChildRole
.DOC_COMMENT
:
78 return PsiImplUtil
.findDocComment(this);
80 case ChildRole
.MODIFIER_LIST
:
81 return findChildByType(MODIFIER_LIST
);
83 case ChildRole
.TYPE_PARAMETER_LIST
:
84 return findChildByType(TYPE_PARAMETER_LIST
);
87 return findChildByType(IDENTIFIER
);
90 return findChildByType(TYPE
);
92 case ChildRole
.METHOD_BODY
:
93 return findChildByType(CODE_BLOCK
);
95 case ChildRole
.PARAMETER_LIST
:
96 return findChildByType(PARAMETER_LIST
);
98 case ChildRole
.THROWS_LIST
:
99 return findChildByType(THROWS_LIST
);
101 case ChildRole
.CLOSING_SEMICOLON
:
102 return TreeUtil
.findChildBackward(this, SEMICOLON
);
106 public int getChildRole(ASTNode child
) {
107 LOG
.assertTrue(child
.getTreeParent() == this);
108 IElementType i
= child
.getElementType();
109 if (i
== JavaTokenType
.DOC_COMMENT
|| i
== JavaDocElementType
.DOC_COMMENT
) {
110 return getChildRole(child
, ChildRole
.DOC_COMMENT
);
112 else if (i
== C_STYLE_COMMENT
|| i
== END_OF_LINE_COMMENT
) {
114 return ChildRoleBase
.NONE
;
117 else if (i
== MODIFIER_LIST
) {
118 return ChildRole
.MODIFIER_LIST
;
120 else if (i
== TYPE_PARAMETER_LIST
) {
121 return ChildRole
.TYPE_PARAMETER_LIST
;
123 else if (i
== CODE_BLOCK
) {
124 return ChildRole
.METHOD_BODY
;
126 else if (i
== PARAMETER_LIST
) {
127 return ChildRole
.PARAMETER_LIST
;
129 else if (i
== THROWS_LIST
) {
130 return ChildRole
.THROWS_LIST
;
132 else if (i
== TYPE
) {
133 return getChildRole(child
, ChildRole
.TYPE
);
135 else if (i
== IDENTIFIER
) {
136 return getChildRole(child
, ChildRole
.NAME
);
138 else if (i
== SEMICOLON
) {
139 return getChildRole(child
, ChildRole
.CLOSING_SEMICOLON
);
142 return ChildRoleBase
.NONE
;