/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.plugins.groovy.lang.psi.api.statements;

import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor;

import java.util.HashSet;

/**
 * User: Dmitry.Krasilschikov
 * Date: 02.06.2009
 */
public class GrNamedArgumentSearchVisitor extends GroovyRecursiveElementVisitor {
  private final String myParamName;
  private final HashSet<String> mySet;

  public GrNamedArgumentSearchVisitor(final String paramName, final HashSet<String> set) {
    myParamName = paramName;
    mySet = set;
  }

  @Override
  public void visitReferenceExpression(GrReferenceExpression referenceExpression) {
    final GrExpression expression = referenceExpression.getQualifierExpression();
    if (!(expression instanceof GrReferenceExpression)) {
      super.visitReferenceExpression(referenceExpression);
      return;
    }

    final GrReferenceExpression qualifierExpr = (GrReferenceExpression)expression;

    if (myParamName.equals(qualifierExpr.getName())) {
      mySet.add(referenceExpression.getName());
    }

    super.visitReferenceExpression(referenceExpression);
  }
}

@@ -658,16 +658,240 @@
 ments;%0A%0A
+import com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.PsiWhiteSpace;%0Aimport org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;%0A
 import o
@@ -1138,16 +1138,199 @@
 Visitor;
+%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral;%0Aimport org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString;
 %0A%0Aimport
@@ -1800,24 +1800,196 @@
 pression) %7B%0A
+    PsiElement nextSibling = referenceExpression.getNextSibling();%0A    if (nextSibling instanceof PsiWhiteSpace) %7B%0A      nextSibling = nextSibling.getNextSibling();%0A    %7D%0A%0A
     final Gr
@@ -2062,17 +2062,16 @@
 ();%0A
+%0A
     if 
-(!
 (exp
@@ -2111,17 +2111,16 @@
 ression)
-)
  %7B%0A     
@@ -2124,38 +2124,156 @@
     
-super.visitReferenceExpression
+if (myParamName.equals(((GrReferenceExpression)expression).getName())) %7B%0A        if (!(nextSibling instanceof GrArgumentList)) %7B%0A          mySet.add
 (ref
@@ -2284,24 +2284,34 @@
 ceExpression
+.getName()
 );%0A      ret
@@ -2311,83 +2311,281 @@
     
-return;%0A
+  %7D%0A 
     
+ 
 %7D%0A
-%0A    final GrReferenceExpression qualifierExpr = (GrReference
+    %7D%0A    else if (expression == null && myParamName.equals(referenceExpression.getName())) %7B%0A      if (nextSibling instanceof GrArgumentList) %7B%0A        GrArgumentList args = (GrArgumentList)nextSibling;%0A%0A        if (args.isIndexPropertiesList()) %7B%0A          Gr
 Expr
@@ -2594,83 +2594,233 @@
 sion
-)
+%5B%5D 
 expr
-ession;%0A%0A
+ = args.getExpressionArguments();%0A%0A   
     
+   
 if (
-myParamName.equals(qualifierExpr.getName())) %7B%0A
+expr.length == 1 && expr%5B0%5D instanceof GrLiteral) %7B%0A            Object value = ((GrLiteral)expr%5B0%5D).getValue();%0A            if (value instanceof String) %7B%0A     
       
+   
 mySe
@@ -2829,39 +2829,67 @@
 add(
-referenceExpression.getName());
+(String)value);%0A            %7D%0A          %7D%0A        %7D%0A      %7D
 %0A   

Support "attr['attribute']" in GrNamedArgumentSearchVisitor.