summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/handlers/LiteralWithSubstitutionHandler.java
blob: 78967106755f87bda3512f86af90e97671f44892 (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
39
40
41
42
43
44
45
46
47
48
49
package com.intellij.structuralsearch.impl.matcher.handlers;

import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.predicates.RegExpPredicate;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Maxim.Mossienko
 * Date: Jun 30, 2004
 * Time: 5:07:33 PM
 * To change this template use File | Settings | File Templates.
 */
public class LiteralWithSubstitutionHandler extends MatchingHandler {
  private final String matchExpression;
  private Matcher matcher;
  private final List<SubstitutionHandler> handlers;

  public LiteralWithSubstitutionHandler(String _matchedExpression,List<SubstitutionHandler> _handlers) {
    matchExpression = _matchedExpression;
    handlers = _handlers;
  }

  public boolean match(PsiElement patternNode, PsiElement matchedNode, MatchContext context) {
    final String text = RegExpPredicate.getMeaningfulText(matchedNode);
    int offset = matchedNode.getText().indexOf(text);
    if (matcher==null) {
      matcher = Pattern.compile(matchExpression).matcher(text);
    } else {
      matcher.reset(text);
    }

    while (matcher.find()) {
      for (int i = 0; i < handlers.size(); ++i) {
        SubstitutionHandler handler = handlers.get(i);

        if (!handler.handle(matchedNode,offset + matcher.start(i+1), offset + matcher.end(i+1),context)) {
          return false;
        }
      }
      return true;
    }
    return false;
  }
}