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
.codeStyle
.CodeStyleManager
;
21 import com
.intellij
.psi
.impl
.source
.Constants
;
22 import com
.intellij
.psi
.impl
.source
.tree
.*;
23 import com
.intellij
.psi
.tree
.ChildRoleBase
;
24 import com
.intellij
.psi
.tree
.IElementType
;
25 import com
.intellij
.util
.CharTable
;
26 import com
.intellij
.util
.IncorrectOperationException
;
27 import org
.jetbrains
.annotations
.NotNull
;
29 public class ParameterListElement
extends CompositeElement
implements Constants
{
30 private static final Logger LOG
= Logger
.getInstance("#com.intellij.psi.impl.source.tree.java.ParameterListElement");
32 public ParameterListElement() {
33 super(PARAMETER_LIST
);
36 public TreeElement
addInternal(TreeElement first
, ASTNode last
, ASTNode anchor
, Boolean before
) {
38 if (before
== null || before
.booleanValue()) {
39 anchor
= findChildByRole(ChildRole
.RPARENTH
);
40 before
= Boolean
.TRUE
;
43 anchor
= findChildByRole(ChildRole
.LPARENTH
);
44 before
= Boolean
.FALSE
;
47 TreeElement firstAdded
= super.addInternal(first
, last
, anchor
, before
);
48 if (first
== last
&& first
.getElementType() == PARAMETER
) {
49 ASTNode element
= first
;
50 final CharTable treeCharTab
= SharedImplUtil
.findCharTableByTree(this);
51 for (ASTNode child
= element
.getTreeNext(); child
!= null; child
= child
.getTreeNext()) {
52 if (child
.getElementType() == COMMA
) break;
53 if (child
.getElementType() == PARAMETER
) {
54 TreeElement comma
= Factory
.createSingleLeafElement(COMMA
, ",", 0, 1, treeCharTab
, getManager());
55 super.addInternal(comma
, comma
, element
, Boolean
.FALSE
);
59 for (ASTNode child
= element
.getTreePrev(); child
!= null; child
= child
.getTreePrev()) {
60 if (child
.getElementType() == COMMA
) break;
61 if (child
.getElementType() == PARAMETER
) {
62 TreeElement comma
= Factory
.createSingleLeafElement(COMMA
, ",", 0, 1, treeCharTab
, getManager());
63 super.addInternal(comma
, comma
, child
, Boolean
.FALSE
);
71 CodeStyleManager
.getInstance(getManager().getProject()).reformat(getPsi());
73 catch (IncorrectOperationException e
) {
79 public void deleteChildInternal(@NotNull ASTNode child
) {
80 if (child
.getElementType() == PARAMETER
) {
81 ASTNode next
= TreeUtil
.skipElements(child
.getTreeNext(), StdTokenSets
.WHITE_SPACE_OR_COMMENT_BIT_SET
);
82 if (next
!= null && next
.getElementType() == COMMA
) {
83 deleteChildInternal(next
);
86 ASTNode prev
= TreeUtil
.skipElementsBack(child
.getTreePrev(), StdTokenSets
.WHITE_SPACE_OR_COMMENT_BIT_SET
);
87 if (prev
!= null && prev
.getElementType() == COMMA
) {
88 deleteChildInternal(prev
);
92 super.deleteChildInternal(child
);
96 CodeStyleManager
.getInstance(getManager().getProject()).reformat(getPsi());
98 catch (IncorrectOperationException e
) {
103 public ASTNode
findChildByRole(int role
) {
104 LOG
.assertTrue(ChildRole
.isUnique(role
));
109 case ChildRole
.LPARENTH
:
110 if (getFirstChildNode().getElementType() == LPARENTH
) {
111 return getFirstChildNode();
117 case ChildRole
.RPARENTH
:
118 if (getLastChildNode().getElementType() == RPARENTH
) {
119 return getLastChildNode();
127 public int getChildRole(ASTNode child
) {
128 LOG
.assertTrue(child
.getTreeParent() == this);
129 IElementType i
= child
.getElementType();
130 if (i
== PARAMETER
) {
131 return ChildRole
.PARAMETER
;
133 else if (i
== COMMA
) {
134 return ChildRole
.COMMA
;
136 else if (i
== LPARENTH
) {
137 return getChildRole(child
, ChildRole
.LPARENTH
);
139 else if (i
== RPARENTH
) {
140 return getChildRole(child
, ChildRole
.RPARENTH
);
143 return ChildRoleBase
.NONE
;