Skip to content

Commit

Permalink
Revert "convert sdk code to kotlin, and fix edt errors (KronicDeth#3631
Browse files Browse the repository at this point in the history
…)"

This reverts commit dcfbf1a.
  • Loading branch information
joshuataylor committed Aug 20, 2024
1 parent 7161299 commit 5a553e0
Show file tree
Hide file tree
Showing 21 changed files with 1,170 additions and 1,024 deletions.
33 changes: 12 additions & 21 deletions src/org/elixir_lang/Elixir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ object Elixir {
environment: Map<String, String>,
workingDirectory: String?,
elixirSdk: Sdk,
erlArgumentList: kotlin.collections.List<String> = emptyList(),
erlArgumentList: kotlin.collections.List<String> = emptyList()
): GeneralCommandLine {
val erlangSdk = elixirSdkToEnsuredErlangSdk(elixirSdk)
val commandLine =
org.elixir_lang.Erl.commandLine(
pty = false,
environment = environment,
workingDirectory = workingDirectory,
erlangSdk = erlangSdk,
)
val commandLine = org.elixir_lang.Erl.commandLine(
pty = false,
environment = environment,
workingDirectory = workingDirectory,
erlangSdk = erlangSdk
)
// MUST be before `addElixir` because it ends with `-extra` which turns off argument parsing for `erl`
commandLine.addParameters(erlArgumentList)
addElixir(commandLine, elixirSdk, erlangSdk)
Expand All @@ -36,17 +35,13 @@ object Elixir {
?: throw MissingErlangSdk(elixirSdk)

fun elixirSdkHasErlangSdk(elixirSdk: Sdk): Boolean = elixirSdkToErlangSdk(elixirSdk) != null

fun elixirSdkToErlangSdk(elixirSdk: Sdk): Sdk? = elixirSdk.sdkAdditionalData?.let { it as SdkAdditionalData }?.getErlangSdk()
fun elixirSdkToErlangSdk(elixirSdk: Sdk): Sdk? =
elixirSdk.sdkAdditionalData?.let { it as SdkAdditionalData }?.erlangSdk

/**
* Adds `-pa ebinDirectory` for those in the `elixirSdk` that aren't in the `erlangSdk`
*/
fun prependNewCodePaths(
commandLine: GeneralCommandLine,
elixirSdk: Sdk,
erlangSdk: Sdk,
) {
fun prependNewCodePaths(commandLine: GeneralCommandLine, elixirSdk: Sdk, erlangSdk: Sdk) {
val elixirEbinDirectories = elixirSdk.ebinDirectories()
val erlangEbinDirectories = erlangSdk.ebinDirectories()
prependNewCodePaths(commandLine, elixirEbinDirectories, erlangEbinDirectories)
Expand All @@ -55,11 +50,7 @@ object Elixir {
/**
* Keep in-suync with [org.elixir_lang.jps.Builder.addElixir]
*/
private fun addElixir(
commandLine: GeneralCommandLine,
elixirSdk: Sdk,
erlangSdk: Sdk,
) {
private fun addElixir(commandLine: GeneralCommandLine, elixirSdk: Sdk, erlangSdk: Sdk) {
prependNewCodePaths(commandLine, elixirSdk, erlangSdk)
commandLine.addParameters("-noshell", "-s", "elixir", "start_cli")
commandLine.addParameters("-elixir", "ansi_enabled", "true")
Expand All @@ -69,7 +60,7 @@ object Elixir {
private fun prependNewCodePaths(
commandLine: GeneralCommandLine,
elixirEbinDirectories: kotlin.collections.List<String>,
erlangEbinDirectories: kotlin.collections.List<String>,
erlangEbinDirectories: kotlin.collections.List<String>
) {
val newEbinDirectories = elixirEbinDirectories - erlangEbinDirectories
prependCodePaths(commandLine, newEbinDirectories)
Expand Down
2 changes: 1 addition & 1 deletion src/org/elixir_lang/facet/configurable/Provider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import com.intellij.openapi.options.ConfigurableProvider
import org.elixir_lang.sdk.ProcessOutput

class Provider(private val project: com.intellij.openapi.project.Project): ConfigurableProvider() {
override fun canCreateConfigurable(): Boolean = ProcessOutput.isSmallIde
override fun canCreateConfigurable(): Boolean = ProcessOutput.isSmallIde()
override fun createConfigurable(): Configurable = Project(project)
}
2 changes: 1 addition & 1 deletion src/org/elixir_lang/facet/sdks/Provider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.intellij.openapi.options.ConfigurableProvider
import org.elixir_lang.sdk.ProcessOutput

abstract class Provider : ConfigurableProvider() {
override fun canCreateConfigurable(): Boolean = ProcessOutput.isSmallIde
override fun canCreateConfigurable(): Boolean = ProcessOutput.isSmallIde()
}
110 changes: 50 additions & 60 deletions src/org/elixir_lang/notification/setup_sdk/Provider.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.elixir_lang.notification.setup_sdk

import com.intellij.ide.actions.ShowSettingsUtilImpl
import com.intellij.openapi.application.ReadAction
import com.intellij.openapi.fileEditor.FileEditor
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleType
Expand All @@ -15,63 +14,53 @@ import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.ui.EditorNotificationPanel
import com.intellij.ui.EditorNotificationProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.elixir_lang.ElixirFileType
import org.elixir_lang.ElixirLanguage
import org.elixir_lang.sdk.ProcessOutput
import org.elixir_lang.sdk.elixir.Type
import java.util.function.Function
import javax.swing.JComponent

/**
* https://github.com/ignatov/intellij-erlang/blob/master/src/org/intellij/erlang/inspection/SetupSDKNotificationProvider.java
*/
class Provider : EditorNotificationProvider {
override fun collectNotificationData(
project: Project,
file: VirtualFile,
file: VirtualFile
): Function<in FileEditor, out JComponent?> =
Function { fileEditor ->
kotlinx.coroutines.runBlocking {
createNotificationPanel(file, project)
}
}
Function<FileEditor, JComponent?> { createNotificationPanel(file, project) }

private suspend fun createNotificationPanel(
private fun createNotificationPanel(
virtualFile: VirtualFile,
project: Project,
project: Project
): EditorNotificationPanel? =
withContext(Dispatchers.Default) {
ReadAction.compute<EditorNotificationPanel?, Throwable> {
if (virtualFile.fileType is ElixirFileType) {
PsiManager
.getInstance(project)
.findFile(virtualFile)
?.let { psiFile ->
if (psiFile.language === ElixirLanguage &&
Type.mostSpecificSdk(psiFile) == null
) {
createPanel(project, psiFile)
} else {
null
}
}
} else {
null
if (virtualFile.fileType is ElixirFileType) {
PsiManager
.getInstance(project)
.findFile(virtualFile)
?.let { psiFile ->
if (psiFile.language === ElixirLanguage &&
Type.mostSpecificSdk(psiFile) == null
) {
createPanel(project, psiFile)
} else {
null
}
}
}
} else {
null
}

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

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

Expand All @@ -89,62 +78,63 @@ class Provider : EditorNotificationProvider {
ShowSettingsUtilImpl.showSettingsDialog(project, "language", "Elixir")
}

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

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

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

private fun createPanel(
project: Project,
psiFile: PsiFile,
): EditorNotificationPanel? {
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)
}
return if (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)
}
} else {
if (ProcessOutput.isSmallIde()) {
createSmallIDEFacetPanel(project)
} else {
createProjectPanel(project)
}
ProcessOutput.isSmallIde -> createSmallIDEFacetPanel(project)
else -> createProjectPanel(project)
}
}

private fun createProjectPanel(project: Project): EditorNotificationPanel =
EditorNotificationPanel().apply {
private fun createProjectPanel(project: Project): EditorNotificationPanel {
return EditorNotificationPanel().apply {
text = "Project SDK is not defined"
createActionLabel(ProjectBundle.message("project.sdk.setup")) {
showProjectSettings(project)
}
}

}
}
}
102 changes: 102 additions & 0 deletions src/org/elixir_lang/sdk/ProcessOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.elixir_lang.sdk;

import com.google.common.base.Charsets;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.CapturingProcessHandler;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.Function;
import com.intellij.util.PlatformUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.util.List;

/**
* Created by zyuyou on 2015/5/27.
*
*/
public class ProcessOutput {
/*
* CONSTANTS
*/

private static final Logger LOGGER = Logger.getInstance(ProcessOutput.class);
public static final int STANDARD_TIMEOUT = 10 * 1000;

@Nullable
public static <T> T transformStdoutLine(@NotNull com.intellij.execution.process.ProcessOutput output, @NotNull Function<String, T> lineTransformer) {
List<String> lines;

if (output.getExitCode() != 0 || output.isTimeout() || output.isCancelled()) {
lines = ContainerUtil.emptyList();
} else {
lines = output.getStdoutLines();
}

T transformed = null;

for (String line : lines) {
transformed = lineTransformer.fun(line);

if (transformed != null) {
break;
}
}

return transformed;
}

@Nullable
public static <T> T transformStdoutLine(@NotNull Function<String, T> lineTransformer,
int timeout,
@NotNull String workDir,
@NotNull String exePath,
@NotNull String... arguments) {
T transformed = null;

try {
com.intellij.execution.process.ProcessOutput output = getProcessOutput(timeout, workDir, exePath, arguments);

transformed = transformStdoutLine(output, lineTransformer);
} catch (ExecutionException executionException) {
LOGGER.warn(executionException);
}

return transformed;
}

@NotNull
public static com.intellij.execution.process.ProcessOutput getProcessOutput(int timeout,
@Nullable String workDir,
@NotNull String exePath,
@NotNull String... arguments) throws ExecutionException{
if(workDir == null || !new File(workDir).isDirectory() || !new File(exePath).canExecute()){
return new com.intellij.execution.process.ProcessOutput();
}

GeneralCommandLine cmd = new GeneralCommandLine().withCharset(Charsets.UTF_8);
cmd.withWorkDirectory(workDir);
cmd.setExePath(exePath);
cmd.addParameters(arguments);

return execute(cmd, timeout);
}

@NotNull
public static com.intellij.execution.process.ProcessOutput execute(@NotNull GeneralCommandLine cmd) throws ExecutionException {
return execute(cmd, STANDARD_TIMEOUT);
}

@NotNull
public static com.intellij.execution.process.ProcessOutput execute(@NotNull GeneralCommandLine cmd, int timeout) throws ExecutionException {
CapturingProcessHandler processHandler = new CapturingProcessHandler(cmd);
return timeout < 0 ? processHandler.runProcess() : processHandler.runProcess(timeout);
}

public static boolean isSmallIde(){
return !(PlatformUtils.isIntelliJ() || PlatformUtils.getPlatformPrefix().equals("AndroidStudio"));
}
}
Loading

0 comments on commit 5a553e0

Please sign in to comment.