Configuration: Set up Kotlin SDK (if necessary) on plugin startup

#KT-22590 Fixed
This commit is contained in:
Alexey Sedunov
2018-02-09 17:25:09 +03:00
parent 99fd66137c
commit e1aec46300
11 changed files with 88 additions and 1 deletions
@@ -25,10 +25,12 @@ import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel;
import com.intellij.openapi.updateSettings.impl.UpdateChecker;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.searches.IndexPatternSearch;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.framework.KotlinSdkType;
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinTodoSearcher;
import org.jetbrains.kotlin.utils.PathUtil;
@@ -73,6 +75,8 @@ public class PluginStartupComponent implements ApplicationComponent {
});
ServiceManager.getService(IndexPatternSearch.class).registerExecutor(new KotlinTodoSearcher());
KotlinSdkType.Companion.setUpIfNeeded();
}
private static void registerPathVariable() {
@@ -10,12 +10,15 @@
package org.jetbrains.kotlin.idea.framework
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.projectRoots.*
import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl
import com.intellij.openapi.projectRoots.impl.SdkConfigurationUtil
import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel
import com.intellij.util.Consumer
import org.jdom.Element
import org.jetbrains.kotlin.idea.KotlinIcons
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.versions.bundledRuntimeVersion
import org.jetbrains.kotlin.utils.PathUtil
import javax.swing.JComponent
@@ -23,6 +26,20 @@ import javax.swing.JComponent
class KotlinSdkType : SdkType("KotlinSDK") {
companion object {
@JvmField val INSTANCE = KotlinSdkType()
val defaultHomePath: String
get() = PathUtil.kotlinPathsForIdeaPlugin.homePath.absolutePath
fun setUpIfNeeded() {
with(ProjectSdksModel()) {
reset(null)
if (sdks.any { it.sdkType is KotlinSdkType }) return
addSdk(INSTANCE, defaultHomePath, null)
ApplicationManager.getApplication().invokeAndWait {
runWriteAction { apply(null, true) }
}
}
}
}
override fun getPresentableName() = "Kotlin SDK"
@@ -48,7 +65,7 @@ class KotlinSdkType : SdkType("KotlinSDK") {
fun createSdkWithUniqueName(existingSdks: Collection<Sdk>): ProjectJdkImpl {
val sdkName = suggestSdkName(SdkConfigurationUtil.createUniqueSdkName(this, "", existingSdks), "")
return ProjectJdkImpl(sdkName, this).apply {
homePath = PathUtil.kotlinPathsForIdeaPlugin.homePath.absolutePath
homePath = defaultHomePath
}
}
+22
View File
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
@@ -0,0 +1,3 @@
<component name="CopyrightManager">
<settings default="" />
</component>
@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/kotlin-runtime.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
</SOURCES>
</library>
</component>
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/module.iml" filepath="$PROJECT_DIR$/module.iml" />
</modules>
</component>
</project>
Binary file not shown.
+11
View File
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>
@@ -18,11 +18,13 @@ package org.jetbrains.kotlin.idea.configuration
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.impl.ApplicationImpl
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.util.io.FileUtil
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.KotlinFacetSettingsProvider
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.idea.compiler.configuration.KotlinCommonCompilerArgumentsHolder
import org.jetbrains.kotlin.idea.framework.KotlinSdkType
import org.jetbrains.kotlin.idea.project.getLanguageVersionSettings
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.junit.Assert
@@ -128,4 +130,8 @@ class ConfigureKotlinInTempDirTest : AbstractConfigureKotlinTest() {
val settings = myProject.getLanguageVersionSettings()
Assert.assertEquals(ApiVersion.KOTLIN_1_1, settings.apiVersion)
}
fun testKotlinSdkAdded() {
Assert.assertTrue(ProjectJdkTable.getInstance().allJdks.any { it.sdkType is KotlinSdkType })
}
}