How to make PsiReference target specific file offset

Hi,

I am trying to create my custom PsiReference, that should target a specific file and open it at a specified position. How can I do that?

Translate('0607')

I have a ReferenceContributor that extracts the 0607 number from that php function. I managed to create a reference, which opens the correct file in the file system, but I can't manage to move the cursor to a position that contains this number. This is how the target file looks like:

$Lng['0606'] = 'Registrovat';
$Lng['0607'] = 'Odešle vyplněné údaje k registraci';
$Lng['0608'] = 'Odhlásit';

I tried to follow these instructions, but with no success. This is my curent code:

class LanguageReference(element: PsiElement, path: String) : BasicReference(element) {
init {
val project = myElement.project
val languageCode = myElement.text.substring(1, myElement.textLength - 1)
val virtualFile = LocalFileSystem.getInstance().findFileByPath(path)
val psiFile = virtualFile?.let { PsiManager.getInstance(project).findFile(it) }

psiFile?.let {
val text = it.text
if (text.contains(languageCode)) {
val langCodeOffset = text.indexOf(languageCode, ignoreCase = true)
val elementAtOffset = LanguagePsiElement(psiFile.virtualFile, langCodeOffset)
result = elementAtOffset // result is passed to resolve() in BasicReference class
}
}
}
}

class LanguagePsiElement(val file: VirtualFile, val offset: Int) : FakePsiElement() {
override fun getParent(): PsiFile? = null

override fun navigate(requestFocus: Boolean) {
PsiNavigationSupport.getInstance().createNavigatable(project, file, offset).navigate(requestFocus)
}
}
0
1 comment

Does your LanguagePsiElement#canNavigate return true?

0

Please sign in to leave a comment.