|
| 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