Search in sources :

Example 91 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class MisspelledHeaderInspectionTest method testFix.

public void testFix() {
    myFixture.enableInspections(new MisspelledHeaderInspection());
    myFixture.configureByText(ManifestFileTypeFactory.MANIFEST, "ManifestVersion: 1.0\n");
    List<IntentionAction> intentions = myFixture.filterAvailableIntentions("Change to");
    assertEquals(1, intentions.size());
    myFixture.launchAction(intentions.get(0));
    myFixture.checkResult("Manifest-Version: 1.0\n");
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) MisspelledHeaderInspection(org.jetbrains.lang.manifest.highlighting.MisspelledHeaderInspection)

Example 92 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project intellij-community by JetBrains.

the class MisspelledHeaderInspectionTest method testCustomHeaderFix.

public void testCustomHeaderFix() {
    MisspelledHeaderInspection inspection = new MisspelledHeaderInspection();
    myFixture.enableInspections(inspection);
    myFixture.configureByText(ManifestFileTypeFactory.MANIFEST, "Custom-Header: -\n");
    List<IntentionAction> intentions = myFixture.filterAvailableIntentions("Add ");
    assertEquals(1, intentions.size());
    myFixture.launchAction(intentions.get(0));
    assertEquals(Collections.singleton("Custom-Header"), inspection.CUSTOM_HEADERS);
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction) MisspelledHeaderInspection(org.jetbrains.lang.manifest.highlighting.MisspelledHeaderInspection)

Example 93 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project kotlin by JetBrains.

