package com.intellij.structuralsearch.impl.matcher.predicates;

import com.intellij.psi.*;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.MatchUtils;

/**
 * Handles finding method
 */
public final class ReferencePredicate extends SubstitutionHandler {
  public ReferencePredicate(String _name) {
    super(_name, true, 1, 1, true);
  }

  public boolean match(PsiElement node, PsiElement match, MatchContext context) {
    match = StructuralSearchUtil.getParentIfIdentifier(match);

    PsiElement result = MatchUtils.getReferencedElement(match);
    if (result == null) {
      result = match;
      //return false;
    }

    return handle(result,context);
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2017 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -682,17 +682,88 @@
 lij.psi.
-*
+PsiElement;%0Aimport com.intellij.structuralsearch.StructuralSearchProfile
 ;%0Aimport
@@ -869,36 +869,20 @@
 her.
-handlers.SubstitutionHandler
+MatchContext
 ;%0Aim
@@ -926,39 +926,42 @@
 pl.matcher.Match
-Context
+ResultImpl
 ;%0Aimport com.int
@@ -1012,110 +1012,245 @@
 ls;%0A
-%0A/**%0A * Handles finding method%0A */%0Apublic final class ReferencePredicate extends SubstitutionHandler %7B
+import com.intellij.structuralsearch.impl.matcher.handlers.MatchPredicate;%0Aimport com.intellij.structuralsearch.plugin.util.SmartPsiPointer;%0A%0Apublic final class ReferencePredicate extends MatchPredicate %7B%0A%0A  private final String myName;%0A
 %0A  p
@@ -1281,17 +1281,16 @@
 (String 
-_
 name) %7B%0A
@@ -1297,44 +1297,39 @@
     
-super(_name, true, 1, 1, true);%0A  %7D%0A
+myName = name;%0A  %7D%0A%0A  @Override
 %0A  p
@@ -1475,16 +1475,22 @@
 );%0A%0A    
+final 
 PsiEleme
@@ -1492,21 +1492,21 @@
 Element 
-resul
+targe
 t = Matc
@@ -1549,24 +1549,24 @@
     if (
-result =
+target !
 = null) 
@@ -1577,86 +1577,316 @@
     
-result = match;%0A      //return false;%0A    %7D%0A%0A    return handle(result,context)
+final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(target);%0A      assert profile != null;%0A      final String image = profile.getText(target, 0, -1);%0A      context.getResult().addSon(new MatchResultImpl(myName, image, new SmartPsiPointer(target), true));%0A    %7D%0A    return true
 ;%0A  

make reference predicate independent from substitution handler