Search in sources :

Example 16 with IntentionAction

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

the class AbstractJavaFXQuickFixTest method doLaunchQuickfixTest.

protected void doLaunchQuickfixTest(final String tagName) throws Exception {
    myFixture.configureByFiles(getTestName(true) + ".fxml");
    final IntentionAction singleIntention = myFixture.findSingleIntention(getHint(tagName));
    assertNotNull(singleIntention);
    myFixture.launchAction(singleIntention);
    myFixture.checkResultByFile(getTestName(true) + "_after.fxml");
}
Also used : IntentionAction(com.intellij.codeInsight.intention.IntentionAction)

Example 17 with IntentionAction

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

the class PropertiesAnnotator method highlightTokens.

private static void highlightTokens(final Property property, final ASTNode node, final AnnotationHolder holder, PropertiesHighlighter highlighter) {
    Lexer lexer = highlighter.getHighlightingLexer();
    final String s = node.getText();
    lexer.start(s);
    while (lexer.getTokenType() != null) {
        IElementType elementType = lexer.getTokenType();
        TextAttributesKey[] keys = highlighter.getTokenHighlights(elementType);
        for (TextAttributesKey key : keys) {
            Pair<String, HighlightSeverity> pair = PropertiesHighlighter.DISPLAY_NAMES.get(key);
            String displayName = pair.getFirst();
            HighlightSeverity severity = pair.getSecond();
            if (severity != null) {
                int start = lexer.getTokenStart() + node.getTextRange().getStartOffset();
                int end = lexer.getTokenEnd() + node.getTextRange().getStartOffset();
                TextRange textRange = new TextRange(start, end);
                final Annotation annotation;
                if (severity == HighlightSeverity.WARNING) {
                    annotation = holder.createWarningAnnotation(textRange, displayName);
                } else if (severity == HighlightSeverity.ERROR) {
                    annotation = holder.createErrorAnnotation(textRange, displayName);
                } else {
                    annotation = holder.createInfoAnnotation(textRange, displayName);
                }
                TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key);
                annotation.setEnforcedTextAttributes(attributes);
                if (key == PropertiesHighlighter.PROPERTIES_INVALID_STRING_ESCAPE) {
                    annotation.registerFix(new IntentionAction() {

                        @NotNull
                        public String getText() {
                            return PropertiesBundle.message("unescape");
                        }

                        @NotNull
                        public String getFamilyName() {
                            return getText();
                        }

                        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                            if (!property.isValid() || !property.getManager().isInProject(property))
                                return false;
                            String text = property.getPropertiesFile().getContainingFile().getText();
                            int startOffset = annotation.getStartOffset();
                            return text.length() > startOffset && text.charAt(startOffset) == '\\';
                        }

                        public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
                            int offset = annotation.getStartOffset();
                            if (property.getPropertiesFile().getContainingFile().getText().charAt(offset) == '\\') {
                                editor.getDocument().deleteString(offset, offset + 1);
                            }
                        }

                        public boolean startInWriteAction() {
                            return true;
                        }
                    });
                }
            }
        }
        lexer.advance();
    }
}
Also used : HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) TextRange(com.intellij.openapi.util.TextRange) TextAttributesKey(com.intellij.openapi.editor.colors.TextAttributesKey) NotNull(org.jetbrains.annotations.NotNull) Annotation(com.intellij.lang.annotation.Annotation) IElementType(com.intellij.psi.tree.IElementType) Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 18 with IntentionAction

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

the class SkeletonTestTask method runTestOn.

