PsiReference in yaml file works differently when i use it thought ReferenceInjector and when i use PsiReferenceContributor.


Hi. My question about PsiReference. I try to use it in yaml file, and don't fully understand how it works. It works differently when i use it thought ReferenceInjector and when i use PsiReferenceContributor.
I prepared simple one class example here
Main question is

- key: A1
- key: A2
  link: A1
- key: A3
  link: A4 # I want error here


When reference injected across ReferenceInjector reference A4 show error.
When reference injected across PsiReferenceContributor on A4 no errors.
How can I make automation reference injection that will show error on invalid links?

1 comment
Comment actions Permalink

I can't reproduce "it works with Injector, but not with Contributor" (and didn't expect it would make a difference).

Actually, highlighting unresolved references must be performed by the plugin itself. There is no "automatic highlighting" unless some other inspection is - by accident - also processing your references and adding highlights for them. You can use Annotator or Inspector for such highlighting, see

Notes: Please upgrade id 'org.jetbrains.intellij' version '0.6.5' to 0.7.2 in Gradle

The actual resolving should occur _inside_ com.icthh.xm.demoplugin.LinkTypeKeyReference#resolve method, not in PsiReferenceProvider, also some cleanup. You can use getVariants() here to quickly visually test your reference is working at expected range.

class YamlPsiReferenceContributor: PsiReferenceContributor() {
override fun registerReferenceProviders(registrar: PsiReferenceRegistrar) {
object : PsiReferenceProvider() {
override fun getReferencesByElement(
element: PsiElement,
context: ProcessingContext
): Array<PsiReference> {
return arrayOf(LinkTypeKeyReference(element))

private fun getPattern() = psiElement<YAMLScalar>().withParent(

inline fun <reified T : PsiElement> psiElement() = psiElement(


class LinkTypeKeyReference(target:PsiElement):
EmptyResolveMessageProvider {
override fun resolve() = findReferenceTarget(element)
override fun getVariants(): Array<Any> {
return arrayOf("1","2","3")

override fun getUnresolvedMessagePattern(): String = "Link with value $value not found"

Please sign in to leave a comment.