Skip to content

Commit baa3217

Browse files
1 parent a7cf2bb commit baa3217

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

src/main/java/io/protostuff/jetbrains/plugin/reference/file/FilePathReferenceProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
import org.jetbrains.annotations.Nullable;
4242

4343
/**
44-
* Copied from IntelliJ IDEA source code.
44+
* File path references provider.
4545
*
46-
* @author cdr
46+
* @author Kostiantyn Shchepanovskyi
4747
*/
4848
@SuppressWarnings("checkstyle:JavadocMethod")
4949
public class FilePathReferenceProvider extends PsiReferenceProvider {
@@ -63,6 +63,7 @@ public FilePathReferenceProvider() {
6363
public FilePathReferenceProvider(boolean endingSlashNotAllowed) {
6464
myEndingSlashNotAllowed = endingSlashNotAllowed;
6565
sourceRootsProviders.add(new AllSourceRootsProvider());
66+
sourceRootsProviders.add(new WebCoreResourcePathRootsProvider());
6667
sourceRootsProviders.add(new CustomIncludePathRootsProvider());
6768
sourceRootsProviders.add(new LibrariesAndSdkClassesRootsProvider());
6869
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.protostuff.jetbrains.plugin.reference.file;
2+
3+
import com.google.common.base.Strings;
4+
import com.intellij.openapi.diagnostic.Logger;
5+
import com.intellij.openapi.module.Module;
6+
import com.intellij.openapi.project.Project;
7+
import com.intellij.openapi.vfs.LocalFileSystem;
8+
import com.intellij.openapi.vfs.VirtualFile;
9+
import com.intellij.util.containers.MultiMap;
10+
import java.lang.reflect.Method;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
/**
17+
* A workaround for getting resource roots in WebStorm 2017.1.
18+
*
19+
* https://github.com/protostuff/protobuf-jetbrains-plugin/issues/30
20+
* https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000163684-Resource-roots-in-WebStorm
21+
*
22+
* @author Kostiantyn Shchepanovskyi
23+
*/
24+
class WebCoreResourcePathRootsProvider implements FilePathReferenceProvider.SourceRootsProvider {
25+
26+
private static final Logger LOGGER = Logger.getInstance(WebCoreResourcePathRootsProvider.class);
27+
28+
private static final Method GET_INSTANCE;
29+
private static final Method GET_RESOURCE_ROOTS;
30+
31+
public static final String CLASS_NAME = "com.intellij.webcore.resourceRoots.WebResourcesPathsConfiguration";
32+
33+
static {
34+
Method getInstance = null;
35+
Method getResourceRoots = null;
36+
try {
37+
Class<?> clazz = Class.forName(CLASS_NAME);
38+
getInstance = clazz.getMethod("getInstance", Project.class);
39+
getResourceRoots = clazz.getMethod("getResourceRoots");
40+
} catch (Exception e) {
41+
LOGGER.info("Could not load class " + CLASS_NAME);
42+
}
43+
GET_INSTANCE = getInstance;
44+
GET_RESOURCE_ROOTS = getResourceRoots;
45+
}
46+
47+
48+
@SuppressWarnings("unchecked")
49+
@Override
50+
public VirtualFile[] getSourceRoots(Module module) {
51+
try {
52+
if (GET_INSTANCE != null && GET_RESOURCE_ROOTS != null) {
53+
List<VirtualFile> result = new ArrayList<>();
54+
Object configurationInstance = GET_INSTANCE.invoke(null, module.getProject());
55+
MultiMap<String, String> resourceRoots = (MultiMap<String, String>) GET_RESOURCE_ROOTS.invoke(configurationInstance);
56+
for (Map.Entry<String, Collection<String>> entry : resourceRoots.entrySet()) {
57+
for (String uri : entry.getValue()) {
58+
if (!Strings.isNullOrEmpty(uri) && uri.startsWith("file://")) {
59+
String path = uri.substring(7);
60+
VirtualFile file = LocalFileSystem.getInstance().findFileByPath(path);
61+
if (file != null && file.isDirectory()) {
62+
result.add(file);
63+
}
64+
}
65+
}
66+
}
67+
return result.toArray(new VirtualFile[0]);
68+
}
69+
} catch (Exception e) {
70+
LOGGER.error("Could not get source roots for WebCore IDE", e);
71+
}
72+
return new VirtualFile[0];
73+
}
74+
}

0 commit comments

Comments
 (0)