Skip to content

Commit

Permalink
Make it possible to report folder changes to jdt.ls (not only file ch…
Browse files Browse the repository at this point in the history
…anges)

Related Issue: eclipse-che/che#10115

Signed-off-by: Victor Rubezhny <[email protected]>
  • Loading branch information
vrubezhny authored and fbricon committed Aug 20, 2018
1 parent bf8db8f commit ce9fb2b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal;

import static org.eclipse.core.resources.IResource.DEPTH_ONE;

import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -601,10 +603,36 @@ private static boolean isJavaIdentifierOrPeriod(char ch) {
return Character.isJavaIdentifierPart(ch) || ch == '.';
}

public static boolean isFolder(String uriString) {
IFile fakeFile = findFile(uriString); // This may return IFile even when uriString really describes a IContainer
IContainer parent = fakeFile == null ? null : fakeFile.getParent();
if (parent == null) {
return false;
}
String name = fakeFile.getName();
try {
if (!parent.isSynchronized(DEPTH_ONE)) {
parent.refreshLocal(DEPTH_ONE, null);
}
for (IResource member : parent.members()) {
if (name.equals(member.getName())) {
return (member instanceof IFolder);
}
}
} catch (CoreException e) {
// Ignore
}
return false;
}

public static IFile findFile(String uriString) {
return (IFile) findResource(toURI(uriString), ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI);
}

public static IContainer findFolder(String uriString) {
return (IContainer) findResource(toURI(uriString), ResourcesPlugin.getWorkspace().getRoot()::findContainersForLocationURI);
}

public static IResource findResource(URI uri, Function<URI, IResource[]> resourceFinder) {
if (uri == null || !"file".equals(uri.getScheme())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void fileChanged(String uriString, CHANGE_TYPE changeType) {
if (uriString == null) {
return;
}
IResource resource = JDTUtils.findFile(uriString);
IResource resource = JDTUtils.isFolder(uriString) ? JDTUtils.findFolder(uriString) : JDTUtils.findFile(uriString);
if (resource == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Comparator;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
Expand Down Expand Up @@ -204,4 +205,17 @@ public void testFakeCompilationUnit() throws Exception {
Path path = Paths.get(tempDir + "/test");
Files.walk(path, FileVisitOption.FOLLOW_LINKS).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
}

@Test
public void testIsFolder() throws Exception {
IProject project = WorkspaceHelper.getProject(ProjectsManager.DEFAULT_PROJECT_NAME);
IContainer iFolder = project.getFolder("/src/org/eclipse");
URI uriFolder = iFolder.getLocationURI();
IFile iFile = project.getFile("/src/org/eclipse/Test.java");
URI uriFile = iFile.getLocationURI();
assertTrue(JDTUtils.isFolder(uriFolder.toString()));
assertFalse(JDTUtils.isFolder(uriFile.toString()));
assertNotNull(JDTUtils.findFile(uriFile.toString()));
assertNotNull(JDTUtils.findFolder(uriFolder.toString()));
}
}

0 comments on commit ce9fb2b

Please sign in to comment.