List<Match> (@NotNull List<Match> matches, PsiElement @NotNull [] pattern, @NotNull List<PsiElement[]> candidates) { List<Match> result = new ArrayList<>(); Set<PsiExpression> firstUsages = null; for (Match match : matches) { List<ExtractedParameter> parameters = match.getExtractedParameters(); PsiElement[] candidateElements = ContainerUtil.find(candidates, elements -> elements.length != 0 && match.getMatchStart() == elements[0]); Set<PsiVariable> candidateVariables = ContainerUtil.map2SetNotNull(parameters, parameter -> parameter.myCandidate.myVariable); if (candidateElements == null || containsModifiedField(candidateElements, candidateVariables)) { continue; } Set<PsiExpression> patternUsages = StreamEx.of(parameters).map(p -> p.myPattern.getUsage()).toSet(); if (firstUsages == null) { Set<PsiVariable> patternVariables = ContainerUtil.map2SetNotNull(parameters, parameter -> parameter.myPattern.myVariable); if (containsModifiedField(pattern, patternVariables)) { return Collections.emptyList(); } firstUsages = patternUsages; result.add(match); } else if (firstUsages.equals(patternUsages)) { result.add(match); } } return result; }