Search in sources :

Example 1 with MatchResult

use of com.intellij.structuralsearch.MatchResult 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)

Example 2 with MatchResult

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

the class ReplacementBuilder method process.

String process(MatchResult match, ReplacementInfoImpl replacementInfo, FileType type) {
    if (parameterizations == null) {
        return replacement;
    }
    final StringBuilder result = new StringBuilder(replacement);
    final HashMap<String, MatchResult> matchMap = new HashMap<>();
    fill(match, matchMap);
    final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByFileType(type);
    assert profile != null;
    int offset = 0;
    for (final ParameterInfo info : parameterizations) {
        MatchResult r = matchMap.get(info.getName());
        if (info.isReplacementVariable()) {
            offset = Replacer.insertSubstitution(result, offset, info, generateReplacement(info, match));
        } else if (r != null) {
            offset = profile.handleSubstitution(info, r, result, offset, matchMap);
        } else {
            offset = profile.handleNoSubstitution(info, offset, result);
        }
    }
    replacementInfo.variableMap = matchMap;
    return result.toString();
}
Also used : HashMap(java.util.HashMap) StructuralSearchProfile(com.intellij.structuralsearch.StructuralSearchProfile) MatchResult(com.intellij.structuralsearch.MatchResult)

Example 3 with MatchResult

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

the class MatchResultImpl method clear.

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

Example 4 with MatchResult

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

the class GuavaOptionalConversionUtil method simplifyParameterPattern.

static String simplifyParameterPattern(PsiMethodCallExpression methodCall) {
    final PsiExpressionList argumentList = methodCall.getArgumentList();
    final PsiExpression[] expressions = argumentList.getExpressions();
    if (expressions.length == 1) {
        final PsiExpression expression = expressions[0];
        Matcher matcher = new Matcher(methodCall.getProject());
        final MatchOptions options = new MatchOptions();
        options.setFileType(StdFileTypes.JAVA);
        final List<MatchResult> results = matcher.testFindMatches(expression.getText(), GuavaOptionalConversionRule.OPTIONAL_CONVERTOR_PATTERN, options, false);
        if (!results.isEmpty()) {
            final MatchResult result = results.get(0);
            if (result.getStart() == 0 && result.getEnd() == -1) {
                return GuavaOptionalConversionRule.OPTIONAL_CONVERTOR_PATTERN;
            }
        }
    }
    return "$o$";
}
Also used : Matcher(com.intellij.structuralsearch.Matcher) MatchOptions(com.intellij.structuralsearch.MatchOptions) MatchResult(com.intellij.structuralsearch.MatchResult)

Example 5 with MatchResult

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

the class ReplacementContext method getNewName2PatternNameMap.

public Map<String, String> getNewName2PatternNameMap() {
    Map<String, String> newNameToSearchPatternNameMap = new HashMap<>(1);
    final Map<String, MatchResult> variableMap = replacementInfo.getVariableMap();
    if (variableMap != null) {
        for (String s : variableMap.keySet()) {
            final MatchResult matchResult = replacementInfo.getVariableMap().get(s);
            PsiElement match = matchResult.getMatch();
            match = StructuralSearchUtil.getParentIfIdentifier(match);
            if (match instanceof PsiNamedElement) {
                final String name = ((PsiNamedElement) match).getName();
                newNameToSearchPatternNameMap.put(name, s);
            }
        }
    }
    return newNameToSearchPatternNameMap;
}
Also used : HashMap(java.util.HashMap) PsiNamedElement(com.intellij.psi.PsiNamedElement) 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