Referencing a DbDataSource

I am writing a plugin for a PHP framework that simplifies work with the database.

When the user is viewing the database configuration file, I'd like them to be able to Ctrl+Click on a database name within a PHP string literal and be taken to the schema in the Database Tools window. Is this possible?

I tried the following. However, dataSource.getReference() always returns null.

public class DatabaseGroupReferenceContributor extends PsiReferenceContributor {
@Override
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar psiReferenceRegistrar) {
final PsiElementPattern.Capture<PsiElement> pattern = psiElement()
.inVirtualFile(virtualFile().withName(string().matches("database.php")));
psiReferenceRegistrar.registerReferenceProvider(
pattern,
new PsiReferenceProvider() {
@NotNull
@Override
public PsiReference[] getReferencesByElement(
@NotNull PsiElement psiElement,
@NotNull ProcessingContext processingContext) {
if (psiElement instanceof StringLiteralExpression) {
String nameInCode = ((StringLiteralExpression) psiElement).getContents();
MyDatabaseService service = ServiceManager.getService(psiElement.getProject(), MyDatabaseService.class);
for (DbDataSource dataSource : service.getDataSources()) {
JBIterable<? extends DasNamespace> schemas = DasUtil.getSchemas(dataSource);
for (DasNamespace schema : schemas) {
String name = schema.getName();
if (name.equals(nameInCode)) {
return new PsiReference[]{
dataSource.getReference()
};
}
}
}
}
return new PsiReference[0];
}
}
);
}
}
2 comments
Comment actions Permalink

First, please make your registration more specific

registrar.registerReferenceProvider(
psiElement().withLanguage(PhpLanguage.INSTANCE).withElementType(PhpElementTypes.STRING),
new PsiReferenceProvider(){....}, PsiReferenceRegistrar.DEFAULT_PRIORITY);
0
Comment actions Permalink

Second, return from getReferencesByElement, should include the references *you create*, eg

new MyDSReference(DS)...

Where MyDSReference has resolve() & isReferenceTo() correctly implemented, accepting and resolving to DS item

0

Please sign in to leave a comment.