summaryrefslogtreecommitdiff
path: root/platform/structuralsearch/source/com/intellij/structuralsearch/impl/matcher/MatchResultImpl.java
blob: 8761472d813a779c9acfa9ad91ec773d276ee98e (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.intellij.structuralsearch.impl.matcher;

import com.intellij.psi.PsiElement;
import com.intellij.structuralsearch.MatchResult;
import com.intellij.structuralsearch.plugin.util.SmartPsiPointer;
import org.jetbrains.annotations.NonNls;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class describing the match result
 */
public final class MatchResultImpl extends MatchResult {
  private String name;
  private SmartPsiPointer matchRef;
  private int start;
  private int end = -1;
  private String matchImage;
  private List<MatchResult> matches;
  private MatchResult parent;
  private boolean target;

  @NonNls public static final String DEFAULT_NAME2 = "end of context match";
  @NonNls public static final String DEFAULT_NAME = "start of context match";
  private boolean myScopeMatch;
  private boolean myMultipleMatch;
  @NonNls private static final String NULL = "null";
  private MatchResultImpl myContext;

  MatchResultImpl() {
  }

  public MatchResultImpl(String name, String image, SmartPsiPointer ref, boolean target) {
    this(name,image,ref,0,-1,target);
  }

  public MatchResultImpl(String name, String image, SmartPsiPointer ref, int start, int end,boolean target) {
    matchRef = ref;
    this.name = name;
    matchImage = image;
    this.target = target;
    this.start = start;
    this.end = end;
  }

  public String getMatchImage() {
    if (matchImage==null) {
      matchImage = NULL;
    }
    return matchImage;
  }

  public void setParent(MatchResult parent) {
    this.parent = parent;
  }

  public SmartPsiPointer getMatchRef() {
    return matchRef;
  }

  public PsiElement getMatch() {
    return matchRef.getElement();
  }

  public void setMatchRef(SmartPsiPointer matchStart) {
    matchRef = matchStart;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<MatchResult> getMatches() {
    if (matches==null) matches = new ArrayList<MatchResult>();
    return matches;
  }

  public List<MatchResult> getAllSons() {
    return getMatches();
  }

  public boolean hasSons() {
    return matches!=null;
  }

  public boolean isScopeMatch() {
    return myScopeMatch;
  }

  public boolean isMultipleMatch() {
    return myMultipleMatch;
  }

  public void clear() {
    if (matchRef != null) {
      matchRef.clear();
      matchRef = null;
    }

    if (matches != null) {
      for (final MatchResult match : matches) {
        ((MatchResultImpl)match).clear();
      }
      matches = null;
    }

    name = null;
    matchImage = null;
  }

  public void clearMatches() {
    matches = null;
  }

  public void setScopeMatch(final boolean scopeMatch) {
    myScopeMatch = scopeMatch;
  }

  public void setMultipleMatch(final boolean multipleMatch) {
    myMultipleMatch = multipleMatch;
  }

  public MatchResultImpl findSon(String name) {
    if (matches!=null) {
      // @todo this could be performance bottleneck, replace with hash lookup!
      for (final MatchResult match : matches) {
        final MatchResultImpl res = (MatchResultImpl)match;

        if (name.equals(res.getName())) {
          return res;
        }
      }
    }
    return null;
  }

  public MatchResultImpl removeSon(String typedVar) {
    if (matches == null) return null;

    // @todo this could be performance bottleneck, replace with hash lookup!
    for(Iterator<MatchResult> i=matches.iterator();i.hasNext();) {
      final MatchResultImpl res = (MatchResultImpl)i.next();
      if (typedVar.equals(res.getName())) {
        i.remove();
        return res;
      }
    }

    return null;
  }

  public void addSon(MatchResultImpl result) {
    getMatches().add(result);
  }

  public void setMatchImage(String matchImage) {
    this.matchImage = matchImage;
  }

  public boolean isTarget() {
    return target;
  }

  public void setTarget(boolean target) {
    this.target = target;
  }

  public boolean isMatchImageNull() {
    return matchImage==null;
  }

  public int getStart() {
    return start;
  }

  public void setStart(int start) {
    this.start = start;
  }

  public int getEnd() {
    return end;
  }

  public void setEnd(int end) {
    this.end = end;
  }

  public void setContext(final MatchResultImpl context) {
    myContext = context;
  }

  public MatchResultImpl getContext() {
    return myContext;
  }

  @Override
  public String toString() {
    return "MatchResultImpl{name='" + name + '\'' + ", matchImage='" + matchImage + '\'' + "}";
  }
}