Poly-reference issues via XML DOM API in multi-module project



What is a preferred way to implement a poly-reference (PsiPolyVariantReference) via CustomReferenceConverter (XML DOM Api)?

Consider an example. Let multi-module project (module A and B), each have the foo.xml file:


<bar name="AAA"/>

<qux ref="AAA"/>
<qux ref="BBB"/>


<bar name="AAA"/>
<bar name="BBB"/>

<qux ref="AAA"/>

Element bar with name BBB has a single declaration in a file from module B and single usage in element qux in a file from module A. While, element bar with name AAA has multiple valid declarations in both modules.

In such case, auto resolving via DomResolveConverter wouldn't work (Since, internaly it uses a Map<String, DomElement> which is suitable for a single element with a particular name, so even implementation of MergingFileDescription or usage of ModelMerger are not usefull).

Looks like CustomReferenceConverter is a solution, but what exactly have to be returned from PsiPolyVariantReference.multiResolve method? An XmlAttributeValue, DomTarget wrapped in PomTargetPsiElement, etc.?

After some experementations, turns out that:

1. In both approaches, referencing and highlighting of AAA elements works fine.

However, find usages shows results only from an invoked file.

2. With PomTargetPsiElement approach, BBB element resolving is ok, but reference (at the cursor) is not highlighted and find usages shows only declaration.

3. With XmlAttributeValue approach, find usages works as expected and reference (at the cursor) is highlighted, but highlighting color is a color of write access instead of read access.


So, what is a proprer way to achive that?


P.S. By the way, are there any way to create partial merged model via ModelMerger? To merge not the full files, but only bar elements for instance..

Thanks in advance!





Source code:

public interface Foo extends DomElement {
List<Bar> getBars();

List<Qux> getQuxies();

default @Nullable Bar findBar(@NotNull String name) {
for (Bar bar : getBars()) {
if (name.equals(ElementPresentationManager.getElementName(bar))) {
return bar;
return null;
public interface Bar extends DomElement {
GenericAttributeValue<String> getName();
public interface Qux extends DomElement {
GenericAttributeValue<String> getRef();
public class FooDomFileDescription extends DomFileDescription<Foo> {
public FooDomFileDescription() {
super(Foo.class, "foo");

public boolean isMyFile(@NotNull XmlFile file, @Nullable Module module) {
return file.getName().equals("foo.xml");

/** @apiNote No caching for the sake of clarity */
public static boolean processFooFiles(@NotNull Project project, @NotNull GlobalSearchScope scope,
@NotNull Processor<Foo> processor) {
final var manager = DomManager.getDomManager(project);
return FilenameIndex.processFilesByName("foo.xml", false, true,
it -> {
final var file = manager.getFileElement((XmlFile) it, Foo.class);
return file == null || processor.process(file.getRootElement());
}, scope, project
public class BarCustomReferenceConverter implements CustomReferenceConverter<String> {
public @NotNull PsiReference[] createReferences(GenericDomValue<String> value, PsiElement element, ConvertContext context) {
return new PsiReference[]{new BarReference((XmlAttributeValue) element)};

public static class BarReference extends PsiPolyVariantReferenceBase<XmlAttributeValue> {
public BarReference(@NotNull XmlAttributeValue element) {

/** @apiNote No caching for the sake of clarity */
public @NotNull ResolveResult[] multiResolve(boolean incompleteCode) {
final String nameToSearch = getValue();
final var project = myElement.getProject();
final var scope = myElement.getResolveScope();
final var results = new SmartList<PsiElement>();

FooDomFileDescription.processFooFiles(project, scope,
foo -> {
final Bar bar = foo.findBar(nameToSearch);
if (bar == null) return true;

final PsiElement target = getTarget(bar);
assert target != null;
return true;

return PsiElementResolveResult.createResults(results);

// /** Approach #1: */
// private static @Nullable PsiElement getTarget(@NotNull Bar bar) {
// return bar.getName().getXmlAttributeValue();
// }

/** Approach #2: */
private static @Nullable PsiElement getTarget(@NotNull Bar bar) {
final var target = DomTarget.getTarget(bar);
return target == null ? null : PomService.convertToPsi(target);
1 comment
Comment actions Permalink

ModelMerger approach is most probably not the solution for this case, it's really used if you need to "join" all corresponding files into one big set (affects "everything" in DOM).

DomTarget approach should work - why do you have @Referencing(BarCustomReferenceConverter.class) in Bar#getName()? AFAIU it is the declaration and not a reference to anything?


Please sign in to leave a comment.