the class AbstractQuickFixMultiFileTest method doAction.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void doAction(String text, boolean actionShouldBeAvailable, String testFilePath) throws Exception {
    Pattern pattern = text.startsWith("/") ? Pattern.compile(text.substring(1, text.length() - 1)) : Pattern.compile(StringUtil.escapeToRegexp(text));
    List<IntentionAction> availableActions = getAvailableActions();
    IntentionAction action = findActionByPattern(pattern, availableActions);
    if (action == null) {
        if (actionShouldBeAvailable) {
            List<String> texts = getActionsTexts(availableActions);
            Collection<HighlightInfo> infos = doHighlighting();
            fail("Action with text '" + text + "' is not available in test " + testFilePath + "\n" + "Available actions (" + texts.size() + "): \n" + StringUtil.join(texts, "\n") + "\nActions:\n" + StringUtil.join(availableActions, "\n") + "\nInfos:\n" + StringUtil.join(infos, "\n"));
        } else {
            DirectiveBasedActionUtils.INSTANCE.checkAvailableActionsAreExpected(getFile(), availableActions);
        }
    } else {
        if (!actionShouldBeAvailable) {
            fail("Action '" + text + "' is available (but must not) in test " + testFilePath);
        }
        ShowIntentionActionsHandler.chooseActionAndInvoke(getFile(), getEditor(), action, action.getText());
        UIUtil.dispatchAllInvocationEvents();
        //noinspection ConstantConditions
        if (!shouldBeAvailableAfterExecution()) {
            IntentionAction afterAction = findActionByPattern(pattern, getAvailableActions());
            if (afterAction != null) {
                fail("Action '" + text + "' is still available after its invocation in test " + testFilePath);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo)

Example 94 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project kotlin by JetBrains.

the class AbstractQuickFixTest method checkForUnexpectedActions.

private void checkForUnexpectedActions() throws ClassNotFoundException {
    String text = getEditor().getDocument().getText();
    Pair<String, Boolean> pair = parseActionHintImpl(getFile(), text);
    if (!pair.second) {
        List<IntentionAction> actions = getAvailableActions();
        String prefix = "class ";
        if (pair.first.startsWith(prefix)) {
            String className = pair.first.substring(prefix.length());
            final Class<?> aClass = Class.forName(className);
            assert IntentionAction.class.isAssignableFrom(aClass) : className + " should be inheritor of IntentionAction";
            final Set<String> validActions = new HashSet<String>(InTextDirectivesUtils.findLinesWithPrefixesRemoved(text, "// ACTION:"));
            CollectionsKt.removeAll(actions, new Function1<IntentionAction, Boolean>() {

                @Override
                public Boolean invoke(IntentionAction action) {
                    return !aClass.isAssignableFrom(action.getClass()) || validActions.contains(action.getText());
                }
            });
            if (!actions.isEmpty()) {
                Assert.fail("Unexpected intention actions present\n " + CollectionsKt.map(actions, new Function1<IntentionAction, String>() {

                    @Override
                    public String invoke(IntentionAction action) {
                        return action.getClass().toString() + " " + action.toString() + "\n";
                    }
                }));
            }
            for (IntentionAction action : actions) {
                if (aClass.isAssignableFrom(action.getClass()) && !validActions.contains(action.getText())) {
                    Assert.fail("Unexpected intention action " + action.getClass() + " found");
                }
            }
        } else {
            // Action shouldn't be found. Check that other actions are expected and thus tested action isn't there under another name.
            DirectiveBasedActionUtils.INSTANCE.checkAvailableActionsAreExpected(getFile(), actions);
        }
    }
}
Also used : SuppressIntentionAction(com.intellij.codeInspection.SuppressIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) HashSet(java.util.HashSet)

Example 95 with IntentionAction

use of com.intellij.codeInsight.intention.IntentionAction in project kotlin by JetBrains.

the class AbstractIntentionTest method doTest.

protected void doTest(@NotNull String path) throws Exception {
    File mainFile = new File(path);
    String mainFileName = FileUtil.getNameWithoutExtension(mainFile);
    IntentionAction intentionAction = createIntention(mainFile);
    List<String> sourceFilePaths = new ArrayList<String>();
    File parentDir = mainFile.getParentFile();
    extraFileLoop: //noinspection ForLoopThatDoesntUseLoopVariable
    for (int i = 1; true; i++) {
        for (String extension : EXTENSIONS) {
            File extraFile = new File(parentDir, mainFileName + "." + i + extension);
            if (extraFile.exists()) {
                sourceFilePaths.add(extraFile.getPath());
                continue extraFileLoop;
            }
        }
        break;
    }
    sourceFilePaths.add(path);
    Map<String, PsiFile> pathToFiles = ContainerUtil.newMapFromKeys(sourceFilePaths.iterator(), new Convertor<String, PsiFile>() {

        @Override
        public PsiFile convert(String path) {
            try {
                configureByFile(path);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return myFile;
        }
    });
    String fileText = FileUtil.loadFile(mainFile, true);
    assertTrue("\"<caret>\" is missing in file \"" + mainFile + "\"", fileText.contains("<caret>"));
    String minJavaVersion = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// MIN_JAVA_VERSION: ");
    if (minJavaVersion != null && !SystemInfo.isJavaVersionAtLeast(minJavaVersion))
        return;
    boolean isWithRuntime = InTextDirectivesUtils.findStringWithPrefixes(fileText, "// WITH_RUNTIME") != null;
    try {
        if (isWithRuntime) {
            ConfigLibraryUtil.configureKotlinRuntimeAndSdk(getModule(), PluginTestCaseBase.mockJdk());
        }
        ConfigLibraryUtil.configureLibrariesByDirective(getModule(), PlatformTestUtil.getCommunityPath(), fileText);
        if (getFile() instanceof KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_BEFORE")) {
            DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) getFile());
        }
        doTestFor(path, pathToFiles, intentionAction, fileText);
        if (getFile() instanceof KtFile && !InTextDirectivesUtils.isDirectiveDefined(fileText, "// SKIP_ERRORS_AFTER")) {
            DirectiveBasedActionUtils.INSTANCE.checkForUnexpectedErrors((KtFile) getFile());
        }
    } finally {
        ConfigLibraryUtil.unconfigureLibrariesByDirective(myModule, fileText);
        if (isWithRuntime) {
            ConfigLibraryUtil.unConfigureKotlinRuntimeAndSdk(getModule(), getTestProjectJdk());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile) PsiFile(com.intellij.psi.PsiFile) KtFile(org.jetbrains.kotlin.psi.KtFile) File(java.io.File)

Aggregations

IntentionAction (com.intellij.codeInsight.intention.IntentionAction)242 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)31 NotNull (org.jetbrains.annotations.NotNull)23 Project (com.intellij.openapi.project.Project)20 TextRange (com.intellij.openapi.util.TextRange)20 Editor (com.intellij.openapi.editor.Editor)17 PsiFile (com.intellij.psi.PsiFile)17 Annotation (com.intellij.lang.annotation.Annotation)16 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)15 ArrayList (java.util.ArrayList)14 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)10 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)9 Pair (com.intellij.openapi.util.Pair)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 EmptyIntentionAction (com.intellij.codeInsight.intention.EmptyIntentionAction)6 AndroidMissingOnClickHandlerInspection (org.jetbrains.android.inspections.AndroidMissingOnClickHandlerInspection)6 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)5 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)5