2 * Copyright 2006 Sascha Weinreuter
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
.intellij
.lang
.regexp
.psi
.impl
;
18 import com
.intellij
.lang
.ASTNode
;
19 import com
.intellij
.openapi
.util
.Comparing
;
20 import com
.intellij
.openapi
.util
.TextRange
;
21 import com
.intellij
.psi
.PsiElement
;
22 import com
.intellij
.psi
.PsiReference
;
23 import com
.intellij
.psi
.util
.PsiTreeUtil
;
24 import com
.intellij
.psi
.util
.PsiElementFilter
;
25 import com
.intellij
.psi
.search
.PsiElementProcessor
;
26 import com
.intellij
.util
.IncorrectOperationException
;
27 import com
.intellij
.util
.ArrayUtil
;
29 import org
.intellij
.lang
.regexp
.psi
.RegExpElementVisitor
;
30 import org
.intellij
.lang
.regexp
.psi
.RegExpElement
;
31 import org
.intellij
.lang
.regexp
.psi
.RegExpGroup
;
32 import org
.intellij
.lang
.regexp
.psi
.RegExpBackref
;
34 public class RegExpBackrefImpl
extends RegExpElementImpl
implements RegExpBackref
{
35 public RegExpBackrefImpl(ASTNode astNode
) {
39 public int getIndex() {
40 final String s
= getUnescapedText();
41 assert s
.charAt(0) == '\\';
42 return Integer
.parseInt(s
.substring(1));
45 public void accept(RegExpElementVisitor visitor
) {
46 visitor
.visitRegExpBackref(this);
49 public RegExpGroup
resolve() {
50 final int index
= getIndex();
52 final PsiElementProcessor
.FindFilteredElement
<RegExpElement
> processor
= new PsiElementProcessor
.FindFilteredElement
<RegExpElement
>(new PsiElementFilter() {
55 public boolean isAccepted(PsiElement element
) {
56 if (element
instanceof RegExpGroup
) {
57 if (((RegExpGroup
)element
).isCapturing() && ++groupCount
== index
) {
61 return element
== RegExpBackrefImpl
.this;
65 PsiTreeUtil
.processElements(getContainingFile(), processor
);
66 if (processor
.getFoundElement() instanceof RegExpGroup
) {
67 return (RegExpGroup
)processor
.getFoundElement();
72 public PsiReference
getReference() {
73 return new PsiReference() {
74 public PsiElement
getElement() {
75 return RegExpBackrefImpl
.this;
78 public TextRange
getRangeInElement() {
79 return TextRange
.from(0, getElement().getTextLength());
82 public String
getCanonicalText() {
83 return getElement().getText();
86 public PsiElement
handleElementRename(String newElementName
) throws IncorrectOperationException
{
87 throw new IncorrectOperationException();
90 public PsiElement
bindToElement(PsiElement element
) throws IncorrectOperationException
{
91 throw new IncorrectOperationException();
94 public boolean isReferenceTo(PsiElement element
) {
95 return Comparing
.equal(element
, resolve());
98 public boolean isSoft() {
102 public PsiElement
resolve() {
103 return RegExpBackrefImpl
.this.resolve();
106 public Object
[] getVariants() {
107 return ArrayUtil
.EMPTY_OBJECT_ARRAY
;