Search in sources :

Example 1 with MatchResultImpl

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);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FindProgressIndicator(com.intellij.find.FindProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Alarm(com.intellij.util.Alarm) MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl) PsiFile(com.intellij.psi.PsiFile) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 2 with MatchResultImpl

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);
        }
    }
}
Also used : CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl) HashSet(java.util.HashSet)

Example 3 with MatchResultImpl

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;
}
Also used : SmartPsiPointer(com.intellij.structuralsearch.plugin.util.SmartPsiPointer) MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl)

Example 4 with MatchResultImpl

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);
        }
    }
}
Also used : CompiledPattern(com.intellij.structuralsearch.impl.matcher.CompiledPattern) MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet)

Example 5 with MatchResultImpl

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());
        }
    }
}
Also used : MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl) MatchResult(com.intellij.structuralsearch.MatchResult) PsiElement(com.intellij.psi.PsiElement)

Aggregations

MatchResultImpl (com.intellij.structuralsearch.impl.matcher.MatchResultImpl)6 PsiElement (com.intellij.psi.PsiElement)3 MatchResult (com.intellij.structuralsearch.MatchResult)2 CompiledPattern (com.intellij.structuralsearch.impl.matcher.CompiledPattern)2 SmartPsiPointer (com.intellij.structuralsearch.plugin.util.SmartPsiPointer)2 HashSet (java.util.HashSet)2 FindProgressIndicator (com.intellij.find.FindProgressIndicator)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 UsageInfo (com.intellij.usageView.UsageInfo)1 Alarm (com.intellij.util.Alarm)1