SSR: comment matching cleanup

This commit is contained in:
Bas Leijdekkers
2018-11-07 21:07:58 +01:00
parent 7e4106b2c8
commit 6ddc5f1c23
3 changed files with 61 additions and 60 deletions
@@ -0,0 +1,35 @@
// 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.openapi.util.text.StringUtil;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.tree.IElementType;
/**
* @author Bas Leijdekkers
*/
public class JavaMatchUtil {
private JavaMatchUtil() {}
public static String getCommentText(PsiComment comment) {
if (comment instanceof PsiDocComment) {
final PsiDocComment docComment = (PsiDocComment)comment;
final StringBuilder result = new StringBuilder();
for (PsiElement element : docComment.getDescriptionElements()) {
result.append(element.getText());
}
return result.toString();
}
else {
final IElementType type = comment.getTokenType();
final String text = comment.getText();
return (type == JavaTokenType.END_OF_LINE_COMMENT)
? StringUtil.trimStart(text, "//")
: StringUtil.trimEnd(text.substring(2), "*/");
}
}
}
@@ -7,6 +7,7 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.javadoc.PsiDocComment;
import com.intellij.psi.javadoc.PsiDocTag;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.structuralsearch.MatchOptions;
@@ -67,24 +68,18 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
return;
}
final Object userData = comment.getUserData(CompiledPattern.HANDLER_KEY);
if (userData instanceof String) {
final String str = (String)userData;
int end = comment2.getTextLength();
if (comment2.getTokenType() == JavaTokenType.C_STYLE_COMMENT) {
end -= 2;
}
myMatchingVisitor.setResult(((SubstitutionHandler)myMatchingVisitor.getMatchContext().getPattern().getHandler(str)).handle(
comment2,
2,
end,
myMatchingVisitor.getMatchContext()
));
final MatchingHandler handler = (MatchingHandler)comment.getUserData(CompiledPattern.HANDLER_KEY);
if (handler instanceof SubstitutionHandler) {
final IElementType tokenType = comment2.getTokenType();
final int end = comment2.getTextLength();
final SubstitutionHandler substitutionHandler = (SubstitutionHandler)handler;
myMatchingVisitor.setResult(substitutionHandler.handle(comment2,
tokenType == JavaDocTokenType.DOC_COMMENT_START ? 3 : 2,
tokenType == JavaTokenType.END_OF_LINE_COMMENT ? end : end - 2,
myMatchingVisitor.getMatchContext()));
}
else if (userData instanceof MatchingHandler) {
myMatchingVisitor.setResult(((MatchingHandler)userData).match(comment, comment2, myMatchingVisitor.getMatchContext()));
else if (handler != null) {
myMatchingVisitor.setResult(handler.match(comment, comment2, myMatchingVisitor.getMatchContext()));
}
else {
myMatchingVisitor.setResult(myMatchingVisitor.matchText(comment, comment2));
@@ -17,6 +17,7 @@ import com.intellij.structuralsearch.MalformedPatternException;
import com.intellij.structuralsearch.StructuralSearchUtil;
import com.intellij.structuralsearch.impl.matcher.CompiledPattern;
import com.intellij.structuralsearch.impl.matcher.JavaCompiledPattern;
import com.intellij.structuralsearch.impl.matcher.JavaMatchUtil;
import com.intellij.structuralsearch.impl.matcher.filters.*;
import com.intellij.structuralsearch.impl.matcher.handlers.*;
import com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator;
@@ -28,7 +29,6 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.intellij.structuralsearch.impl.matcher.compiler.GlobalCompilingVisitor.OccurenceKind.*;
@@ -39,11 +39,7 @@ import static com.intellij.structuralsearch.impl.matcher.compiler.GlobalCompilin
public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
final GlobalCompilingVisitor myCompilingVisitor;
@NonNls private static final String COMMENT = "\\s*(__\\$_\\w+)\\s*";
private static final Pattern ourPattern = Pattern.compile("//" + COMMENT, Pattern.DOTALL);
private static final Pattern ourPattern2 = Pattern.compile("/\\*" + COMMENT + "\\*/", Pattern.DOTALL);
private static final Pattern ourPattern3 = Pattern.compile("/\\*\\*" + COMMENT + "\\*/", Pattern.DOTALL);
@NonNls private static final Pattern COMMENT_PATTERN = Pattern.compile("__\\$_\\w+");
static final Set<String> excludedKeywords = ContainerUtil.newHashSet(PsiKeyword.CLASS, PsiKeyword.INTERFACE, PsiKeyword.ENUM,
PsiKeyword.THROWS, PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS);
@@ -189,47 +185,23 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
public void visitComment(PsiComment comment) {
super.visitComment(comment);
final String text = comment.getText();
Matcher matcher = ourPattern.matcher(text);
boolean matches = false;
if (!matcher.matches()) {
matcher = ourPattern2.matcher(text);
if (!matcher.matches()) {
matcher = ourPattern3.matcher(text);
}
else {
matches = true;
}
}
else {
matches = true;
}
if (matches || matcher.matches()) {
String str = matcher.group(1);
comment.putUserData(CompiledPattern.HANDLER_KEY, str);
GlobalCompilingVisitor.setFilter(
myCompilingVisitor.getContext().getPattern().getHandler(comment),
CommentFilter.getInstance()
);
SubstitutionHandler handler = (SubstitutionHandler)myCompilingVisitor.getContext().getPattern().getHandler(str);
final CompiledPattern pattern = myCompilingVisitor.getContext().getPattern();
GlobalCompilingVisitor.setFilter(pattern.getHandler(comment), CommentFilter.getInstance());
final String commentText = JavaMatchUtil.getCommentText(comment).trim();
if (COMMENT_PATTERN.matcher(commentText).matches()) {
final SubstitutionHandler handler = (SubstitutionHandler)pattern.getHandler(commentText);
if (handler == null) {
throw new MalformedPatternException();
}
RegExpPredicate predicate = handler.findRegExpPredicate();
comment.putUserData(CompiledPattern.HANDLER_KEY, handler);
final RegExpPredicate predicate = handler.findRegExpPredicate();
if (GlobalCompilingVisitor.isSuitablePredicate(predicate, handler)) {
myCompilingVisitor.processTokenizedName(predicate.getRegExp(), true, GlobalCompilingVisitor.OccurenceKind.COMMENT);
myCompilingVisitor.processTokenizedName(predicate.getRegExp(), true, COMMENT);
}
matches = true;
}
if (!matches) {
MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(text, GlobalCompilingVisitor.OccurenceKind.COMMENT);
else {
final MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(comment.getText(), COMMENT);
if (handler != null) comment.putUserData(CompiledPattern.HANDLER_KEY, handler);
}
}
@@ -248,11 +220,10 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
@Override
public void visitLiteralExpression(PsiLiteralExpression expression) {
String text = expression.getText();
final String text = expression.getText();
if (StringUtil.isQuotedString(text)) {
@Nullable MatchingHandler handler =
myCompilingVisitor.processPatternStringWithFragments(text, LITERAL);
@Nullable final MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(text, LITERAL);
if (PsiType.CHAR.equals(expression.getType()) &&
(handler instanceof LiteralWithSubstitutionHandler || handler == null && expression.getValue() == null)) {