summaryrefslogtreecommitdiff
path: root/plugins/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/predicates/WithinPredicate.java
blob: 3904eb397bceac842ead52559c65fa7cc181a725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.intellij.structuralsearch.impl.matcher.predicates;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.MatchResult;
import com.intellij.structuralsearch.Matcher;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.compiler.PatternCompiler;

/**
 * @author Maxim.Mossienko
 */
public class WithinPredicate extends AbstractStringBasedPredicate {
  private final MatchOptions myMatchOptions;
  private Matcher matcher;

  public WithinPredicate(String name, String within, Project project) {
    super(name, within);
    myMatchOptions = new MatchOptions();

    myMatchOptions.setLooseMatching(true);
    final String unquoted = StringUtil.stripQuotesAroundValue(within);
    if (!unquoted.equals(within)) {
      myMatchOptions.setSearchPattern(unquoted);
      PatternCompiler.transformOldPattern(myMatchOptions);
      matcher = new Matcher(project, myMatchOptions);
    } else {
      assert false;
    }
  }

  public boolean match(PsiElement node, PsiElement match, int start, int end, MatchContext context) {
    final MatchResult result = matcher.isMatchedByDownUp(match, myMatchOptions);
    return result != null;
  }
}