Skip to content

Commit

Permalink
chore: migrate LSPIJUtils.toLocation into PsiUtilsLSImpl
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon authored and angelozerr committed Jan 10, 2024
1 parent 2c849c9 commit abc6df9
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit abc6df9

Please sign in to comment.