DOM error checking

Hi.

I'm trying to get IDEA to highlight errors in my XML document.

According to the "Accessing XML through Intellij IDEA" document, adding @NameValue should be enough for IDEA to highlight duplicates on those attributes marked. Also, adding a @Required annotation should make that attribute to be marked as required as well. None of this is happening on my plugin at the moment for some reason. I override getIdentityScope() on my DomFileDescription and I see that is never been called, so I don't understand why.

Also, I want the following to be marked as an error (IDEA is already resolving 'reference' to 'element' for me)

<element id="myElement"/>
<myTag reference="myElement"/>
<myTag reference="unknownReference"/>     <!-- This should be an error, 'unknownReference' doesn't exist -->

Thanks in advance!

14 comments

I'm working on similar problem.    To mark some attribute  as error  I did:
      - generated DOM model from XSD and adjusted it manually with namespaces etc.
      - (via DomFileDescription ) registered  file description with  proper Schema URI , I use URI for both key and namespace value.
      - created reference contributor and linked  PsiReferenceProvider ( implemented it ) to attribute value in question
      - reference provider creates PsiReference for just attribute value, and to mark attribute as error  just return null as reference

PROFIT:  your attribute value is now highlighted red  and there is even error messsage if you implemented one

0

You must provide an instance of com.intellij.util.xml.highlighting.BasicDomElementsInspection with your root DOM to provide error-highlighting.

See org.jetbrains.android.inspections.AndroidDomInspection as sample (you don't need to override shouldCheckResolveProblems() if highlighting should be applied to all elements).

0

Hey Yann,

It would be nice if your information could be added to http://confluence.jetbrains.com/display/IDEADEV/Accessing+XML+through+IntelliJ+IDEA+DOM
There's currently a TODO posted at the top for exactly this question :)

I wonder if these wiki pages are publically editable? Perhaps users could contribute to the documentation to help improve things over time;

Cheers

0

I'm going to add this bit later today. We welcome any contributions, please post anything you think should be added and we'll review and add it.

Page has been updated/clarified w/r to BasicDomElementsInspection

0

While yo are at it,  can you add some bits on automatic generation of DOM elements from XSD ( and that result must be processed heaviliy  )  and maybe better error messaging
when DOM  can not be created.  This one is not really helpful:


[  25891]  ERROR - currency.PrioritizedFutureTask - Original exception:  
java.lang.IllegalArgumentException: Argument 0 for @NotNull parameter of com/intellij/util/ObjectUtils._assertNotNull must not be null
     at com.intellij.util.ObjectUtils._assertNotNull(ObjectUtils.java)
     at com.intellij.util.ObjectUtils.assertNotNull(ObjectUtils.java:33)
     at com.intellij.util.xml.impl.StaticGenericInfo$1.fun(StaticGenericInfo.java:76)
     at com.intellij.util.xml.impl.StaticGenericInfo$1.fun(StaticGenericInfo.java:73)
     at com.intellij.util.containers.ContainerUtilRt.map2Set(ContainerUtilRt.java:315)
     at com.intellij.util.containers.ContainerUtilRt.map2Set(ContainerUtilRt.java:308)
     at com.intellij.util.containers.ContainerUtil.map2Set(ContainerUtil.java:1912)
     at com.intellij.util.xml.impl.StaticGenericInfo.buildMethodMaps(StaticGenericInfo.java:81)
     at com.intellij.util.xml.impl.DynamicGenericInfo.checkInitialized(DynamicGenericInfo.java:75)
     at com.intellij.util.xml.impl.DynamicGenericInfo.getCustomNameChildrenDescription(DynamicGenericInfo.java:188)
     at com.intellij.util.xml.impl.DomSemContributor$5.fun(DomSemContributor.java:174)
     at com.intellij.util.xml.impl.DomSemContributor$5.fun(DomSemContributor.java:156)
     at com.intellij.semantic.SemServiceImpl$7$1.fun(SemServiceImpl.java:140)
     at com.intellij.semantic.SemServiceImpl$7$1.fun(SemServiceImpl.java:137)
     at com.intellij.semantic.SemServiceImpl.a(SemServiceImpl.java:237)
     at com.intellij.semantic.SemServiceImpl.getSemElements(SemServiceImpl.java:206)
     at com.intellij.semantic.SemService.getSemElement(SemService.java:37)
     at com.intellij.util.xml.impl.DomManagerImpl.getDomHandler(DomManagerImpl.java:403)
     at com.intellij.util.xml.impl.DomManagerImpl.getDomElement(DomManagerImpl.java:381)
     at com.intellij.patterns.DomPatterns$1.accepts(DomPatterns.java:60)
     at com.intellij.patterns.DomPatterns$1.accepts(DomPatterns.java:54)
     at com.intellij.patterns.ElementPatternCondition.accepts(ElementPatternCondition.java:39)
     at com.intellij.patterns.ObjectPattern.accepts(ObjectPattern.java:53)
     at com.intellij.patterns.PatternConditionPlus.process(PatternConditionPlus.java:45)
     at com.intellij.patterns.PatternConditionPlus.process(PatternConditionPlus.java:26)
     at com.intellij.patterns.TreeElementPattern$4.processValues(TreeElementPattern.java:124)
     at com.intellij.patterns.PatternConditionPlus.accepts(PatternConditionPlus.java:41)
     at com.intellij.patterns.ElementPatternCondition.accepts(ElementPatternCondition.java:39)
     at com.intellij.patterns.ObjectPattern.accepts(ObjectPattern.java:53)
     at com.intellij.psi.impl.source.resolve.reference.NamedObjectProviderBinding.a(NamedObjectProviderBinding.java:111)
     at com.intellij.psi.impl.source.resolve.reference.NamedObjectProviderBinding.addAcceptableReferenceProviders(NamedObjectProviderBinding.java:68)
     at com.intellij.psi.impl.source.resolve.reference.PsiReferenceRegistrarImpl.getPairsByElement(PsiReferenceRegistrarImpl.java:177)
     at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.doGetReferencesFromProviders(ReferenceProvidersRegistryImpl.java:88)
     at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:65)
     at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:53)
     at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.getReferences(XmlAttributeValueImpl.java:99)
     at com.intellij.psi.impl.SharedPsiElementImplUtil.a(SharedPsiElementImplUtil.java:67)
     at com.intellij.psi.impl.SharedPsiElementImplUtil.findReferenceAt(SharedPsiElementImplUtil.java:49)
     at com.intellij.psi.impl.SharedPsiElementImplUtil.findReferenceAt(SharedPsiElementImplUtil.java:63)
     at com.intellij.psi.impl.source.tree.CompositePsiElement.findReferenceAt(CompositePsiElement.java:116)
     at com.intellij.psi.SingleRootFileViewProvider.a(SingleRootFileViewProvider.java:419)
     at com.intellij.psi.SingleRootFileViewProvider.findReferenceAt(SingleRootFileViewProvider.java:391)
     at com.intellij.psi.impl.source.PsiFileImpl.findReferenceAt(PsiFileImpl.java:567)
     at com.intellij.codeInsight.TargetElementUtilBase.findReference(TargetElementUtilBase.java:114)
     at com.intellij.codeInsight.TargetElementUtilBase.getReferenceOrReferencedElement(TargetElementUtilBase.java:302)
     at com.intellij.codeInsight.TargetElementUtil.getReferenceOrReferencedElement(TargetElementUtil.java:97)
     at com.intellij.codeInsight.TargetElementUtilBase.findTargetElement(TargetElementUtilBase.java:201)
     at com.intellij.codeInsight.TargetElementUtil.findTargetElement(TargetElementUtil.java:62)
     at com.intellij.codeInsight.daemon.impl.IdentifierHighlighterPass.doCollectInformation(IdentifierHighlighterPass.java:97)
     at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:62)
     at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass$1$1.run(PassExecutorService.java:357)
     at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1182)
     at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass$1.run(PassExecutorService.java:348)
     at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:226)
     at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.a(PassExecutorService.java:345)
     at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:321)
     at com.intellij.concurrency.JobLauncherImpl$3.call(JobLauncherImpl.java:145)
     at com.intellij.concurrency.JobLauncherImpl$3.call(JobLauncherImpl.java:142)
     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
     at java.util.concurrent.FutureTask.run(FutureTask.java:166)
     at com.intellij.concurrency.PrioritizedFutureTask.access$101(PrioritizedFutureTask.java:31)
     at com.intellij.concurrency.PrioritizedFutureTask$1.run(PrioritizedFutureTask.java:70)
     at com.intellij.concurrency.PrioritizedFutureTask.run(PrioritizedFutureTask.java:113)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
     at java.lang.Thread.run(Thread.java:722)
