Contribute a reference between two existing languages

I am trying to connect two existing PSIElements via reference: XML attributes that follow a given pattern should be linked to their translations, found in properties files. Debugging shows, that the I18nReference objects are created the right way, the multiResolve function is called, and it also finds the right Property - PsiElements which are then returned. Sadly though, the IDE does not display anything. I cannot click on the XML text to navigate to the i18n properties. What am I missing? What could be the cause of the problem?

Here is the code of my I18nReference class:

public class I18nReference extends PsiPolyVariantReferenceBase<PsiElement> {
private final String key;

public I18nReference(@NotNull PsiElement element, String key, TextRange rangeInElement) {
super(element, rangeInElement, false);
this.key = key;
}

@NotNull @Override public ResolveResult[] multiResolve(boolean incompleteCode) {
return PsiElementResolveResult.createResults(findProperties(myElement.getProject(), key));
}

public static List<Property> findProperties(Project project, String key) {
List<Property> result = new ArrayList<>();

forAllProperties(project, property -> {
if (key.equals(property.getKey())) {
result.add(property);
}
});

return result;
}

@NotNull @Override public Object[] getVariants() {
Project project = myElement.getProject();
List<LookupElement> variants = new ArrayList<>();
forAllProperties(project, property -> {
if (property.getKey() != null && property.getKey().length() > 0) {
variants.add(LookupElementBuilder.create(property).
withTypeText(property.getContainingFile().getName())
);
}
});
return variants.toArray();
}

// finding all instances of Property psi elements in this project
public static void forAllProperties(Project project, Consumer<Property> handler) {
Collection<VirtualFile> virtualFiles =
FileTypeIndex.getFiles(PropertiesFileType.INSTANCE, GlobalSearchScope.allScope(project));
for (VirtualFile virtualFile : virtualFiles) {
PropertiesFileImpl propertiesFile = (PropertiesFileImpl) PsiManager.getInstance(project).findFile(virtualFile);
if (propertiesFile != null) {
Collection<Property> properties = PsiTreeUtil.findChildrenOfType(propertiesFile, Property.class);
for (Property property : properties) {
handler.accept(property);
}
}
}
}
1
1 comment

Please show code registering this reference. Also, you might want to use com.intellij.lang.properties.psi.PropertyKeyIndex in your reference or consider extending com.intellij.lang.properties.references.PropertyReferenceBase

0

Please sign in to leave a comment.