extracting XmlPathReferenceInspection

This commit is contained in:
Dmitry Avdeev
2012-09-04 16:31:20 +04:00
parent 0e56c6af42
commit d708f1a245
20 changed files with 173 additions and 64 deletions
@@ -136,10 +136,10 @@ public class ProblemsHolder {
}
public void registerProblem(@NotNull PsiReference reference) {
registerProblem(reference, inresolvedReferenceMessage(reference), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
registerProblem(reference, unresolvedReferenceMessage(reference), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
public static String inresolvedReferenceMessage(PsiReference reference) {
public static String unresolvedReferenceMessage(PsiReference reference) {
String message;
if (reference instanceof EmptyResolveMessageProvider) {
String pattern = ((EmptyResolveMessageProvider)reference).getUnresolvedMessagePattern();
@@ -26,14 +26,12 @@ public abstract class PathReferenceProviderBase implements PathReferenceProvider
final TextRange range = manipulator.getRangeInElement(psiElement);
int offset = range.getStartOffset();
int endOffset = range.getEndOffset();
boolean dynamicContext = false;
final String elementText = psiElement.getText();
for (DynamicContextProvider provider: Extensions.getExtensions(DynamicContextProvider.EP_NAME)) {
final int dynamicOffset = provider.getOffset(psiElement, offset, elementText);
if (dynamicOffset == -1) {
return false;
} else if (dynamicOffset != offset) {
dynamicContext = true;
offset = dynamicOffset;
}
}
@@ -44,7 +42,7 @@ public abstract class PathReferenceProviderBase implements PathReferenceProvider
}
try {
final String text = elementText.substring(offset, endOffset);
return createReferences(psiElement, offset, text, references, soft || dynamicContext);
return createReferences(psiElement, offset, text, references, soft);
} catch (StringIndexOutOfBoundsException e) {
LOG.error("Cannot process string: '" + psiElement.getParent().getParent().getText() + "'", e);
return false;
@@ -359,7 +359,7 @@ public class FileReference implements FileReferenceOwner, PsiPolyVariantReferenc
@Override
public PsiFileSystemItem resolve() {
ResolveResult[] resolveResults = multiResolve(false);
return resolveResults.length == 1 ? (PsiFileSystemItem)resolveResults[0].getElement() : null;
return resolveResults.length == 1 ? (PsiFileSystemItem)resolveResults[0].getElement() : null;
}
@Nullable
@@ -533,11 +533,7 @@ public class FileReference implements FileReferenceOwner, PsiPolyVariantReferenc
protected PsiElement rename(final String newName) throws IncorrectOperationException {
final TextRange range = new TextRange(myFileReferenceSet.getStartInElement(), getRangeInElement().getEndOffset());
PsiElement element = getElement();
final ElementManipulator<PsiElement> manipulator = CachingReference.getManipulator(element);
if (manipulator == null) {
throw new IncorrectOperationException("Manipulator not defined for: " + element + " of class " + element.getClass());
}
return manipulator.handleContentChange(element, range, newName);
return CachingReference.getManipulator(element).handleContentChange(element, range, newName);
}
@Override
@@ -559,7 +555,7 @@ public class FileReference implements FileReferenceOwner, PsiPolyVariantReferenc
public String getUnresolvedMessagePattern() {
return LangBundle.message("error.cannot.resolve")
+ " " + (isLast() ? LangBundle.message("terms.file") : LangBundle.message("terms.directory"))
+ " ''" + StringUtil.escapePattern(StringUtil.notNullize(decode(getCanonicalText()))) + "''";
+ " '" + StringUtil.escapePattern(StringUtil.notNullize(decode(getCanonicalText()))) + "'";
}
public final boolean isLast() {
@@ -57,7 +57,7 @@ html.inspections.form.input.without.label=Form input without an associated label
html.inspections.create.label.from.text.before.action=Create a new label from the text before {0} element
html.inspections.create.label.from.text.after.action=Create a new label from the text after {0} element
html.inspections.create.new.label=Associate a new label with {0} element
html.inspections.unknown.target=Unknown href target
html.inspections.unknown.target=Unresolved href target
html.annotators.non.existent.internet.resource=Cannot find resource "{0}"
@@ -355,6 +355,13 @@
<localInspection language="XML" shortName="XmlUnusedNamespaceDeclaration" displayName="Unused XML schema declaration"
groupKey="xml.inspections.group.name" bundle="messages.XmlBundle" enabledByDefault="true" level="WARNING"
implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection"/>
<localInspection language="XML" implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlPathReferenceInspection"
displayName="File path resolving in XML"
groupName="XML" enabledByDefault="true" level="ERROR"/>
<localInspection language="XML" implementationClass="com.intellij.codeInsight.daemon.impl.analysis.HtmlPathReferenceInspection"
displayName="File path resolving in XML"
groupName="XML" enabledByDefault="true" level="WARNING"/>
<globalInspection shortName="XmlHighlighting" displayName="XML highlighting" groupName="General" enabledByDefault="true" level="ERROR"
implementationClass="com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitorBasedInspection"/>
@@ -24,6 +24,7 @@ package com.intellij.lang.ant;
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.XmlPathReferenceInspection;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.lang.ant.dom.AntResolveInspection;
import com.intellij.lang.ant.validation.AntDuplicateTargetsInspection;
@@ -137,6 +138,8 @@ public class AntHighlightingTest extends DaemonAnalyzerTestCase {
@Override
protected LocalInspectionTool[] configureLocalInspectionTools() {
return new LocalInspectionTool[]{new AntDuplicateTargetsInspection(), new AntResolveInspection()};
return new LocalInspectionTool[]{new AntDuplicateTargetsInspection(),
new AntResolveInspection(),
new XmlPathReferenceInspection()};
}
}
@@ -15,13 +15,12 @@
*/
package com.theoryinpractice.testng.referenceContributor;
import com.intellij.codeInsight.daemon.impl.analysis.XmlPathReferenceInspection;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ui.UIUtil;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
@@ -73,10 +72,11 @@ public class TestNGSuiteTest extends LightCodeInsightFixtureTestCase {
myFixture.addClass("package org.testng.annotations; public @interface Test {}");
myFixture.addClass("package o; @Test public class MyTest { public void testMe(){} }");
myFixture.addFileToProject("subPack/test-unit.xml", "<suite>" +
"<test>" +
"<classes></classes>" +
"</test>" +
"</suite>");
"<test>" +
"<classes></classes>" +
"</test>" +
"</suite>");
myFixture.enableInspections(new XmlPathReferenceInspection());
myFixture.testHighlighting("testng.xml");
}
catch (Exception e) {
+1
View File
@@ -23,6 +23,7 @@
<orderEntry type="module" module-name="jsp-openapi" />
<orderEntry type="library" name="TestNG" level="project" />
<orderEntry type="module" module-name="java-indexing-api" />
<orderEntry type="module" module-name="xml" scope="TEST" />
</component>
<component name="copyright">
<Base>
@@ -17,13 +17,15 @@ package com.intellij.util.xml.highlighting;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.lang.annotation.HighlightSeverity;
import com.intellij.psi.PsiReference;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.xml.GenericDomValue;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NotNull;
/**
@@ -33,7 +35,7 @@ class DomElementResolveProblemDescriptorImpl extends DomElementProblemDescriptor
@NotNull private final PsiReference myReference;
public DomElementResolveProblemDescriptorImpl(@NotNull final GenericDomValue domElement, @NotNull final PsiReference reference, LocalQuickFix... quickFixes) {
super(domElement, XmlHighlightVisitor.getErrorDescription(reference), HighlightSeverity.ERROR, quickFixes);
super(domElement, reference instanceof FileReference ? ProblemsHolder.unresolvedReferenceMessage(reference) : XmlHighlightVisitor.getErrorDescription(reference), HighlightSeverity.ERROR, quickFixes);
myReference = reference;
}
@@ -0,0 +1,5 @@
<html>
<body>
Highlights unresolved file references in HTML.
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
Highlights unresolved file references in XML.
</body>
</html>
@@ -0,0 +1,28 @@
/*
* Copyright 2000-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.analysis;
/**
* @author Dmitry Avdeev
* Date: 9/3/12
*/
public class HtmlPathReferenceInspection extends XmlPathReferenceInspection {
@Override
protected boolean isForHtml() {
return true;
}
}
@@ -41,6 +41,7 @@ import com.intellij.profile.codeInspection.InspectionProjectProfileManager;
import com.intellij.psi.*;
import com.intellij.psi.html.HtmlTag;
import com.intellij.psi.impl.source.SourceTreeToPsiMap;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceOwner;
import com.intellij.psi.meta.PsiMetaData;
import com.intellij.psi.templateLanguages.OuterLanguageElement;
import com.intellij.psi.tree.IElementType;
@@ -556,9 +557,10 @@ public class XmlHighlightVisitor extends XmlElementVisitor implements HighlightV
}
@Override public void visitXmlAttributeValue(XmlAttributeValue value) {
checkReferences(value);
final PsiElement parent = value.getParent();
if (!(parent instanceof XmlAttribute)) {
checkReferences(value);
return;
}
@@ -577,11 +579,8 @@ public class XmlHighlightVisitor extends XmlElementVisitor implements HighlightV
getTagProblemInfoType(tag),
value,
error));
return;
}
}
checkReferences(value);
}
private void checkReferences(PsiElement value) {
@@ -594,7 +593,7 @@ public class XmlHighlightVisitor extends XmlElementVisitor implements HighlightV
for (int i = start; i < references.length; ++i) {
PsiReference reference = references[i];
ProgressManager.checkCanceled();
if (reference == null) {
if (reference instanceof FileReferenceOwner) {
continue;
}
if (!hasBadResolve(reference, false)) {
@@ -0,0 +1,85 @@
/*
* Copyright 2000-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.XmlSuppressableInspectionTool;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiReference;
import com.intellij.psi.XmlElementVisitor;
import com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReferenceOwner;
import com.intellij.psi.xml.*;
import com.intellij.xml.util.HtmlUtil;
import org.jetbrains.annotations.NotNull;
/**
* @author Dmitry Avdeev
* Date: 9/3/12
*/
public class XmlPathReferenceInspection extends XmlSuppressableInspectionTool {
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new XmlElementVisitor() {
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
checkRefs(value, holder, isOnTheFly);
}
@Override
public void visitXmlDoctype(XmlDoctype xmlDoctype) {
checkRefs(xmlDoctype, holder, isOnTheFly);
}
@Override
public void visitXmlTag(XmlTag tag) {
checkRefs(tag, holder, isOnTheFly);
}
};
}
private void checkRefs(PsiElement element, ProblemsHolder holder, boolean isOnTheFly) {
PsiReference[] references = element.getReferences();
for (PsiReference reference : references) {
if (!(reference instanceof FileReferenceOwner) ||
XmlHighlightVisitor.skipValidation(element)) {
continue;
}
boolean isHtml = HtmlUtil.isHtmlTagContainingFile(element);
if (isHtml ^ isForHtml()) {
continue;
}
if (isHtml) {
PsiElement parent = element.getParent();
if (parent instanceof XmlAttribute && "href".equals(((XmlAttribute)parent).getLocalName())) {
// handled by HtmlUnknownTargetInspection
continue;
}
}
if (XmlHighlightVisitor.hasBadResolve(reference, false)) {
holder.registerProblem(reference, ProblemsHolder.unresolvedReferenceMessage(reference),
isHtml ? ProblemHighlightType.GENERIC_ERROR_OR_WARNING : ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
}
protected boolean isForHtml() {
return false;
}
}
@@ -15,11 +15,9 @@
*/
package com.intellij.codeInspection.htmlInspections;
import com.intellij.codeInsight.daemon.EmptyResolveMessageProvider;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.xml.XmlAttribute;
import com.intellij.psi.xml.XmlAttributeValue;
@@ -28,8 +26,6 @@ import com.intellij.xml.util.HtmlUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.text.MessageFormat;
/**
* @author Eugene.Kudelevsky
*/
@@ -57,25 +53,8 @@ public class HtmlUnknownTargetInspection extends HtmlLocalInspectionTool {
if (valueElement != null) {
PsiReference[] refs = valueElement.getReferences();
for (PsiReference ref : refs) {
if (ref instanceof EmptyResolveMessageProvider && XmlHighlightVisitor.hasBadResolve(ref, false)) {
PsiElement element = ref.getElement();
if (element != null && element.getTextLength() == 0) {
continue;
}
String messagePattern = ((EmptyResolveMessageProvider)ref).getUnresolvedMessagePattern();
String description;
try {
description = MessageFormat.format(messagePattern, ref.getCanonicalText());
}
catch (IllegalArgumentException ex) {
description = messagePattern;
}
if (description != null) {
description += " #loc";
}
holder.registerProblem(ref, description, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
if (XmlHighlightVisitor.hasBadResolve(ref, false)) {
holder.registerProblem(ref, ProblemsHolder.unresolvedReferenceMessage(ref), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
@@ -15,9 +15,7 @@
*/
package com.intellij.codeInspection.htmlInspections;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitorBasedInspection;
import com.intellij.codeInsight.daemon.impl.analysis.XmlUnboundNsPrefixInspection;
import com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection;
import com.intellij.codeInsight.daemon.impl.analysis.*;
import com.intellij.codeInspection.InspectionToolProvider;
import com.intellij.xml.util.*;
@@ -40,7 +38,9 @@ public class XmlInspectionToolProvider implements InspectionToolProvider {
HtmlUnknownAttributeInspection.class,
XmlUnboundNsPrefixInspection.class,
XmlUnusedNamespaceInspection.class,
XmlHighlightVisitorBasedInspection.class
XmlHighlightVisitorBasedInspection.class,
XmlPathReferenceInspection.class,
HtmlPathReferenceInspection.class
};
}
}
@@ -16,7 +16,7 @@
package com.intellij.lang.dtd;
import com.intellij.ide.highlighter.XmlFileHighlighter;
import com.intellij.lang.Language;
import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.fileTypes.SingleLazyInstanceSyntaxHighlighterFactory;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
@@ -25,12 +25,12 @@ import org.jetbrains.annotations.NotNull;
/**
* @author max
*/
public class DTDLanguage extends Language {
public class DTDLanguage extends XMLLanguage {
public static final DTDLanguage INSTANCE = new DTDLanguage();
protected DTDLanguage() {
super("DTD", "text/dtd", "text/x-dtd");
super(XMLLanguage.INSTANCE, "DTD", "text/dtd", "text/x-dtd");
SyntaxHighlighterFactory.LANGUAGE_FACTORY.addExplicitExtension(this, new SingleLazyInstanceSyntaxHighlighterFactory() {
@NotNull
protected SyntaxHighlighter createHighlighter() {
@@ -19,7 +19,6 @@ package com.intellij.xml.util;
import com.intellij.codeHighlighting.HighlightDisplayLevel;
import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
import com.intellij.codeInsight.dataflow.map.MapSemilattice;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInspection.*;
@@ -29,7 +28,6 @@ import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.xml.*;
@@ -110,7 +108,7 @@ public class CheckDtdReferencesInspection extends XmlSuppressableInspectionTool
final String image = ref.getCanonicalText();
if (image.equals("-") || image.equals("O")) continue;
}
holder.registerProblem(ref, XmlHighlightVisitor.getErrorDescription(ref), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
holder.registerProblem(ref);
}
}
}
@@ -5,6 +5,7 @@ import com.intellij.codeInsight.completion.CodeCompletionHandlerBase;
import com.intellij.codeInsight.completion.CompletionType;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor;
import com.intellij.codeInsight.daemon.impl.analysis.XmlPathReferenceInspection;
import com.intellij.codeInsight.daemon.impl.analysis.XmlUnboundNsPrefixInspection;
import com.intellij.codeInsight.daemon.impl.quickfix.AddXsiSchemaLocationForExtResourceAction;
import com.intellij.codeInsight.lookup.LookupManager;
@@ -953,7 +954,8 @@ public class XmlHighlightingTest extends DaemonAnalyzerTestCase {
new RequiredAttributesInspection(),
new XmlDuplicatedIdInspection(),
new CheckDtdReferencesInspection(),
new XmlUnboundNsPrefixInspection()
new XmlUnboundNsPrefixInspection(),
new XmlPathReferenceInspection()
};
}
@@ -1588,6 +1590,7 @@ public class XmlHighlightingTest extends DaemonAnalyzerTestCase {
doTestWithLocations(null, "xsd");
}
@HighlightingFlags(HighlightingFlag.SkipExternalValidation)
public void testDtdElementRefs() throws Exception {
doTestWithLocations(
new String[] [] {
@@ -10,7 +10,7 @@
http://schemas.niku.com/2002/service service-2002-02.xsd
">
<page id="calendar.actionitemProperties" space="calendar.organizerHome" template="union.appPage">
<xi:include href="<error>portlets</error>/<error>actionitemTOC.xml</error>"/>
<<error descr="An 'include' failed, and no 'fallback' element was found.">xi:include</error> href="<error descr="Cannot resolve directory 'portlets'">portlets</error>/<error descr="Cannot resolve file 'actionitemTOC.xml'">actionitemTOC.xml</error>"/>
<portlet id="calendar.actionitemProperties" view="actionitem/actionitemProperties.xsl"/>
<pageAction id="calendar.actionitemProperties" policyId="">
<retrieveService type="xbl" componentId="calendar" description="actionitem/actionitemProperties.xbl" portletId="calendar.actionitemProperties"/>