Skip to content

Commit

Permalink
Fix reset version and code reformat of SDK (#3632)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuataylor authored Aug 15, 2024
1 parent 32abdba commit c7146e6
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 183 deletions.
2 changes: 0 additions & 2 deletions src/org/elixir_lang/notification/setup_sdk/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleType
import com.intellij.openapi.project.Project
import org.elixir_lang.notification.setup_sdk.Provider.Companion.showFacetSettings
import org.elixir_lang.notification.setup_sdk.Provider.Companion.showModuleSettings

/**
* Created by zyuyou on 15/7/8.
Expand Down
152 changes: 75 additions & 77 deletions src/org/elixir_lang/notification/setup_sdk/Provider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Provider : EditorNotificationProvider {
project: Project,
file: VirtualFile,
): Function<in FileEditor, out JComponent?> =
Function { fileEditor ->
Function {
kotlinx.coroutines.runBlocking {
createNotificationPanel(file, project)
}
Expand Down Expand Up @@ -60,91 +60,89 @@ class Provider : EditorNotificationProvider {
}
}

companion object {
fun showFacetSettings(project: Project) {
if (ProcessOutput.isSmallIde) {
showSmallIDEFacetSettings(project)
}
// TODO Elixir Facet in non-Elixir module in IntelliJ
}

fun showModuleSettings(
project: Project,
module: Module,
) {
ProjectSettingsService.getInstance(project).openModuleSettings(module)
}
}

private fun showProjectSettings(project: Project) {
SdkPopupFactory
.newBuilder()
.withProject(project)
.withSdkType(Type.instance)
.updateProjectSdkFromSelection()
.buildPopup()
.showInFocusCenter()
}
fun showFacetSettings(project: Project) {
if (ProcessOutput.isSmallIde) {
showSmallIDEFacetSettings(project)
}
// TODO Elixir Facet in non-Elixir module in IntelliJ
}

private fun showSmallIDEFacetSettings(project: Project) {
ShowSettingsUtilImpl.showSettingsDialog(project, "language", "Elixir")
}
fun showModuleSettings(
project: Project,
module: Module,
) {
ProjectSettingsService.getInstance(project).openModuleSettings(module)
}

private fun createSmallIDEFacetPanel(project: Project): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Elixir Facet SDK is not defined"
@Suppress("DialogTitleCapitalization")
createActionLabel("Setup Elixir Facet SDK") {
showSmallIDEFacetSettings(project)
}
}
private fun showProjectSettings(project: Project) {
SdkPopupFactory.newBuilder()
.withProject(project)
.withSdkType(Type.instance)
.updateProjectSdkFromSelection()
.buildPopup()
.showInFocusCenter()
}

private fun createFacetPanel(project: Project): EditorNotificationPanel? =
if (ProcessOutput.isSmallIde) {
createSmallIDEFacetPanel(project)
} else {
// TODO Elixir Facet in non-Elixir module in IntelliJ
null
}
private fun showSmallIDEFacetSettings(project: Project) {
ShowSettingsUtilImpl.showSettingsDialog(project, "language", "Elixir")
}

private fun createModulePanel(
project: Project,
module: Module,
): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Elixir Module SDK is not defined"
@Suppress("DialogTitleCapitalization")
createActionLabel("Setup Elixir Module SDK") {
showModuleSettings(project, module)
}
}
private fun createSmallIDEFacetPanel(project: Project): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Elixir Facet SDK is not defined"
@Suppress("DialogTitleCapitalization")
createActionLabel("Setup Elixir Facet SDK") {
showSmallIDEFacetSettings(project)
}
}

private fun createPanel(
project: Project,
psiFile: PsiFile,
): EditorNotificationPanel? {
val module = ModuleUtilCore.findModuleForPsiElement(psiFile)
private fun createFacetPanel(project: Project): EditorNotificationPanel? =
if (ProcessOutput.isSmallIde) {
createSmallIDEFacetPanel(project)
} else {
// TODO Elixir Facet in non-Elixir module in IntelliJ
null
}

return when {
module != null -> {
// CANNOT use ModuleType.is(module, ElixirModuleType.getInstance()) as ElixirModuleType depends on
// JavaModuleBuilder and so only available in IntelliJ
if (ModuleType.get(module).id == "ELIXIR_MODULE") {
createModulePanel(project, module)
} else {
createFacetPanel(project)
}
}
ProcessOutput.isSmallIde -> createSmallIDEFacetPanel(project)
else -> createProjectPanel(project)
}
private fun createModulePanel(
project: Project,
module: Module,
): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Elixir Module SDK is not defined"
@Suppress("DialogTitleCapitalization")
createActionLabel("Setup Elixir Module SDK") {
showModuleSettings(project, module)
}
}

private fun createProjectPanel(project: Project): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Project SDK is not defined"
createActionLabel(ProjectBundle.message("project.sdk.setup")) {
showProjectSettings(project)
}
private fun createPanel(
project: Project,
psiFile: PsiFile,
): EditorNotificationPanel? {
val module = ModuleUtilCore.findModuleForPsiElement(psiFile)

return when {
module != null -> {
// CANNOT use ModuleType.is(module, ElixirModuleType.getInstance()) as ElixirModuleType depends on
// JavaModuleBuilder and so only available in IntelliJ
if (ModuleType.get(module).id == "ELIXIR_MODULE") {
createModulePanel(project, module)
} else {
createFacetPanel(project)
}
}
ProcessOutput.isSmallIde -> createSmallIDEFacetPanel(project)
else -> createProjectPanel(project)
}
}

private fun createProjectPanel(project: Project): EditorNotificationPanel =
EditorNotificationPanel().apply {
text = "Project SDK is not defined"
createActionLabel(ProjectBundle.message("project.sdk.setup")) {
showProjectSettings(project)
}
}
6 changes: 4 additions & 2 deletions src/org/elixir_lang/sdk/elixir/Release.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Release private constructor(
) : Comparable<Release> {

companion object {
@JvmField val V_1_0_4 = Release("1", "0", "4", null, null)
@JvmField val LATEST = Release("1", "6", "0", "dev", null)
@JvmField
val V_1_0_4 = Release("1", "0", "4", null, null)
@JvmField
val LATEST = Release("1", "6", "0", "dev", null)

private val VERSION_PATTERN = Pattern.compile(
"(\\d+)(?:\\.(\\d+))?(?:\\.(\\d+))?(?:\\-([\\d\\w\\.\\-]+))?(?:\\+([\\d\\w\\-]+))?"
Expand Down
30 changes: 18 additions & 12 deletions src/org/elixir_lang/sdk/elixir/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
import javax.swing.Icon
import kotlin.Comparator
import kotlin.collections.ArrayList

class Type : org.elixir_lang.sdk.erlang_dependent.Type(SerializerExtension.ELIXIR_SDK_TYPE_ID) {
/**
Expand Down Expand Up @@ -101,7 +99,8 @@ class Type : org.elixir_lang.sdk.erlang_dependent.Type(SerializerExtension.ELIXI

override fun getPresentableName(): String = "Elixir SDK"

override fun getVersionString(sdkHome: String): String = Release.fromString(File(sdkHome).name)?.version() ?: "Unknown"
override fun getVersionString(sdkHome: String): String =
Release.fromString(File(sdkHome).name)?.version() ?: "Unknown"

/**
* Map of home paths to versions in descending version order so that newer versions are favored.
Expand Down Expand Up @@ -148,7 +147,8 @@ class Type : org.elixir_lang.sdk.erlang_dependent.Type(SerializerExtension.ELIXI
return homePathByVersion
}

private fun invalidSdkHomeException(virtualFile: VirtualFile): Exception = Exception(invalidSdkHomeMessage(virtualFile))
private fun invalidSdkHomeException(virtualFile: VirtualFile): Exception =
Exception(invalidSdkHomeMessage(virtualFile))

private fun invalidSdkHomeMessage(virtualFile: VirtualFile): String =
if (virtualFile.isDirectory) {
Expand Down Expand Up @@ -178,10 +178,10 @@ ELIXIR_SDK_HOME
val iex = Elixir.getIExExecutable(path)
val mix = Elixir.mixFile(path)
return elixir.canExecute() &&
elixirc.canExecute() &&
iex.canExecute() &&
mix.canRead() &&
HomePath.hasEbinPath(path)
elixirc.canExecute() &&
iex.canExecute() &&
mix.canRead() &&
HomePath.hasEbinPath(path)
}

override fun setupSdkPaths(sdk: Sdk) {
Expand Down Expand Up @@ -218,7 +218,8 @@ ELIXIR_SDK_HOME
override fun createAdditionalDataConfigurable(
sdkModel: SdkModel,
sdkModificator: SdkModificator,
): com.intellij.openapi.projectRoots.AdditionalDataConfigurable = AdditionalDataConfigurable(sdkModel, sdkModificator)
): com.intellij.openapi.projectRoots.AdditionalDataConfigurable =
AdditionalDataConfigurable(sdkModel, sdkModificator)

override fun saveAdditionalData(
additionalData: com.intellij.openapi.projectRoots.SdkAdditionalData,
Expand Down Expand Up @@ -395,7 +396,12 @@ ELIXIR_SDK_HOME
val configuredRoots = elixirSdkModificator.getRoots(type)
for (internalRoot in internalRoots) {
for (expandedInternalRoot in expandInternalErlangSdkRoot(internalRoot, type)) {
sdkModificatorRootTypeConsumer(elixirSdkModificator, configuredRoots, expandedInternalRoot, type)
sdkModificatorRootTypeConsumer(
elixirSdkModificator,
configuredRoots,
expandedInternalRoot,
type
)
}
}
}
Expand Down Expand Up @@ -492,8 +498,8 @@ ELIXIR_SDK_HOME
@JvmStatic
fun erlangSdkType(): SdkType =
if (ProcessOutput.isSmallIde) {
/* intellij-erlang's "Erlang SDK" does not work in small IDEs because it uses JavadocRoot for documentation,
which isn't available in Small IDEs. */
/* intellij-erlang's "Erlang SDK" does not work in small IDEs because it uses JavadocRoot for documentation,
which isn't available in Small IDEs. */
null
} else {
EP_NAME.extensionList.find { sdkType -> sdkType.name == "Erlang SDK" }
Expand Down
20 changes: 11 additions & 9 deletions src/org/elixir_lang/sdk/erlang/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Type : SdkType("Erlang SDK for Elixir SDK") {
private const val ERTS_VERSION_PREFIX_LINE = "org.elixir_lang.sdk.erlang.Type ERTS_VERSION:"
private const val PRINT_VERSION_INFO_EXPRESSION =
"io:format(\"~n~s~n~s~n~s~n~s~n\",[" +
"\"$OTP_RELEASE_PREFIX_LINE\"," +
"erlang:system_info(otp_release)," +
"\"$ERTS_VERSION_PREFIX_LINE\"," +
"erlang:system_info(version)" +
"]),erlang:halt()."
"\"$OTP_RELEASE_PREFIX_LINE\"," +
"erlang:system_info(otp_release)," +
"\"$ERTS_VERSION_PREFIX_LINE\"," +
"erlang:system_info(version)" +
"]),erlang:halt()."
private const val WINDOWS_DEFAULT_HOME_PATH = "C:\\Program Files\\erl9.0"
private val NIX_PATTERN = HomePath.nixPattern("erlang")
private const val LINUX_MINT_HOME_PATH = "${HomePath.LINUX_MINT_HOME_PATH}/erlang"
Expand Down Expand Up @@ -78,9 +78,11 @@ class Type : SdkType("Erlang SDK for Elixir SDK") {
HomePath.mergeHomebrew(homePathByVersion, "erlang", VERSION_PATH_TO_HOME_PATH)
HomePath.mergeNixStore(homePathByVersion, NIX_PATTERN, VERSION_PATH_TO_HOME_PATH)
}

SystemInfo.isWindows -> {
putIfDirectory(homePathByVersion, HomePath.UNKNOWN_VERSION, WINDOWS_DEFAULT_HOME_PATH)
}

SystemInfo.isLinux -> {
putIfDirectory(homePathByVersion, HomePath.UNKNOWN_VERSION, LINUX_DEFAULT_HOME_PATH)
putIfDirectory(homePathByVersion, HomePath.UNKNOWN_VERSION, LINUX_MINT_HOME_PATH)
Expand All @@ -106,10 +108,10 @@ class Type : SdkType("Erlang SDK for Elixir SDK") {

override fun isRootTypeApplicable(type: OrderRootType): Boolean =
type == OrderRootType.CLASSES ||
type == OrderRootType.SOURCES ||
type ==
org.elixir_lang.sdk.Type
.documentationRootType()
type == OrderRootType.SOURCES ||
type ==
org.elixir_lang.sdk.Type
.documentationRootType()

override fun setupSdkPaths(sdk: Sdk) {
val sdkModificator = sdk.sdkModificator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.intellij.openapi.projectRoots.AdditionalDataConfigurable
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.SdkModel
import com.intellij.openapi.projectRoots.SdkModificator
import com.intellij.openapi.projectRoots.impl.ProjectJdkImpl
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.util.Comparing
import com.intellij.ui.SimpleListCellRenderer
Expand Down Expand Up @@ -190,7 +189,11 @@ class AdditionalDataConfigurable(
)
sdkModificator.sdkAdditionalData = sdkAdditionData
ApplicationManager.getApplication().runWriteAction { sdkModificator.commitChanges() }
(elixirSdk as ProjectJdkImpl).resetVersionString()

// reset the version string to force the sdk to be reloaded
// @todo is this necessary?
this.sdkModificator.versionString = null
this.sdkModificator.commitChanges()
}

override fun reset() {
Expand Down
Loading

0 comments on commit c7146e6

Please sign in to comment.