SSR: move method to profile

This commit is contained in:
Bas Leijdekkers
2018-11-07 21:07:57 +01:00
parent 6dac93eff3
commit 671dd1ad2a
7 changed files with 54 additions and 53 deletions
@@ -113,13 +113,12 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
@Override
public String getMeaningfulText(PsiElement element) {
if (element instanceof PsiReferenceExpression &&
((PsiReferenceExpression)element).getQualifierExpression() != null) {
if (element instanceof PsiReferenceExpression && ((PsiReferenceExpression)element).getQualifierExpression() != null) {
final PsiElement resolve = ((PsiReferenceExpression)element).resolve();
if (resolve instanceof PsiClass) return element.getText();
final PsiElement referencedElement = ((PsiReferenceExpression)element).getReferenceNameElement();
String text = referencedElement != null ? referencedElement.getText() : "";
final String text = referencedElement != null ? referencedElement.getText() : "";
if (resolve == null && text.length() > 0 && Character.isUpperCase(text.charAt(0))) {
return element.getText();
@@ -129,6 +128,31 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
return super.getMeaningfulText(element);
}
@Override
@Nullable
public String getAlternativeText(PsiElement node, String previousText) {
// Short class name is matched with fully qualified name
if(node instanceof PsiJavaCodeReferenceElement || node instanceof PsiClass) {
final PsiElement element = (node instanceof PsiJavaCodeReferenceElement)
? ((PsiJavaCodeReferenceElement)node).resolve()
: node;
if (element instanceof PsiClass) {
String text = ((PsiClass)element).getQualifiedName();
if (text != null && text.equals(previousText)) {
text = ((PsiClass)element).getName();
}
if (text != null) {
return text;
}
}
} else if (node instanceof PsiLiteralExpression || node instanceof PsiComment) {
return node.getText();
}
return null;
}
@Override
public PsiElement updateCurrentNode(PsiElement targetNode) {
if (targetNode instanceof PsiCodeBlock && ((PsiCodeBlock)targetNode).getStatementCount() == 1) {
@@ -1,9 +1,11 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.impl.matcher;
import com.intellij.psi.*;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiJavaToken;
import com.intellij.structuralsearch.impl.matcher.strategies.JavaMatchingStrategy;
import org.jetbrains.annotations.Nullable;
/**
* @author Eugene.Kudelevsky
@@ -40,31 +42,6 @@ public class JavaCompiledPattern extends CompiledPattern {
!(element instanceof PsiJavaCodeReferenceElement && element.getParent() instanceof PsiAnnotation);
}
@Nullable
@Override
public String getAlternativeTextToMatch(PsiElement node, String previousText) {
// Short class name is matched with fully qualified name
if(node instanceof PsiJavaCodeReferenceElement || node instanceof PsiClass) {
PsiElement element = (node instanceof PsiJavaCodeReferenceElement)?
((PsiJavaCodeReferenceElement)node).resolve():
node;
if (element instanceof PsiClass) {
String text = ((PsiClass)element).getQualifiedName();
if (text != null && text.equals(previousText)) {
text = ((PsiClass)element).getName();
}
if (text != null) {
return text;
}
}
} else if (node instanceof PsiLiteralExpression) {
return node.getText();
}
return null;
}
public boolean isRequestsSuperFields() {
return requestsSuperFields;
}
@@ -10,6 +10,7 @@ import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler;
import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler;
import com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator;
@@ -813,7 +814,7 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
regExpPredicate.setNodeTextGenerator(new RegExpPredicate.NodeTextGenerator() {
@Override
public String getText(PsiElement element) {
StringBuilder builder = new StringBuilder(RegExpPredicate.getMeaningfulText(element));
StringBuilder builder = new StringBuilder(StructuralSearchUtil.getMeaningfulText(element));
for (int i = 0; i < matchedArrayDimensions; ++i) builder.append("[]");
return builder.toString();
}
@@ -197,6 +197,10 @@ public abstract class StructuralSearchProfile {
return getTypedVarString(element);
}
public String getAlternativeText(PsiElement element, String previousText) {
return null;
}
public PsiElement updateCurrentNode(PsiElement node) {
return node;
}
@@ -179,4 +179,14 @@ public class StructuralSearchUtil {
final StructuralSearchProfile profile = getProfileByPsiElement(match);
return profile != null && profile.isDocCommentOwner(match);
}
public static String getMeaningfulText(PsiElement matchedNode) {
final StructuralSearchProfile profile = getProfileByPsiElement(matchedNode);
return profile != null ? profile.getMeaningfulText(matchedNode) : matchedNode.getText();
}
public static String getAlternativeText(PsiElement matchedNode, String previousText) {
final StructuralSearchProfile profile = getProfileByPsiElement(matchedNode);
return profile != null ? profile.getAlternativeText(matchedNode, previousText) : null;
}
}
@@ -161,11 +161,6 @@ public abstract class CompiledPattern {
return true;
}
@Nullable
public String getAlternativeTextToMatch(PsiElement node, String previousText) {
return null;
}
@NotNull
public List<PsiElement> getVariableNodes(@NotNull String name) {
final Collection<PsiElement> elements = variableNodes.get(name);
@@ -4,7 +4,6 @@ package com.intellij.structuralsearch.impl.matcher.predicates;
import com.intellij.psi.*;
import com.intellij.structuralsearch.MalformedPatternException;
import com.intellij.structuralsearch.SSRBundle;
import com.intellij.structuralsearch.StructuralSearchProfile;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.MatchContext;
import com.intellij.structuralsearch.impl.matcher.MatchResultImpl;
@@ -55,10 +54,7 @@ public final class RegExpPredicate extends MatchPredicate {
realRegexp = ".*?\\b(?:" + realRegexp + ")\\b.*?";
}
pattern = Pattern.compile(
realRegexp,
(caseSensitive ? 0: Pattern.CASE_INSENSITIVE) | (multiline ? Pattern.DOTALL:0)
);
pattern = Pattern.compile(realRegexp, (caseSensitive ? 0: Pattern.CASE_INSENSITIVE) | (multiline ? Pattern.DOTALL:0));
} catch(PatternSyntaxException ex) {
throw new MalformedPatternException(SSRBundle.message("error.incorrect.regexp.constraint", regexp, baseHandlerName));
}
@@ -82,15 +78,14 @@ public final class RegExpPredicate extends MatchPredicate {
public boolean match(PsiElement matchedNode, int start, int end, MatchContext context) {
if (matchedNode==null) return false;
String text = myNodeTextGenerator != null ? myNodeTextGenerator.getText(matchedNode) : getMeaningfulText(matchedNode);
String text = myNodeTextGenerator != null
? myNodeTextGenerator.getText(matchedNode)
: StructuralSearchUtil.getMeaningfulText(matchedNode);
boolean result = doMatch(text, start, end, context, matchedNode);
if (!result) {
matchedNode = StructuralSearchUtil.getParentIfIdentifier(matchedNode);
String alternativeText = context.getPattern().getAlternativeTextToMatch(matchedNode, text);
String alternativeText = StructuralSearchUtil.getAlternativeText(matchedNode, text);
if (alternativeText != null) {
result = doMatch(alternativeText, start, end, context, matchedNode);
}
@@ -99,13 +94,8 @@ public final class RegExpPredicate extends MatchPredicate {
return result;
}
public static String getMeaningfulText(PsiElement matchedNode) {
final StructuralSearchProfile profile = StructuralSearchUtil.getProfileByPsiElement(matchedNode);
return profile != null ? profile.getMeaningfulText(matchedNode) : matchedNode.getText();
}
boolean doMatch(String text, MatchContext context, PsiElement matchedElement) {
return doMatch(text,0,-1,context, matchedElement);
return doMatch(text, 0, -1 ,context, matchedElement);
}
boolean doMatch(String text, int from, int end, MatchContext context,PsiElement matchedElement) {
@@ -114,7 +104,7 @@ public final class RegExpPredicate extends MatchPredicate {
}
if (simpleString) {
return (caseSensitive)?text.equals(regexp):text.equalsIgnoreCase(regexp);
return caseSensitive ? text.equals(regexp) : text.equalsIgnoreCase(regexp);
}
if(!multiline && text.contains("\n")) setMultiline(true);