ActionHint understands ProblemHighlightType now in addition to simple true/false

This commit is contained in:
Tagir Valeev
2016-11-23 17:26:56 +07:00
parent e5e55e3479
commit 4ec11eb771
2 changed files with 46 additions and 12 deletions
@@ -16,6 +16,8 @@
package com.intellij.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ex.QuickFixWrapper;
import com.intellij.lang.Commenter;
import com.intellij.lang.LanguageCommenters;
import com.intellij.lang.injection.InjectedLanguageManager;
@@ -40,10 +42,12 @@ import static org.junit.Assert.fail;
public class ActionHint {
final String myExpectedText;
final boolean myShouldPresent;
final ProblemHighlightType myHighlightType;
private ActionHint(String expectedText, boolean shouldPresent) {
private ActionHint(String expectedText, boolean shouldPresent, ProblemHighlightType severity) {
myExpectedText = expectedText;
myShouldPresent = shouldPresent;
myHighlightType = severity;
}
/**
@@ -79,11 +83,25 @@ public class ActionHint {
@Nullable
public IntentionAction findAndCheck(Collection<IntentionAction> actions, Supplier<String> infoSupplier) {
IntentionAction result = actions.stream().filter(t -> t.getText().equals(myExpectedText)).findFirst().orElse(null);
if(result == null && myShouldPresent) {
fail("Action with text '" + myExpectedText + "' not found\nAvailable actions: " +
actions.stream().map(IntentionAction::getText).collect(Collectors.joining(", ", "[", "]\n")) +
infoSupplier.get());
} else if(result != null && !myShouldPresent) {
if(myShouldPresent) {
if(result == null) {
fail("Action with text '" + myExpectedText + "' not found\nAvailable actions: " +
actions.stream().map(IntentionAction::getText).collect(Collectors.joining(", ", "[", "]\n")) +
infoSupplier.get());
} else if(myHighlightType != null) {
if(!(result instanceof QuickFixWrapper)) {
fail("Action with text '" + myExpectedText + "' is not a LocalQuickFix, but " + result.getClass().getName() +
"\nExpected LocalQuickFix with ProblemHighlightType=" + myHighlightType + "\n" +
infoSupplier.get());
}
ProblemHighlightType actualType = ((QuickFixWrapper)result).getHighlightType();
if(actualType != myHighlightType) {
fail("Action with text '" + myExpectedText + "' has wrong ProblemHighlightType.\nExpected: " + myHighlightType +
"\nActual: " + actualType + "\n" + infoSupplier.get());
}
}
}
else if(result != null) {
fail("Action with text '" + myExpectedText + "' is present, but should not\n" + infoSupplier.get());
}
return result;
@@ -93,8 +111,13 @@ public class ActionHint {
* Parse given file with given contents extracting ActionHint of it.
* <p>
* Currently the following syntax is supported:
* // "quick-fix name or intention text" "true|false"
* (replace // with line comment prefix in the corresponding language if necessary)
* </p>
* {@code // "quick-fix name or intention text" "true|false|<ProblemHighlightType>"}
* <p>
* (replace // with line comment prefix in the corresponding language if necessary).
* If {@link ProblemHighlightType} enum value is specified instead of true/false
* (e.g. {@code "INFORMATION"}), then
* it's expected that the action is present and it's a quick-fix with given highlight type.
* </p>
*
* @param file PsiFile associated with contents (used to determine the language)
@@ -114,11 +137,15 @@ public class ActionHint {
assert comment != null : commenter;
// "quick fix action text to perform" "should be available"
Pattern pattern = Pattern.compile("^" + Pattern.quote(comment) + " \"(.*)\" \"(true|false)\".*", Pattern.DOTALL);
Pattern pattern = Pattern.compile("^" + Pattern.quote(comment) + " \"(.*)\" \"(\\w+)\".*", Pattern.DOTALL);
Matcher matcher = pattern.matcher(contents);
TestCase.assertTrue("No comment found in " + file.getVirtualFile(), matcher.matches());
final String text = matcher.group(1);
final boolean actionShouldBeAvailable = Boolean.parseBoolean(matcher.group(2));
return new ActionHint(text, actionShouldBeAvailable);
String state = matcher.group(2);
if(state.equals("true") || state.equals("false")) {
return new ActionHint(text, Boolean.parseBoolean(state), null);
} else {
return new ActionHint(text, true, ProblemHighlightType.valueOf(state));
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.QuickFix;
import com.intellij.openapi.command.undo.UndoUtil;
import com.intellij.openapi.diagnostic.Logger;
@@ -29,6 +30,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
/**
* @author max
@@ -104,6 +106,11 @@ public class QuickFixWrapper implements IntentionAction {
return (LocalQuickFix)myDescriptor.getFixes()[myFixNumber];
}
@TestOnly
public ProblemHighlightType getHighlightType() {
return myDescriptor.getHighlightType();
}
public String toString() {
return getText();
}