2 * Copyright 2000-2014 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 org.jetbrains.plugins.groovy.lang.psi.impl.synthetic;
18 import com.intellij.psi.*;
19 import com.intellij.psi.impl.source.tree.LeafElement;
20 import com.intellij.psi.search.LocalSearchScope;
21 import com.intellij.psi.search.SearchScope;
22 import com.intellij.psi.xml.XmlAttributeValue;
23 import com.intellij.psi.xml.XmlToken;
24 import com.intellij.util.IncorrectOperationException;
25 import org.jetbrains.annotations.NonNls;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel;
28 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
29 import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils;
31 import java.util.Collections;
32 import java.util.List;
35 * @author sergey.evdokimov
37 public class GrLightVariable extends GrImplicitVariableImpl implements NavigatablePsiElement {
39 private final List<PsiElement> myDeclarations;
41 private Object myCreatorKey;
43 public GrLightVariable(PsiManager manager,
45 @NonNls @NotNull String type,
46 @NotNull PsiElement navigationElement) {
47 this(manager, name, type, Collections.singletonList(navigationElement), getDeclarationScope(navigationElement));
50 public GrLightVariable(PsiManager manager,
52 @NonNls @NotNull String type,
53 @NotNull List<PsiElement> declarations,
54 @NotNull PsiElement scope) {
55 this(manager, name, JavaPsiFacade.getElementFactory(manager.getProject()).createTypeFromText(type, scope), declarations, scope);
58 public GrLightVariable(PsiManager manager,
60 @NotNull PsiType type,
61 @NotNull PsiElement navigationElement) {
62 this(manager, name, type, Collections.singletonList(navigationElement), getDeclarationScope(navigationElement));
65 public GrLightVariable(PsiManager manager,
67 @NotNull PsiType type,
68 @NotNull List<PsiElement> declarations,
69 @NotNull PsiElement scope) {
70 super(manager, new GrLightIdentifier(manager, name), type, false, scope);
72 myDeclarations = declarations;
73 if (!myDeclarations.isEmpty()) {
74 setNavigationElement(myDeclarations.get(0));
78 private static PsiElement getDeclarationScope(PsiElement navigationElement) {
79 return navigationElement.getContainingFile();
83 public boolean isWritable() {
84 return getNavigationElement() != this;
88 public PsiFile getContainingFile() {
89 if (!myDeclarations.isEmpty()) {
90 return myDeclarations.get(0).getContainingFile();
93 return getDeclarationScope().getContainingFile();
98 public boolean isValid() {
99 for (PsiElement declaration : myDeclarations) {
100 if (!declaration.isValid()) return false;
108 public SearchScope getUseScope() {
109 return new LocalSearchScope(getDeclarationScope());
113 public boolean isEquivalentTo(PsiElement another) {
114 if (another instanceof GrLightVariable) {
115 return myDeclarations.equals(((GrLightVariable)another).getDeclarations());
117 return myDeclarations.contains(another) || super.isEquivalentTo(another);
121 public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
122 for (PsiElement declaration : myDeclarations) {
123 if (declaration instanceof PsiNamedElement) {
124 if (declaration instanceof PsiMethod) {
125 name = GroovyPropertyUtils.getGetterNameNonBoolean(name);
127 ((PsiNamedElement)declaration).setName(name);
129 else if (declaration instanceof GrArgumentLabel) {
130 ((GrArgumentLabel)declaration).setName(name);
132 else if (declaration instanceof XmlAttributeValue) {
133 PsiElement leftQuote = declaration.getFirstChild();
135 if (!(leftQuote instanceof XmlToken)) continue;
137 PsiElement textToken = leftQuote.getNextSibling();
139 if (!(textToken instanceof XmlToken)) continue;
141 PsiElement rightQuote = textToken.getNextSibling();
143 if (!(rightQuote instanceof XmlToken) || rightQuote.getNextSibling() != null) continue;
145 ((LeafElement)textToken).replaceWithText(name);
147 else if (declaration instanceof GrReferenceExpression) {
148 ((GrReferenceExpression)declaration).handleElementRename(name);
152 return getNameIdentifier().replace(new GrLightIdentifier(myManager, name));
155 public Object getCreatorKey() {
159 public void setCreatorKey(Object creatorKey) {
160 myCreatorKey = creatorKey;
163 public List<PsiElement> getDeclarations() {
164 return myDeclarations;