use of com.intellij.structuralsearch.impl.matcher.MatchResultImpl in project intellij-community by JetBrains.
the class SearchCommand method findUsages.
public void findUsages(final Processor<Usage> processor) {
final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
final MatchResultSink sink = new MatchResultSink() {
int count;
public void setMatchingProcess(MatchingProcess _process) {
process = _process;
findStarted();
}
public void processFile(PsiFile element) {
final VirtualFile virtualFile = element.getVirtualFile();
if (virtualFile != null)
progress.setText(SSRBundle.message("looking.in.progress.message", virtualFile.getPresentableName()));
}
public void matchingFinished() {
if (mySearchContext.getProject().isDisposed())
return;
findEnded();
progress.setText(SSRBundle.message("found.progress.message", count));
}
public ProgressIndicator getProgressIndicator() {
return progress;
}
public void newMatch(MatchResult result) {
UsageInfo info;
if (MatchResult.MULTI_LINE_MATCH.equals(result.getName())) {
int start = -1;
int end = -1;
PsiElement parent = result.getMatch().getParent();
for (final MatchResult matchResult : ((MatchResultImpl) result).getMatches()) {
PsiElement el = matchResult.getMatch();
final int elementStart = el.getTextRange().getStartOffset();
if (start == -1 || start > elementStart) {
start = elementStart;
}
final int newend = elementStart + el.getTextLength();
if (newend > end) {
end = newend;
}
}
final int parentStart = parent.getTextRange().getStartOffset();
int startOffset = start - parentStart;
info = new UsageInfo(parent, startOffset, end - parentStart);
} else {
info = new UsageInfo(StructuralSearchUtil.getPresentableElement(result.getMatch()));
}
Usage usage = new UsageInfo2UsageAdapter(info);
processor.process(usage);
foundUsage(result, usage);
++count;
}
};
try {
new Matcher(mySearchContext.getProject()).findMatches(sink, myConfiguration.getMatchOptions());
} catch (final StructuralSearchException e) {
final Alarm alarm = new Alarm();
alarm.addRequest(() -> NotificationGroup.toolWindowGroup("Structural Search", ToolWindowId.FIND).createNotification(SSRBundle.message("problem", e.getMessage()), MessageType.ERROR).notify(mySearchContext.getProject()), 100, ModalityState.NON_MODAL);
}
}
use of com.intellij.structuralsearch.impl.matcher.MatchResultImpl in project intellij-community by JetBrains.
the class MatchingHandler method matchInAnyOrder.
public boolean matchInAnyOrder(NodeIterator patternNodes, NodeIterator matchedNodes, final MatchContext context) {
final MatchResultImpl saveResult = context.hasResult() ? context.getResult() : null;
context.setResult(null);
try {
if (patternNodes.hasNext() && !matchedNodes.hasNext()) {
return validateSatisfactionOfHandlers(patternNodes, context);
}
Set<PsiElement> matchedElements = null;
for (; patternNodes.hasNext(); patternNodes.advance()) {
final PsiElement patternNode = patternNodes.current();
final CompiledPattern pattern = context.getPattern();
final MatchingHandler handler = pattern.getHandler(patternNode);
final PsiElement startMatching = matchedNodes.current();
do {
final PsiElement element = handler.getPinnedNode(null);
final PsiElement matchedNode = element != null ? element : matchedNodes.current();
if (element == null)
matchedNodes.advance();
if (!matchedNodes.hasNext())
matchedNodes.reset();
if (matchedElements == null || !matchedElements.contains(matchedNode)) {
if (handler.match(patternNode, matchedNode, context)) {
if (matchedElements == null)
matchedElements = new HashSet<>();
matchedElements.add(matchedNode);
if (handler.shouldAdvanceThePatternFor(patternNode, matchedNode)) {
break;
}
} else if (element != null) {
return false;
}
// clear state of dependent objects
clearingVisitor.clearState(pattern, patternNode);
}
// passed of elements and does not found the match
if (startMatching == matchedNodes.current()) {
final boolean result = validateSatisfactionOfHandlers(patternNodes, context);
if (result && context.getMatchedElementsListener() != null) {
context.getMatchedElementsListener().matchedElements(matchedElements);
}
return result;
}
} while (true);
if (!handler.shouldAdvanceThePatternFor(patternNode, null)) {
patternNodes.rewind();
}
}
final boolean result = validateSatisfactionOfHandlers(patternNodes, context);
if (result && context.getMatchedElementsListener() != null) {
context.getMatchedElementsListener().matchedElements(matchedElements);
}
return result;
} finally {
if (saveResult != null) {
if (context.hasResult()) {
saveResult.getMatches().addAll(context.getResult().getMatches());
}
context.setResult(saveResult);
}
}
}
use of com.intellij.structuralsearch.impl.matcher.MatchResultImpl in project intellij-community by JetBrains.
the class SubstitutionHandler method createMatch.
private MatchResultImpl createMatch(final PsiElement match, int start, int end) {
final String image = match == null ? null : StructuralSearchUtil.getProfileByPsiElement(match).getText(match, start, end);
final SmartPsiPointer ref = new SmartPsiPointer(match);
final MatchResultImpl result = myNestedResult == null ? new MatchResultImpl(name, image, ref, start, end, target) : myNestedResult;
if (myNestedResult != null) {
myNestedResult.setName(name);
myNestedResult.setMatchImage(image);
myNestedResult.setMatchRef(ref);
myNestedResult.setStart(start);
myNestedResult.setEnd(end);
myNestedResult.setTarget(target);
myNestedResult = null;
}
return result;
}
use of com.intellij.structuralsearch.impl.matcher.MatchResultImpl in project intellij-community by JetBrains.
the class SubstitutionHandler method matchInAnyOrder.
public boolean matchInAnyOrder(NodeIterator patternNodes, NodeIterator matchedNodes, final MatchContext context) {
final MatchResultImpl saveResult = context.hasResult() ? context.getResult() : null;
context.setResult(null);
try {
if (patternNodes.hasNext() && !matchedNodes.hasNext()) {
return validateSatisfactionOfHandlers(patternNodes, context);
}
Set<PsiElement> matchedElements = null;
for (; patternNodes.hasNext(); patternNodes.advance()) {
int matchedOccurs = 0;
final PsiElement patternNode = patternNodes.current();
final CompiledPattern pattern = context.getPattern();
final MatchingHandler handler = pattern.getHandler(patternNode);
final PsiElement startMatching = matchedNodes.current();
do {
final PsiElement element = handler.getPinnedNode(null);
final PsiElement matchedNode = (element != null) ? element : matchedNodes.current();
if (element == null)
matchedNodes.advance();
if (!matchedNodes.hasNext())
matchedNodes.reset();
if (matchedOccurs <= maxOccurs && (matchedElements == null || !matchedElements.contains(matchedNode))) {
if (handler.match(patternNode, matchedNode, context)) {
++matchedOccurs;
if (matchedElements == null)
matchedElements = new HashSet<>();
matchedElements.add(matchedNode);
if (handler.shouldAdvanceThePatternFor(patternNode, matchedNode)) {
break;
}
} else if (element != null) {
return false;
}
// clear state of dependent objects
clearingVisitor.clearState(pattern, patternNode);
}
// passed of elements and does not found the match
if (startMatching == matchedNodes.current()) {
final boolean result = validateSatisfactionOfHandlers(patternNodes, context) && matchedOccurs >= minOccurs && matchedOccurs <= maxOccurs;
if (result && context.getMatchedElementsListener() != null) {
context.getMatchedElementsListener().matchedElements(matchedElements);
}
return result;
}
} while (true);
if (!handler.shouldAdvanceThePatternFor(patternNode, null)) {
patternNodes.rewind();
}
}
final boolean result = validateSatisfactionOfHandlers(patternNodes, context);
if (result && context.getMatchedElementsListener() != null) {
context.getMatchedElementsListener().matchedElements(matchedElements);
}
return result;
} finally {
if (saveResult != null) {
if (context.hasResult()) {
saveResult.getMatches().addAll(context.getResult().getMatches());
}
context.setResult(saveResult);
}
}
}
use of com.intellij.structuralsearch.impl.matcher.MatchResultImpl in project intellij-community by JetBrains.
the class SubstitutionHandler method removeLastResults.
private void removeLastResults(int numberOfResults, MatchContext context) {
if (numberOfResults == 0)
return;
final MatchResultImpl substitution = context.getResult().findSon(name);
if (substitution != null) {
final List<PsiElement> matchedNodes = context.getMatchedNodes();
if (substitution.hasSons()) {
final List<MatchResult> sons = substitution.getMatches();
while (numberOfResults > 0) {
--numberOfResults;
final MatchResult matchResult = sons.remove(sons.size() - 1);
if (matchedNodes != null)
matchedNodes.remove(matchResult.getMatch());
}
if (sons.isEmpty()) {
context.getResult().removeSon(name);
}
} else {
final MatchResultImpl matchResult = context.getResult().removeSon(name);
if (matchedNodes != null)
matchedNodes.remove(matchResult.getMatch());
}
}
}
Aggregations