Search in sources :

Example 6 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class HtmlUnknownTagInspectionBase method checkTag.

@Override
protected void checkTag(@NotNull final XmlTag tag, @NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(tag instanceof HtmlTag) || !XmlHighlightVisitor.shouldBeValidated(tag)) {
        return;
    }
    XmlElementDescriptor descriptorFromContext = XmlUtil.getDescriptorFromContext(tag);
    PsiElement parent = tag.getParent();
    XmlElementDescriptor parentDescriptor = parent instanceof XmlTag ? ((XmlTag) parent).getDescriptor() : null;
    XmlElementDescriptor ownDescriptor = isAbstractDescriptor(descriptorFromContext) ? tag.getDescriptor() : descriptorFromContext;
    if (isAbstractDescriptor(ownDescriptor) || (parentDescriptor instanceof HtmlElementDescriptorImpl && ownDescriptor instanceof HtmlElementDescriptorImpl && isAbstractDescriptor(descriptorFromContext))) {
        final String name = tag.getName();
        if (!isCustomValuesEnabled() || !isCustomValue(name)) {
            final AddCustomHtmlElementIntentionAction action = new AddCustomHtmlElementIntentionAction(TAG_KEY, name, XmlBundle.message("add.custom.html.tag", name));
            // todo: support "element is not allowed" message for html5
            // some tags in html5 cannot be found in xhtml5.xsd if they are located in incorrect context, so they get any-element descriptor (ex. "canvas: tag)
            final String message = isAbstractDescriptor(ownDescriptor) ? XmlErrorMessages.message("unknown.html.tag", name) : XmlErrorMessages.message("element.is.not.allowed.here", name);
            final PsiElement startTagName = XmlTagUtil.getStartTagNameElement(tag);
            assert startTagName != null;
            final PsiElement endTagName = XmlTagUtil.getEndTagNameElement(tag);
            List<LocalQuickFix> quickfixes = new ArrayList<>();
            quickfixes.add(action);
            if (isOnTheFly) {
                PsiFile file = startTagName.getContainingFile();
                if (file instanceof XmlFile) {
                    quickfixes.add(XmlQuickFixFactory.getInstance().createNSDeclarationIntentionFix(startTagName, "", null));
                }
                // People using non-HTML as their template data language (but having not changed this in the IDE)
                // will most likely see 'unknown html tag' error, because HTML is usually the default.
                // So if they check quick fixes for this error they'll discover Change Template Data Language feature.
                ContainerUtil.addIfNotNull(quickfixes, createChangeTemplateDataFix(file));
            }
            if (HtmlUtil.isHtml5Tag(name) && !HtmlUtil.hasNonHtml5Doctype(tag)) {
                quickfixes.add(new SwitchToHtml5WithHighPriorityAction());
            }
            ProblemHighlightType highlightType = tag.getContainingFile().getContext() == null ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.INFORMATION;
            if (startTagName.getTextLength() > 0) {
                holder.registerProblem(startTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
            if (endTagName != null) {
                holder.registerProblem(endTagName, message, highlightType, quickfixes.toArray(new LocalQuickFix[quickfixes.size()]));
            }
        }
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) HtmlTag(com.intellij.psi.html.HtmlTag) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) PsiFile(com.intellij.psi.PsiFile) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 7 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class MavenDomAnnotator method addProblems.

private static void addProblems(DomElement element, MavenDomProjectModel model, DomElementAnnotationHolder holder, MavenProjectProblem.ProblemType... types) {
    MavenProject mavenProject = MavenDomUtil.findProject(model);
    if (mavenProject != null) {
        for (MavenProjectProblem each : mavenProject.getProblems()) {
            MavenProjectProblem.ProblemType type = each.getType();
            if (!Arrays.asList(types).contains(type))
                continue;
            VirtualFile problemFile = LocalFileSystem.getInstance().findFileByPath(each.getPath());
            LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
            if (problemFile != null && !Comparing.equal(mavenProject.getFile(), problemFile)) {
                fixes = new LocalQuickFix[] { new OpenProblemFileFix(problemFile) };
            }
            holder.createProblem(element, HighlightSeverity.ERROR, each.getDescription(), fixes);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenProjectProblem(org.jetbrains.idea.maven.model.MavenProjectProblem) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix)

Example 8 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class XsltValidator method checkUnusedVariable.

public static void checkUnusedVariable(XsltVariable variable, ProblemsHolder holder) {
    if (variable instanceof XsltParameter) {
        if (((XsltParameter) variable).isAbstract()) {
            return;
        }
    } else {
        if (variable.isVoid()) {
            return;
        }
    }
    final XmlTag tag = variable.getTag();
    final XmlTag templateTag = XsltCodeInsightUtil.getTemplateTag(tag, false);
    if (templateTag == null) {
        return;
    }
    final XmlAttribute attribute = tag.getAttribute("name");
    if (attribute == null) {
        return;
    }
    final PsiElement token = XsltSupport.getAttValueToken(attribute);
    if (token == null) {
        return;
    }
    final SearchScope scope = new LocalSearchScope(templateTag);
    final Query<PsiReference> refs = ReferencesSearch.search(variable, scope, false);
    if (isUnused(variable, refs)) {
        final String name = variable.getName();
        assert name != null;
        final LocalQuickFix[] fixes;
        if (variable instanceof XsltParameter) {
            fixes = new LocalQuickFix[] { new DeleteUnusedParameterFix(name, (XsltParameter) variable) };
        } else {
            fixes = new LocalQuickFix[] { new DeleteUnusedVariableFix(name, variable) };
        }
        holder.registerProblem(token, ((DeleteUnusedElementBase) fixes[0]).getType() + " '" + name + "' is never used", ProblemHighlightType.LIKE_UNUSED_SYMBOL, fixes);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DeleteUnusedVariableFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedVariableFix) PsiReference(com.intellij.psi.PsiReference) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) DeleteUnusedParameterFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedParameterFix) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) DeleteUnusedElementBase(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedElementBase) XmlTag(com.intellij.psi.xml.XmlTag)

Example 9 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class PropertyReference method getQuickFixes.

public LocalQuickFix[] getQuickFixes() {
    List<PropertiesFile> propertiesFiles = retrievePropertyFilesByBundleName(myBundleName, getElement());
    LocalQuickFix fix = PropertiesQuickFixFactory.getInstance().createCreatePropertyFix(myElement, myKey, propertiesFiles);
    return new LocalQuickFix[] { fix };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 10 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix 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)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)59 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)11 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)10 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)10 ASTNode (com.intellij.lang.ASTNode)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 XmlTag (com.intellij.psi.xml.XmlTag)6 PsiFile (com.intellij.psi.PsiFile)5 PsiReference (com.intellij.psi.PsiReference)5 ArrayList (java.util.ArrayList)5 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)4 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)3 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)3 Pair (com.intellij.openapi.util.Pair)3 TextRange (com.intellij.openapi.util.TextRange)3 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3