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
;
18 import com
.intellij
.openapi
.util
.TextRange
;
19 import com
.intellij
.psi
.*;
20 import com
.intellij
.psi
.impl
.PsiImplUtil
;
21 import com
.intellij
.util
.IncorrectOperationException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.List
;
26 import org
.jetbrains
.annotations
.NotNull
;
29 * Created by IntelliJ IDEA.
33 * To change this template use Options | File Templates.
35 public class PsiLabelReference
implements PsiReference
{
36 private final PsiStatement myStatement
;
37 private PsiIdentifier myIdentifier
;
39 public PsiLabelReference(PsiStatement stat
, PsiIdentifier identifier
){
41 myIdentifier
= identifier
;
44 public PsiElement
getElement(){
48 public TextRange
getRangeInElement(){
49 final int parent
= myIdentifier
.getStartOffsetInParent();
50 return new TextRange(parent
, myIdentifier
.getTextLength() + parent
);
53 public PsiElement
resolve(){
54 final String label
= myIdentifier
.getText();
55 if(label
== null) return null;
56 PsiElement context
= myStatement
;
57 while(context
!= null){
58 if(context
instanceof PsiLabeledStatement
){
59 final PsiLabeledStatement statement
= (PsiLabeledStatement
) context
;
60 if(label
.equals(statement
.getName()))
63 context
= context
.getContext();
68 public String
getCanonicalText(){
69 return getElement().getText();
72 public PsiElement
handleElementRename(String newElementName
) throws IncorrectOperationException
{
73 myIdentifier
= (PsiIdentifier
) PsiImplUtil
.setName(myIdentifier
, newElementName
);
77 public PsiElement
bindToElement(@NotNull PsiElement element
) throws IncorrectOperationException
{
78 if(element
instanceof PsiLabeledStatement
){
79 myIdentifier
= (PsiIdentifier
) PsiImplUtil
.setName(myIdentifier
, ((PsiLabeledStatement
)element
).getName());
82 throw new IncorrectOperationException("Can't bind not to labeled statement");
85 public boolean isReferenceTo(PsiElement element
){
86 return resolve() == element
;
89 public Object
[] getVariants(){
90 final List result
= new ArrayList();
91 PsiElement context
= myStatement
;
92 while(context
!= null){
93 if(context
instanceof PsiLabeledStatement
){
96 context
= context
.getContext();
98 return result
.toArray();
101 public boolean isSoft(){