From eff3bc448992a3805330982cada7898e0e1fb42c Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 8 May 2024 14:06:34 +0800 Subject: [PATCH] Return the uri of the parent class when the class file points to a inner class - When the class file name containing '$', we try to get its top-level class (remove the '$'' and its inner class name) from the Java model, if the top-level class do exist, we compare the content of the two classes and return the uri of the top-level class if the content is the same. --- .../jdt/ls/core/internal/JDTUtils.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java index f415004ccb..5c90f55b09 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/JDTUtils.java @@ -854,11 +854,30 @@ public static String toUri(IClassFile classFile) { return null; } + String classId = classFile.getHandleIdentifier(); + String classFileName = classFile.getElementName(); + // try to point to the parent class if this is an inner class, this avoid + // opening new tab for the actual same source file. + if (classFileName.contains("$")) { + String parentClassId = classId.substring(0, classId.lastIndexOf("$")) + ".class"; + IJavaElement parentClass = JavaCore.create(parentClassId); + if (parentClass != null && parentClass instanceof IClassFile parentClassFile) { + try { + // they are from the same source file if the contents are the same. + if (Objects.equals(parentClassFile.getBuffer(), classFile.getBuffer())) { + classId = parentClassFile.getHandleIdentifier(); + classFileName = parentClassFile.getElementName(); + } + } catch (JavaModelException e) { + JavaLanguageServerPlugin.logException("Error get content of the class file", e); + } + } + } String packageName = classFile.getParent().getElementName(); String jarName = classFile.getParent().getParent().getElementName(); String uriString = null; try { - uriString = new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFile.getElementName(), classFile.getHandleIdentifier(), null).toASCIIString(); + uriString = new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFileName, classId, null).toASCIIString(); } catch (URISyntaxException e) { JavaLanguageServerPlugin.logException("Error generating URI for class ", e); }