fix KT-7879 KotlinJS - Cannot browse jslib sources

#KT-7879 Fixed
This commit is contained in:
Michael Nedzelsky
2015-06-18 15:04:40 +03:00
parent 17ca0d2c2c
commit eda7f49b61
4 changed files with 75 additions and 51 deletions
@@ -16,14 +16,13 @@
package org.jetbrains.kotlin.idea.configuration;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.impl.scopes.LibraryScope;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.framework.JavaRuntimeLibraryDescription;
import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider;
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil;
@@ -32,8 +31,6 @@ import org.jetbrains.kotlin.idea.versions.KotlinRuntimeLibraryCoreUtil;
import org.jetbrains.kotlin.utils.KotlinPaths;
import org.jetbrains.kotlin.utils.PathUtil;
import static org.jetbrains.kotlin.idea.configuration.ConfigureKotlinInProjectUtils.showInfoNotification;
public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator {
public static final String NAME = "java";
@@ -95,49 +92,11 @@ public class KotlinJavaModuleConfigurator extends KotlinWithLibraryConfigurator
);
}
public void copySourcesToPathFromLibrary(@NotNull Library library) {
String dirToJarFromLibrary = getPathFromLibrary(library, OrderRootType.SOURCES);
assert dirToJarFromLibrary != null : "Directory to file from library should be non null";
copyFileToDir(getExistingJarFiles().getRuntimeSourcesJar(), dirToJarFromLibrary);
}
public boolean changeOldSourcesPathIfNeeded(@NotNull Library library) {
if (!removeOldSourcesRootIfNeeded(library)) {
return false;
}
String parentDir = getPathFromLibrary(library, OrderRootType.CLASSES);
assert parentDir != null : "Parent dir for classes jar should exists for Kotlin library";
return addSourcesToLibraryIfNeeded(library, getExistingJarFiles().getRuntimeSourcesDestination(parentDir));
}
private static boolean removeOldSourcesRootIfNeeded(@NotNull Library library) {
@Nullable
@Override
protected String getOldSourceRootUrl(@NotNull Library library) {
VirtualFile runtimeJarPath = JavaRuntimePresentationProvider.getRuntimeJar(library);
if (runtimeJarPath == null) {
return false;
}
String oldLibrarySourceRoot = runtimeJarPath.getUrl() + "src";
String[] librarySourceRoots = library.getUrls(OrderRootType.SOURCES);
for (String sourceRoot : librarySourceRoots) {
if (sourceRoot.equals(oldLibrarySourceRoot)) {
final Library.ModifiableModel model = library.getModifiableModel();
model.removeRoot(oldLibrarySourceRoot, OrderRootType.SOURCES);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
model.commit();
}
});
showInfoNotification("Source root '" + oldLibrarySourceRoot + "' was removed for " + library.getName() + " library");
return true;
}
}
return false;
return runtimeJarPath != null ? runtimeJarPath.getUrl() + "src" : null;
}
@Override
@@ -17,8 +17,12 @@
package org.jetbrains.kotlin.idea.configuration;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.framework.JSLibraryStdDescription;
import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider;
import org.jetbrains.kotlin.idea.project.ProjectStructureUtil;
import org.jetbrains.kotlin.idea.project.TargetPlatform;
import org.jetbrains.kotlin.js.JavaScript;
@@ -88,4 +92,11 @@ public class KotlinJsModuleConfigurator extends KotlinWithLibraryConfigurator {
KotlinJsModuleConfigurator() {
}
@Nullable
@Override
protected String getOldSourceRootUrl(@NotNull Library library) {
VirtualFile jsStdLibJar = JSLibraryStdPresentationProvider.getJsStdLibJar(library);
return jsStdLibJar != null ? jsStdLibJar.getUrl() : null;
}
}
@@ -64,6 +64,9 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
@NotNull
public abstract RuntimeLibraryFiles getExistingJarFiles();
@Nullable
protected abstract String getOldSourceRootUrl(@NotNull Library library);
@Override
public boolean isApplicable(@NotNull Module module) {
return !JetPluginUtil.isAndroidGradleModule(module) &&
@@ -509,6 +512,46 @@ public abstract class KotlinWithLibraryConfigurator implements KotlinProjectConf
return file;
}
public void copySourcesToPathFromLibrary(@NotNull Library library) {
String dirToJarFromLibrary = getPathFromLibrary(library, OrderRootType.SOURCES);
assert dirToJarFromLibrary != null : "Directory to file from library should be non null";
copyFileToDir(getExistingJarFiles().getRuntimeSourcesJar(), dirToJarFromLibrary);
}
public boolean changeOldSourcesPathIfNeeded(@NotNull Library library) {
if (!removeOldSourcesRootIfNeeded(library)) {
return false;
}
String parentDir = getPathFromLibrary(library, OrderRootType.CLASSES);
assert parentDir != null : "Parent dir for classes jar should exists for Kotlin library";
return addSourcesToLibraryIfNeeded(library, getExistingJarFiles().getRuntimeSourcesDestination(parentDir));
}
protected boolean removeOldSourcesRootIfNeeded(@NotNull Library library) {
String oldLibrarySourceRoot = getOldSourceRootUrl(library);
String[] librarySourceRoots = library.getUrls(OrderRootType.SOURCES);
for (String sourceRoot : librarySourceRoots) {
if (sourceRoot.equals(oldLibrarySourceRoot)) {
final Library.ModifiableModel model = library.getModifiableModel();
model.removeRoot(oldLibrarySourceRoot, OrderRootType.SOURCES);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
model.commit();
}
});
showInfoNotification("Source root '" + oldLibrarySourceRoot + "' was removed for " + library.getName() + " library");
return true;
}
}
return false;
}
KotlinWithLibraryConfigurator() {
}
}
@@ -44,6 +44,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.JetPluginUtil;
import org.jetbrains.kotlin.idea.configuration.ConfigureKotlinInProjectUtils;
import org.jetbrains.kotlin.idea.configuration.KotlinJavaModuleConfigurator;
import org.jetbrains.kotlin.idea.configuration.KotlinJsModuleConfigurator;
import org.jetbrains.kotlin.idea.framework.JSLibraryStdPresentationProvider;
import org.jetbrains.kotlin.idea.framework.JavaRuntimePresentationProvider;
import org.jetbrains.kotlin.idea.framework.LibraryPresentationProviderUtil;
@@ -164,17 +165,21 @@ public class KotlinRuntimeLibraryUtil {
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
KotlinJavaModuleConfigurator configurator = (KotlinJavaModuleConfigurator)
KotlinJavaModuleConfigurator kJvmConfigurator = (KotlinJavaModuleConfigurator)
ConfigureKotlinInProjectUtils.getConfiguratorByName(KotlinJavaModuleConfigurator.NAME);
assert configurator != null : "Configurator with given name doesn't exists: " + KotlinJavaModuleConfigurator.NAME;
assert kJvmConfigurator != null : "Configurator with given name doesn't exists: " + KotlinJavaModuleConfigurator.NAME;
KotlinJsModuleConfigurator kJsConfigurator = (KotlinJsModuleConfigurator)
ConfigureKotlinInProjectUtils.getConfiguratorByName(KotlinJsModuleConfigurator.NAME);
assert kJsConfigurator != null : "Configurator with given name doesn't exists: " + KotlinJsModuleConfigurator.NAME;
for (Library library : libraries) {
if (LibraryPresentationProviderUtil.isDetected(JavaRuntimePresentationProvider.getInstance(), library)) {
updateJar(project, JavaRuntimePresentationProvider.getRuntimeJar(library), LibraryJarDescriptor.RUNTIME_JAR);
updateJar(project, JavaRuntimePresentationProvider.getReflectJar(library), LibraryJarDescriptor.REFLECT_JAR);
if (configurator.changeOldSourcesPathIfNeeded(library)) {
configurator.copySourcesToPathFromLibrary(library);
if (kJvmConfigurator.changeOldSourcesPathIfNeeded(library)) {
kJvmConfigurator.copySourcesToPathFromLibrary(library);
}
else {
updateJar(project, JavaRuntimePresentationProvider.getRuntimeSrcJar(library), LibraryJarDescriptor.RUNTIME_SRC_JAR);
@@ -182,7 +187,13 @@ public class KotlinRuntimeLibraryUtil {
}
else if (LibraryPresentationProviderUtil.isDetected(JSLibraryStdPresentationProvider.getInstance(), library)) {
updateJar(project, JSLibraryStdPresentationProvider.getJsStdLibJar(library), LibraryJarDescriptor.JS_STDLIB_JAR);
updateJar(project, JSLibraryStdPresentationProvider.getJsStdLibSrcJar(library), LibraryJarDescriptor.JS_STDLIB_SRC_JAR);
if (kJsConfigurator.changeOldSourcesPathIfNeeded(library)) {
kJsConfigurator.copySourcesToPathFromLibrary(library);
}
else {
updateJar(project, JSLibraryStdPresentationProvider.getJsStdLibSrcJar(library), LibraryJarDescriptor.JS_STDLIB_SRC_JAR);
}
}
}
}