From abc6df95d1000855a6e918576e12ad1430a76213 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Wed, 10 Jan 2024 15:35:23 +0100 Subject: [PATCH] chore: migrate LSPIJUtils.toLocation into PsiUtilsLSImpl Signed-off-by: Fred Bricon --- .../psi/internal/core/ls/PsiUtilsLSImpl.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java index aa1868c9b..d5c2e7e57 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/core/ls/PsiUtilsLSImpl.java @@ -15,6 +15,7 @@ import com.intellij.openapi.module.Module; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.util.TextRange; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiDocumentManager; @@ -23,6 +24,7 @@ import com.intellij.psi.PsiManager; import com.intellij.psi.PsiMember; import com.intellij.psi.PsiMethod; +import com.intellij.psi.impl.light.LightRecordField; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.psi.util.ClassUtil; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.JsonRpcHelpers; @@ -34,6 +36,7 @@ import org.eclipse.lsp4j.Range; import org.eclipse.lsp4mp.commons.ClasspathKind; import org.eclipse.lsp4mp.commons.DocumentFormat; +import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -100,7 +103,32 @@ public void discoverSource(PsiFile classFile) { @Override public Location toLocation(PsiElement psiMember) { - return LSPIJUtils.toLocation(psiMember); + PsiElement sourceElement = getNavigationElement(psiMember); + + if (sourceElement != null) { + PsiFile file = sourceElement.getContainingFile(); + Document document = PsiDocumentManager.getInstance(psiMember.getProject()).getDocument(file); + if (document != null) { + TextRange range = sourceElement.getTextRange(); + return toLocation(file, LSPIJUtils.toRange(range, document)); + } + } + return null; + } + + public static Location toLocation(PsiFile file, Range range) { + return toLocation(file.getVirtualFile(), range); + } + + public static Location toLocation(VirtualFile file, Range range) { + return new Location(LSPIJUtils.toUriAsString(file), range); + } + + private static @Nullable PsiElement getNavigationElement(PsiElement psiMember) { + if (psiMember instanceof LightRecordField psiRecord) { + psiMember = psiRecord.getRecordComponent(); + } + return psiMember.getNavigationElement(); } @Override