Skip to content

Commit aefa4e9

Browse files
authored
Fix the sync issue with non-java files (#131)
* Fix the sync issue with non-java files
1 parent 91e92bb commit aefa4e9

File tree

4 files changed

+342
-181
lines changed

4 files changed

+342
-181
lines changed

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/ExtUtils.java

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,27 @@
1313

1414
import java.net.URI;
1515
import java.net.URISyntaxException;
16+
import java.util.Arrays;
1617

18+
import org.apache.commons.lang3.StringUtils;
19+
import org.eclipse.core.runtime.CoreException;
1720
import org.eclipse.core.runtime.IPath;
21+
import org.eclipse.core.runtime.IStatus;
22+
import org.eclipse.core.runtime.Status;
23+
import org.eclipse.jdt.core.IJarEntryResource;
24+
import org.eclipse.jdt.core.IPackageFragment;
1825
import org.eclipse.jdt.core.IPackageFragmentRoot;
26+
import org.eclipse.jdt.core.JavaCore;
27+
import org.eclipse.jdt.core.JavaModelException;
28+
import org.eclipse.jdt.internal.core.JarEntryDirectory;
1929
import org.eclipse.jdt.internal.core.JarEntryFile;
2030
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
2131

2232
public final class ExtUtils {
23-
24-
private static final String JDT_SCHEME = "jdt";
25-
33+
public static final String JDT_SCHEME = "jdt";
2634
private static final String CONTENTS_AUTHORITY = "jarentry";
2735

28-
public static String toUri(JarEntryFile jarEntryFile) {
36+
public static String toUri(IJarEntryResource jarEntryFile) {
2937
IPackageFragmentRoot fragmentRoot = jarEntryFile.getPackageFragmentRoot();
3038
try {
3139
return new URI(JDT_SCHEME, CONTENTS_AUTHORITY, jarEntryFile.getFullPath().toPortableString(), fragmentRoot.getHandleIdentifier(), null).toASCIIString();
@@ -35,10 +43,79 @@ public static String toUri(JarEntryFile jarEntryFile) {
3543
}
3644
}
3745

46+
public static boolean isJarResourceUri(URI uri) {
47+
return uri != null && JDT_SCHEME.equals(uri.getScheme()) && CONTENTS_AUTHORITY.equals(uri.getAuthority());
48+
}
49+
50+
public static JarEntryFile findJarEntryFile(IPackageFragmentRoot packageRoot, String path) throws JavaModelException {
51+
String[] segments = StringUtils.split(path, "/");
52+
String packageName = StringUtils.join(Arrays.asList(segments).subList(0, segments.length - 1), '.');
53+
IPackageFragment packageFragment = packageRoot.getPackageFragment(packageName);
54+
if (packageFragment != null && packageFragment.exists()) {
55+
Object[] objs = packageFragment.getNonJavaResources();
56+
for (Object obj : objs) {
57+
if (obj instanceof IJarEntryResource) {
58+
IJarEntryResource child = (IJarEntryResource) obj;
59+
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
60+
return (JarEntryFile) child;
61+
}
62+
}
63+
}
64+
}
65+
Object[] resources = packageRoot.getNonJavaResources();
66+
67+
for (Object resource : resources) {
68+
if (resource instanceof JarEntryFile) {
69+
JarEntryFile file = (JarEntryFile) resource;
70+
if (file.getFullPath().toPortableString().equals(path)) {
71+
return file;
72+
}
73+
}
74+
if (resource instanceof JarEntryDirectory) {
75+
JarEntryDirectory directory = (JarEntryDirectory) resource;
76+
JarEntryFile file = findFileInJar(directory, path);
77+
if (file != null) {
78+
return file;
79+
}
80+
}
81+
}
82+
return null;
83+
}
84+
85+
public static IJarEntryResource getJarEntryResource(URI uri) throws CoreException {
86+
if (uri == null) {
87+
throw new NullPointerException("Cannot get jar resource from null URI.");
88+
}
89+
String handleId = uri.getQuery();
90+
if (handleId == null) {
91+
throw new NullPointerException("Invalid uri for a jar entry.");
92+
}
93+
IPackageFragmentRoot packageRoot = (IPackageFragmentRoot) JavaCore.create(handleId);
94+
if (packageRoot == null) {
95+
throw new CoreException(new Status(IStatus.ERROR, JdtlsExtActivator.PLUGIN_ID, String.format("No package root found for %s", handleId)));
96+
}
97+
return findJarEntryFile(packageRoot, uri.getPath());
98+
}
99+
38100
public static IPath removeProjectSegment(String projectElementName, IPath path) {
39101
if (projectElementName.equals(path.segment(0))) {
40102
return path.removeFirstSegments(1).makeRelative();
41103
}
42104
return path;
43105
}
106+
107+
private static JarEntryFile findFileInJar(JarEntryDirectory directory, String path) {
108+
for (IJarEntryResource child : directory.getChildren()) {
109+
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
110+
return (JarEntryFile) child;
111+
}
112+
if (child instanceof JarEntryDirectory) {
113+
JarEntryFile file = findFileInJar((JarEntryDirectory) child, path);
114+
if (file != null) {
115+
return file;
116+
}
117+
}
118+
}
119+
return null;
120+
}
44121
}

jdtls.ext/com.microsoft.jdtls.ext.core/src/com/microsoft/jdtls/ext/core/JarFileContentProvider.java

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,14 @@
1515
import java.io.InputStream;
1616
import java.net.URI;
1717
import java.nio.charset.StandardCharsets;
18-
import java.util.Arrays;
1918

2019
import org.apache.commons.io.IOUtils;
21-
import org.apache.commons.lang3.StringUtils;
2220
import org.eclipse.core.runtime.CoreException;
2321
import org.eclipse.core.runtime.IProgressMonitor;
2422
import org.eclipse.core.runtime.IStatus;
2523
import org.eclipse.core.runtime.Status;
26-
import org.eclipse.jdt.core.IJarEntryResource;
27-
import org.eclipse.jdt.core.IPackageFragment;
2824
import org.eclipse.jdt.core.IPackageFragmentRoot;
2925
import org.eclipse.jdt.core.JavaCore;
30-
import org.eclipse.jdt.internal.core.JarEntryDirectory;
3126
import org.eclipse.jdt.internal.core.JarEntryFile;
3227
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
3328
import org.eclipse.jdt.ls.core.internal.IContentProvider;
@@ -49,40 +44,12 @@ private String getContent(String rootId, String path, IProgressMonitor pm) {
4944
if (packageRoot == null) {
5045
throw new CoreException(new Status(IStatus.ERROR, JdtlsExtActivator.PLUGIN_ID, String.format("No package root found for %s", rootId)));
5146
}
52-
if (packageRoot instanceof JarPackageFragmentRoot) {
53-
Object[] resources = packageRoot.getNonJavaResources();
54-
55-
for (Object resource : resources) {
56-
if (resource instanceof JarEntryFile) {
57-
JarEntryFile file = (JarEntryFile) resource;
58-
if (file.getFullPath().toPortableString().equals(path)) {
59-
return readFileContent(file);
60-
}
61-
}
62-
if (resource instanceof JarEntryDirectory) {
63-
JarEntryDirectory directory = (JarEntryDirectory) resource;
64-
JarEntryFile file = findFileInJar(directory, path);
65-
if (file != null) {
66-
return readFileContent(file);
67-
}
68-
}
69-
}
70-
// if the file exists in the java packages
71-
String[] segments = StringUtils.split(path, "/");
72-
String packageName = StringUtils.join(Arrays.asList(segments).subList(0, segments.length - 1), '.');
73-
IPackageFragment packageFragment = packageRoot.getPackageFragment(packageName);
74-
if (packageFragment != null && packageFragment.exists()) {
7547

76-
Object[] objs = packageFragment.getNonJavaResources();
77-
for (Object obj : objs) {
78-
if (obj instanceof IJarEntryResource) {
79-
IJarEntryResource child = (IJarEntryResource) obj;
80-
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
81-
return readFileContent((JarEntryFile) child);
82-
}
83-
}
8448

85-
}
49+
if (packageRoot instanceof JarPackageFragmentRoot) {
50+
JarEntryFile fileEntry = ExtUtils.findJarEntryFile(packageRoot, path);
51+
if (fileEntry != null) {
52+
return readFileContent(fileEntry);
8653
}
8754

8855
}
@@ -92,20 +59,7 @@ private String getContent(String rootId, String path, IProgressMonitor pm) {
9259
return null;
9360
}
9461

95-
private static JarEntryFile findFileInJar(JarEntryDirectory directory, String path) {
96-
for (IJarEntryResource child : directory.getChildren()) {
97-
if (child instanceof JarEntryFile && child.getFullPath().toPortableString().equals(path)) {
98-
return (JarEntryFile) child;
99-
}
100-
if (child instanceof JarEntryDirectory) {
101-
JarEntryFile file = findFileInJar((JarEntryDirectory) child, path);
102-
if (file != null) {
103-
return file;
104-
}
105-
}
106-
}
107-
return null;
108-
}
62+
10963

11064
private static String readFileContent(JarEntryFile file) throws CoreException {
11165
try (InputStream stream = (file.getContents())) {

0 commit comments

Comments
 (0)