Search in sources :

Example 6 with MatchResult

use of com.intellij.structuralsearch.MatchResult in project intellij-community by JetBrains.

the class SubstitutionHandler method addResult.

public void addResult(PsiElement match, int start, int end, MatchContext context) {
    if (totalMatchedOccurs == -1) {
        final MatchResultImpl matchResult = context.getResult();
        final MatchResultImpl substitution = matchResult.findSon(name);
        if (substitution == null) {
            matchResult.addSon(createMatch(match, start, end));
        } else if (maxOccurs > 1) {
            final MatchResultImpl result = createMatch(match, start, end);
            if (!substitution.isMultipleMatch()) {
                // adding intermediate node to contain all multiple matches
                final MatchResultImpl sonresult = new MatchResultImpl(substitution.getName(), substitution.getMatchImage(), substitution.getMatchRef(), substitution.getStart(), substitution.getEnd(), target);
                substitution.setMatchRef(new SmartPsiPointer(match == null ? null : match));
                substitution.setMultipleMatch(true);
                if (substitution.isScopeMatch()) {
                    substitution.setScopeMatch(false);
                    sonresult.setScopeMatch(true);
                    for (MatchResult r : substitution.getAllSons()) sonresult.addSon((MatchResultImpl) r);
                    substitution.clearMatches();
                }
                substitution.addSon(sonresult);
            }
            substitution.addSon(result);
        }
    }
}
Also used : MatchResultImpl(com.intellij.structuralsearch.impl.matcher.MatchResultImpl) SmartPsiPointer(com.intellij.structuralsearch.plugin.util.SmartPsiPointer) MatchResult(com.intellij.structuralsearch.MatchResult)

Example 7 with MatchResult

use of com.intellij.structuralsearch.MatchResult in project intellij-community by JetBrains.

the class ScriptSupport method buildVariableMap.

private static Map<String, Object> buildVariableMap(@NotNull MatchResult result, @NotNull Map<String, Object> out) {
    final String name = result.getName();
    if (name != null && !result.isMultipleMatch()) {
        final Object value = out.get(name);
        final PsiElement match = StructuralSearchUtil.getPresentableElement(result.getMatch());
        if (value == null) {
            out.put(name, match);
        } else if (value instanceof List) {
            @SuppressWarnings("unchecked") final List<PsiElement> list = (List<PsiElement>) value;
            list.add(match);
        } else if (value instanceof PsiElement) {
            final List<PsiElement> list = new ArrayList<>();
            list.add((PsiElement) value);
            list.add(match);
            out.put(name, list);
        } else {
            throw new AssertionError();
        }
    }
    if (result.hasSons()) {
        for (MatchResult son : result.getAllSons()) {
            buildVariableMap(son, out);
        }
    }
    return out;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MatchResult(com.intellij.structuralsearch.MatchResult) PsiElement(com.intellij.psi.PsiElement)

Aggregations

MatchResult (com.intellij.structuralsearch.MatchResult)7 PsiElement (com.intellij.psi.PsiElement)3 MatchResultImpl (com.intellij.structuralsearch.impl.matcher.MatchResultImpl)2 HashMap (java.util.HashMap)2 PsiNamedElement (com.intellij.psi.PsiNamedElement)1 MatchOptions (com.intellij.structuralsearch.MatchOptions)1 Matcher (com.intellij.structuralsearch.Matcher)1 StructuralSearchProfile (com.intellij.structuralsearch.StructuralSearchProfile)1 SmartPsiPointer (com.intellij.structuralsearch.plugin.util.SmartPsiPointer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1