IDEA-243739 SSR: Improve comments matching

LiteralWithSubstitutionHandler was taking into account the complete comment text when matching.
By making it possible to add an offset when matching, we can make end of line / block comments match with other comments types when the pattern contains variables.

GitOrigin-RevId: fb7a46dddbcd49177954352271cddc9aeadad9c6
This commit is contained in:
yopox
2020-06-17 22:02:07 +03:00
committed by intellij-monorepo-bot
parent 6a837bdad9
commit 28bdd4c75a
3 changed files with 18 additions and 4 deletions
@@ -13,6 +13,7 @@ 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.LiteralWithSubstitutionHandler;
import com.intellij.structuralsearch.impl.matcher.handlers.MatchingHandler;
import com.intellij.structuralsearch.impl.matcher.handlers.SubstitutionHandler;
import com.intellij.structuralsearch.impl.matcher.iterators.DocValuesIterator;
@@ -85,6 +86,15 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
myMatchingVisitor.setResult(substitutionHandler.handle(other, start, end, myMatchingVisitor.getMatchContext()));
}
}
else if (handler instanceof LiteralWithSubstitutionHandler) {
final LiteralWithSubstitutionHandler lwsHandler = (LiteralWithSubstitutionHandler) handler;
int offset = other.getTokenType() == JavaDocTokenType.DOC_COMMENT_START ? 3 : 2;
String commentText = comment.getText();
while (commentText.length() > offset && commentText.charAt(offset) <= ' ') {
offset++;
}
myMatchingVisitor.setResult(lwsHandler.match(other, JavaMatchUtil.getCommentText(other).trim(), offset, myMatchingVisitor.getMatchContext()));
}
else if (handler != null) {
myMatchingVisitor.setResult(handler.match(comment, other, myMatchingVisitor.getMatchContext()));
}
@@ -204,7 +204,7 @@ public class JavaCompilingVisitor extends JavaRecursiveElementWalkingVisitor {
}
else if (!commentText.isEmpty()) {
if (myCompilingVisitor.hasFragments(commentText)) {
final MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(comment.getText(), COMMENT);
final MatchingHandler handler = myCompilingVisitor.processPatternStringWithFragments(JavaMatchUtil.getCommentText(comment).trim(), COMMENT);
if (handler != null) comment.putUserData(CompiledPattern.HANDLER_KEY, handler);
}
else {
@@ -22,10 +22,14 @@ public class LiteralWithSubstitutionHandler extends MatchingHandler {
@Override
public boolean match(PsiElement patternNode, PsiElement matchedNode, MatchContext context) {
final String text = matchedNode.getText();
return match(matchedNode, matchedNode.getText(), 0, context);
}
public boolean match(PsiElement matchedNode, String text, int textOffset, MatchContext context) {
if (myMatcher == null) {
myMatcher = Pattern.compile(myRegexp, myCaseSensitive ? 0 : Pattern.CASE_INSENSITIVE).matcher(text);
} else {
}
else {
myMatcher.reset(text);
}
@@ -35,7 +39,7 @@ public class LiteralWithSubstitutionHandler extends MatchingHandler {
for (int i = 0; i < myHandlers.size(); ++i) {
final SubstitutionHandler handler = myHandlers.get(i);
if (!handler.handle(matchedNode, myMatcher.start(i + 1), myMatcher.end(i + 1), context)) {
if (!handler.handle(matchedNode, textOffset + myMatcher.start(i + 1), textOffset + myMatcher.end(i + 1), context)) {
return false;
}
}