[  76475]  ERROR - n.impl.GeneralHighlightingPass - Argument 0 for @NotNull parameter of com/intellij/util/ObjectUtils._assertNotNull must not be null



( Helpful message would include offending class / method )

0

Thank you for your responses! I'll try that tomorrow.

While you are improving the docs, can you also add CustomReferenceConverter to it? In my experience, automatic referencing doesn't work unless you implement that interface in your Converter, although the document only says to implement the latter.

0

Could you please create an issue http://youtrack.jetbrains.com/issues/IDEA and attach your XSD you're using for creating DOM model (you can set visibility to idea-developers to hide it from public). Thanks.

0

You don't need to implement CustomReferenceConverter unless you want to create custom PSI references. Please post an example where your "plain" Converter does not work.

0

I have the following element:

<element property="my.property"/>

'my.property' is a property from a .properties file. I wanted to Cmd+Click on it to go to the .properties file.

I implemented a Converter<PropertyReference>, but before implementing CustomReferenceConverter, the converter did nothing as far as I know.

0

PropertyReference _is_ a PsiReference and should not be used as GenericAttributeValue type here.

Two possibilities:
1) change to GAV<IProperty> and use Converter
2) use CustomReferenceConverter and provide PropertyReference from there directly (easiest way)

0

Hi Yann,

I've tried your better solution (implement Converter<IProperty> without CustomReferenceConverter) and now I get proper errors on invalid properties (red wavy line instead of red text), but Cmd+Click doesn't go to the declaration unless I also implement the CustomReferenceConverter (after doing that, errors are now red text instead red wavy line). Is that the expected behavior?

Thanks again :)

0

I see, please try implementing ResolvingConverter instead and override ResolvingConverter#getPsiElement returning com.intellij.lang.properties.IProperty#getPsiElement

0

Yes, that one worked, although when doing that, I lost the auto completion, as I'm returning an empty list on the getVariants() function. Previously, that worked automatically, so unless ResolvingConverter provides me with something Converter + CustomReferenceConverter does not, I'll continue with the latter :)

Thank you again, Yann!

0

Please sign in to leave a comment.