@Override
public void runTestOn(@NotNull final String sdkHome) throws IOException, InvalidSdkException {
    final Sdk sdk = createTempSdk(sdkHome, SdkCreationType.SDK_PACKAGES_ONLY);
    final File skeletonsPath = new File(PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath()));
    // File with module skeleton
    File skeletonFileOrDirectory = new File(skeletonsPath, myModuleNameToBeGenerated);
    // Module may be stored in "moduleName.py" or "moduleName/__init__.py"
    if (skeletonFileOrDirectory.isDirectory()) {
        skeletonFileOrDirectory = new File(skeletonFileOrDirectory, PyNames.INIT_DOT_PY);
    } else {
        skeletonFileOrDirectory = new File(skeletonFileOrDirectory.getAbsolutePath() + PyNames.DOT_PY);
    }
    final File skeletonFile = skeletonFileOrDirectory;
    if (skeletonFile.exists()) {
        // To make sure we do not reuse it
        assert skeletonFile.delete() : "Failed to delete file " + skeletonFile;
    }
    ApplicationManager.getApplication().invokeAndWait(() -> {
        // File that uses CLR library
        myFixture.copyFileToProject("dotNet/" + mySourceFileToRunGenerationOn, mySourceFileToRunGenerationOn);
        // Library itself
        myFixture.copyFileToProject("dotNet/PythonLibs.dll", "PythonLibs.dll");
        // Another library
        myFixture.copyFileToProject("dotNet/SingleNameSpace.dll", "SingleNameSpace.dll");
        myFixture.configureByFile(mySourceFileToRunGenerationOn);
    }, ModalityState.NON_MODAL);
    // This inspection should suggest us to generate stubs
    myFixture.enableInspections(PyUnresolvedReferencesInspection.class);
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
            final String intentionName = PyBundle.message("sdk.gen.stubs.for.binary.modules", myUseQuickFixWithThisModuleOnly);
            final IntentionAction intention = myFixture.findSingleIntention(intentionName);
            Assert.assertNotNull("No intention found to generate skeletons!", intention);
            Assert.assertThat("Intention should be quick fix to run", intention, Matchers.instanceOf(QuickFixWrapper.class));
            final LocalQuickFix quickFix = ((QuickFixWrapper) intention).getFix();
            Assert.assertThat("Quick fix should be 'generate binary skeletons' fix to run", quickFix, Matchers.instanceOf(GenerateBinaryStubsFix.class));
            final Task fixTask = ((GenerateBinaryStubsFix) quickFix).getFixTask(myFixture.getFile());
            fixTask.run(new AbstractProgressIndicatorBase());
        }
    });
    FileUtil.copy(skeletonFile, new File(myFixture.getTempDirPath(), skeletonFile.getName()));
    if (myExpectedSkeletonFile != null) {
        final String actual = StreamUtil.readText(new FileInputStream(skeletonFile), Charset.defaultCharset());
        final String skeletonText = StreamUtil.readText(new FileInputStream(new File(getTestDataPath(), myExpectedSkeletonFile)), Charset.defaultCharset());
        // TODO: Move to separate method ?
        if (!Matchers.equalToIgnoringWhiteSpace(removeGeneratorVersion(skeletonText)).matches(removeGeneratorVersion(actual))) {
            throw new FileComparisonFailure("asd", skeletonText, actual, skeletonFile.getAbsolutePath());
        }
    }
    myFixture.configureByFile(skeletonFile.getName());
}
Also used : Task(com.intellij.openapi.progress.Task) PyExecutionFixtureTestTask(com.jetbrains.env.PyExecutionFixtureTestTask) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) AbstractProgressIndicatorBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorBase) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) FileInputStream(java.io.FileInputStream) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure)

Example 19 with IntentionAction

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

the class MavenDependencyCompletionAndResolutionTest method testChooseFileIntentionForSystemDependency.

public void testChooseFileIntentionForSystemDependency() throws Throwable {
    createProjectPom("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<dependencies>" + "  <dependency><caret>" + "    <groupId>xxx</groupId>" + "    <artifactId>xxx</artifactId>" + "    <version>xxx</version>" + "    <scope>system</system>" + "  </dependency>" + "</dependencies>");
    IntentionAction action = getIntentionAtCaret("Choose File");
    assertNotNull(action);
    String libPath = myIndicesFixture.getRepositoryHelper().getTestDataPath("local1/junit/junit/4.0/junit-4.0.jar");
    final VirtualFile libFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(libPath);
    ((ChooseFileIntentionAction) ((IntentionActionWrapper) action).getDelegate()).setFileChooser(() -> new VirtualFile[] { libFile });
    XmlCodeStyleSettings xmlSettings = CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings().getCustomSettings(XmlCodeStyleSettings.class);
    int prevValue = xmlSettings.XML_TEXT_WRAP;
    try {
        // prevent file path from wrapping.
        xmlSettings.XML_TEXT_WRAP = CommonCodeStyleSettings.DO_NOT_WRAP;
        myFixture.launchAction(action);
    } finally {
        xmlSettings.XML_TEXT_WRAP = prevValue;
        ((ChooseFileIntentionAction) ((IntentionActionWrapper) action).getDelegate()).setFileChooser(null);
    }
    MavenDomProjectModel model = MavenDomUtil.getMavenDomProjectModel(myProject, myProjectPom);
    MavenDomDependency dep = model.getDependencies().getDependencies().get(0);
    assertEquals(findPsiFile(libFile), dep.getSystemPath().getValue());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) ChooseFileIntentionAction(org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction) ChooseFileIntentionAction(org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) XmlCodeStyleSettings(com.intellij.psi.formatter.xml.XmlCodeStyleSettings) MavenDomDependency(org.jetbrains.idea.maven.dom.model.MavenDomDependency)

Example 20 with IntentionAction

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

the class MavenDependencyCompletionAndResolutionTest method testNoChooseFileIntentionForNonSystemDependency.

public void testNoChooseFileIntentionForNonSystemDependency() throws Throwable {
    createProjectPom("<groupId>test</groupId>" + "<artifactId>project</artifactId>" + "<version>1</version>" + "<dependencies>" + "  <dependency><caret>" + "    <groupId>xxx</groupId>" + "    <artifactId>xxx</artifactId>" + "    <version>xxx</version>" + "    <scope>compile</system>" + "  </dependency>" + "</dependencies>");
    IntentionAction action = getIntentionAtCaret("Choose File");
    assertNull(action);
}
Also used : ChooseFileIntentionAction(org.jetbrains.idea.maven.dom.intentions.ChooseFileIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction)

Aggregations

IntentionAction (com.intellij.codeInsight.intention.IntentionAction)241 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)31 NotNull (org.jetbrains.annotations.NotNull)22 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