Add kotlin annotations for android SDK
This commit is contained in:
@@ -29,6 +29,7 @@ import com.intellij.psi.JavaPsiFacade;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.ui.EditorNotificationPanel;
|
||||
import com.intellij.ui.EditorNotifications;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.plugin.JetFileType;
|
||||
import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil;
|
||||
@@ -53,17 +54,33 @@ public class AbsentJdkAnnotationsNotifications extends EditorNotifications.Provi
|
||||
|
||||
GlobalSearchScope scope = module.getModuleWithDependenciesAndLibrariesScope(false);
|
||||
if (JavaPsiFacade.getInstance(project).findClass("jet.JetObject", scope) == null) return null;
|
||||
if (KotlinRuntimeLibraryUtil.jdkAnnotationsArePresent(module)) return null;
|
||||
|
||||
Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
|
||||
if (sdk == null) return null;
|
||||
|
||||
return createMissingSdkAnnotationsPanelIfNeeded(module, sdk);
|
||||
}
|
||||
|
||||
private EditorNotificationPanel createMissingSdkAnnotationsPanelIfNeeded(final Module module, @NotNull Sdk sdk) {
|
||||
final boolean isAndroidSdk = isAndroidSdk(sdk);
|
||||
if (KotlinRuntimeLibraryUtil.jdkAnnotationsArePresent(module)) {
|
||||
if (!isAndroidSdk || KotlinRuntimeLibraryUtil.androidSdkAnnotationsArePresent(module)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
EditorNotificationPanel panel = new EditorNotificationPanel();
|
||||
panel.setText("Kotlin external annotations for JDK are not set for '" + sdk.getName() + "'.");
|
||||
panel.createActionLabel("Set up Kotlin JDK annotations", new Runnable() {
|
||||
String sdkKind = isAndroidSdk ? "Android SDK" : "JDK";
|
||||
panel.setText("Kotlin external annotations for " + sdkKind + " are not set for '" + sdk.getName() + "'.");
|
||||
panel.createActionLabel("Set up Kotlin " + sdkKind + " annotations", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
KotlinRuntimeLibraryUtil.addJdkAnnotations(module);
|
||||
if (!KotlinRuntimeLibraryUtil.jdkAnnotationsArePresent(module)) {
|
||||
KotlinRuntimeLibraryUtil.addJdkAnnotations(module);
|
||||
}
|
||||
if (isAndroidSdk && !KotlinRuntimeLibraryUtil.androidSdkAnnotationsArePresent(module)) {
|
||||
KotlinRuntimeLibraryUtil.addAndroidSdkAnnotations(module);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -74,4 +91,8 @@ public class AbsentJdkAnnotationsNotifications extends EditorNotifications.Provi
|
||||
public Key<EditorNotificationPanel> getKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
private static boolean isAndroidSdk(@NotNull Sdk sdk) {
|
||||
return sdk.getSdkType().getName().equals("Android SDK");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,13 +88,20 @@ public class KotlinRuntimeLibraryUtil {
|
||||
}
|
||||
|
||||
public static void addJdkAnnotations(@NotNull Module module) {
|
||||
addAnnotations(module, PathUtil.getKotlinPathsForIdeaPlugin().getJdkAnnotationsPath());
|
||||
}
|
||||
|
||||
public static void addAndroidSdkAnnotations(@NotNull Module module) {
|
||||
addAnnotations(module, PathUtil.getKotlinPathsForIdeaPlugin().getAndroidSdkAnnotationsPath());
|
||||
}
|
||||
|
||||
private static void addAnnotations(@NotNull Module module, @NotNull File annotationsPath) {
|
||||
Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
|
||||
if (sdk == null) {
|
||||
return;
|
||||
}
|
||||
File annotationsIoFile = PathUtil.getKotlinPathsForIdeaPlugin().getJdkAnnotationsPath();
|
||||
if (annotationsIoFile.exists()) {
|
||||
VirtualFile jdkAnnotationsJar = LocalFileSystem.getInstance().findFileByIoFile(annotationsIoFile);
|
||||
if (annotationsPath.exists()) {
|
||||
VirtualFile jdkAnnotationsJar = LocalFileSystem.getInstance().findFileByIoFile(annotationsPath);
|
||||
if (jdkAnnotationsJar != null) {
|
||||
SdkModificator modificator = sdk.getSdkModificator();
|
||||
modificator.addRoot(JarFileSystem.getInstance().getJarRootForLocalFile(jdkAnnotationsJar),
|
||||
@@ -105,13 +112,21 @@ public class KotlinRuntimeLibraryUtil {
|
||||
}
|
||||
|
||||
public static boolean jdkAnnotationsArePresent(@NotNull Module module) {
|
||||
return areAnnotationsPresent(module, PathUtil.JDK_ANNOTATIONS_JAR);
|
||||
}
|
||||
|
||||
public static boolean androidSdkAnnotationsArePresent(@NotNull Module module) {
|
||||
return areAnnotationsPresent(module, PathUtil.ANDROID_SDK_ANNOTATIONS_JAR);
|
||||
}
|
||||
|
||||
private static boolean areAnnotationsPresent(@NotNull Module module, @NotNull final String jarFileName) {
|
||||
Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
|
||||
if (sdk == null) return false;
|
||||
return ContainerUtil.exists(sdk.getRootProvider().getFiles(AnnotationOrderRootType.getInstance()),
|
||||
new Condition<VirtualFile>() {
|
||||
@Override
|
||||
public boolean value(VirtualFile file) {
|
||||
return PathUtil.JDK_ANNOTATIONS_JAR.equals(file.getName());
|
||||
return jarFileName.equals(file.getName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user