Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

[feat] GO support #43

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
927fe14
[feature] create go build target |#BAZEL-784 Done
zortenburger Dec 14, 2023
dea7e27
calculateAllGoSdkInfos done
kronosmichall Dec 14, 2023
22e2e8a
added setSdk
kronosmichall Jan 4, 2024
40d76e8
go sdk mock works
kronosmichall Jan 4, 2024
a843252
changed home path to /usr/local/go
kronosmichall Jan 4, 2024
e53c401
added isGoSupportEnabled = true for each module
kronosmichall Jan 15, 2024
5f47add
extension point for Go SDK - not yet full working
maclick Jan 21, 2024
e3f49be
Merge branch 'main' into go-support
maclick Jan 24, 2024
1e1d048
Merge remote-tracking branch 'upstream/main' into go-support
maclick Feb 22, 2024
f77ae5e
clenup
maclick Feb 22, 2024
2de6adc
Work in progress, add internal dependencies
edokimok Mar 12, 2024
f8bd25b
cos
abrams27 Mar 13, 2024
1e8f812
Merge remote-tracking branch 'upstream/main' into go-support
maclick Mar 17, 2024
368bb0c
merge cleanup
maclick Mar 17, 2024
9f5e27b
small style changes
maclick Mar 17, 2024
369b5f7
Fix indentation, cleanup (#4)
edokimok Mar 25, 2024
f275b76
GoModule toState (#3)
kronosmichall Mar 25, 2024
d43fa4e
Merge remote-tracking branch 'upstream/main' into go-support
maclick Mar 25, 2024
cf5f1a1
cleanup after main merge
maclick Mar 25, 2024
ca18964
fix detekt
maclick Mar 25, 2024
b37a80d
Go support sources (#5)
zortenburger Apr 2, 2024
b356780
move go plugin usages to extension point (#6)
maclick Apr 2, 2024
b082f1d
Fix go related data in MMM state (#7)
maclick Apr 2, 2024
47089ed
Merge branch 'upstream-main' into go-support
maclick Apr 3, 2024
ea83181
WIP, working importpath, empty root
edokimok Apr 18, 2024
1bd89ea
pass target root for Go build targets
maclick Apr 24, 2024
4876112
Green code!
edokimok Apr 25, 2024
de6e867
Minor fix
edokimok Apr 25, 2024
d9efbeb
Add shouldAddMarker for Go top level functions
edokimok May 18, 2024
58e66a3
Set should replace to true
edokimok May 23, 2024
c24bbda
Merge remote-tracking branch 'refs/remotes/upstream/main' into backup
edokimok May 23, 2024
a9f3869
Try merge
edokimok May 23, 2024
25c9a70
Merge pull request #10 from edokimok/backup
maclick Jun 4, 2024
d0eb941
Add tests for Go resources
zortenburger Jun 21, 2024
9fa7c74
Merge pull request #11 from zortenburger/go-support-resources-tests
maclick Jun 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ object Plugin {
const val pluginVerifierIdeVersions = "241.14494.17"

object Platform {
const val type = "IC"
const val type = "IU"
const val version = "241.14494.17-EAP-SNAPSHOT"
const val downloadSources = true

// Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
// Example: platformPlugins =" com.intellij.java, com.jetbrains.php:203.4449.22"
val plugins =
listOf("com.intellij.java", "org.jetbrains.kotlin", "PythonCore:241.14494.17", "org.jetbrains.android:241.14494.17")
listOf("com.intellij.java", "org.jetbrains.kotlin", "PythonCore:241.14494.17", "org.jetbrains.android:241.14494.17",
"org.jetbrains.plugins.go:241.14494.17")
}

const val javaVersion = "17"
Expand Down
8 changes: 8 additions & 0 deletions protocol/src/main/kotlin/org/jetbrains/bsp/GoBuildTarget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jetbrains.bsp

import java.net.URI

public data class GoBuildTarget(
val sdkHomePath: URI?,
val importPath: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ch.epfl.scala.bsp4j.PythonBuildTarget
import ch.epfl.scala.bsp4j.ScalaBuildTarget
import com.google.gson.Gson
import com.google.gson.JsonObject
import org.jetbrains.bsp.GoBuildTarget
import org.jetbrains.bsp.protocol.AndroidBuildTarget
import org.jetbrains.bsp.protocol.KotlinBuildTarget

Expand All @@ -32,6 +33,9 @@ public fun extractKotlinBuildTarget(target: BuildTarget): KotlinBuildTarget? =
extractData(target, "kotlin")
?: extractAndroidBuildTarget(target)?.kotlinBuildTarget

public fun extractGoBuildTarget(target: BuildTarget): GoBuildTarget? =
extractData(target, "go")

public fun extractJvmBuildTarget(target: BuildTarget): JvmBuildTarget? =
extractData(target, BuildTargetDataKind.JVM)
?: extractAndroidBuildTarget(target)?.jvmBuildTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.intellij.openapi.util.registry.Registry
private const val PYTHON_SUPPORT = "bsp.python.support"
private const val SCALA_SUPPORT = "bsp.scala.support"
private const val ANDROID_SUPPORT = "bsp.android.support"
private const val GO_SUPPORT = "bsp.go.support"
private const val BUILD_PROJECT_ON_SYNC = "bsp.build.project.on.sync"

public object BspFeatureFlags {
Expand All @@ -17,6 +18,9 @@ public object BspFeatureFlags {
public val isAndroidSupportEnabled: Boolean
get() = Registry.`is`(ANDROID_SUPPORT)

public val isGoSupportEnabled: Boolean
get() = Registry.`is`(GO_SUPPORT)

public val isBuildProjectOnSyncEnabled: Boolean
get() = Registry.`is`(BUILD_PROJECT_ON_SYNC)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.jetbrains.plugins.bsp.extension.points

import ch.epfl.scala.bsp4j.BuildTarget
import com.goide.project.GoModuleSettings
import com.goide.sdk.GoSdk
import com.goide.sdk.GoSdkService
import com.goide.vgo.project.workspaceModel.VgoWorkspaceModelUpdater
import com.intellij.openapi.application.writeAction
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.openapi.module.Module
import com.intellij.openapi.project.Project
import org.jetbrains.bsp.protocol.utils.extractGoBuildTarget
import org.jetbrains.plugins.bsp.magicmetamodel.ProjectDetails

public interface GoSdkGetterExtension {
public suspend fun addGoSdks(
project: Project,
)

public fun calculateAllGoSdkInfos(
projectDetails: ProjectDetails,
)

public fun enableGoSupportForModule(
module: Module,
)

public fun restoreGoModulesRegistry(
project: Project,
)
}

private val ep = ExtensionPointName.create<GoSdkGetterExtension>(
"org.jetbrains.bsp.goSdkGetterExtension",
)

public fun goSdkExtension(): GoSdkGetterExtension? = ep.extensionList.firstOrNull()

public fun goSdkExtensionExists(): Boolean = ep.extensionList.isNotEmpty()

public class GoSdkGetter: GoSdkGetterExtension {
private var goSdks: Set<GoSdk> = emptySet()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extension points shouldnt have state, so i'd be better to keep it in a service with state

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump (you can check DefaultConnectionDetailsProviderExtension for an inspiration)


override suspend fun addGoSdks(
project: Project,
) {
val goSdkService = GoSdkService.getInstance(project)
goSdks.forEach {
writeAction {
goSdkService.setSdk(it)
}
}
}

override fun calculateAllGoSdkInfos(
projectDetails: ProjectDetails,
) {
goSdks = projectDetails.targets
.mapNotNull {
createGoSdk(it)
}
.toSet()
}

override fun enableGoSupportForModule(
module: Module,
) {
GoModuleSettings.getInstance(module).isGoSupportEnabled = true
}

override fun restoreGoModulesRegistry(
project: Project,
) {
VgoWorkspaceModelUpdater(project).restoreModulesRegistry()
}

private fun createGoSdk(target: BuildTarget): GoSdk? =
extractGoBuildTarget(target)?.let {
if (it.sdkHomePath == null) {
GoSdk.NULL
} else {
GoSdk.fromHomePath(it.sdkHomePath?.path)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.BuildTargetI
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.ContentRoot
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GenericModuleInfo
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GenericSourceRoot
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GoAddendum
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GoModule
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GoModuleDependency
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.IntermediateLibraryDependency
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.IntermediateModuleDependency
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.JavaAddendum
Expand Down Expand Up @@ -200,8 +203,9 @@ public data class ModuleState(
var scalaAddendum: ScalaAddendumState? = null,
var javaAddendum: JavaAddendumState? = null,
var androidAddendum: AndroidAddendumState? = null,
var goAddendum: GoAddendumState? = null,
) : ConvertableFromState<Module> {
public fun toJavaModule(): JavaModule = JavaModule(
private fun toJavaModule(): JavaModule = JavaModule(
genericModuleInfo = module.fromState(),
baseDirContentRoot = baseDirContentRoot?.fromState(),
sourceRoots = sourceRoots.map { it.toJavaSourceRoot() },
Expand All @@ -214,17 +218,29 @@ public data class ModuleState(
androidAddendum = androidAddendum?.fromState(),
)

public fun toPythonModule(): PythonModule = PythonModule(
private fun toPythonModule(): PythonModule = PythonModule(
module = module.fromState(),
sourceRoots = sourceRoots.map { it.toGenericSourceRoot() },
libraries = libraries?.map { it.toPythonLibrary() }.orEmpty(),
resourceRoots = resourceRoots.map { it.toResourceRoot() },
sdkInfo = sdkInfo?.fromState(),
)

private fun toGoModule(): GoModule =
GoModule(
module = module.fromState(),
importPath = goAddendum?.importPath ?: "",
root = Path(goAddendum?.root ?: ""),
sourceRoots = sourceRoots.map { it.toGenericSourceRoot() },
resourceRoots = resourceRoots.map { it.toResourceRoot() },
goDependencies = goAddendum?.goDependencies?.map { it.fromState() }.orEmpty()
)

override fun fromState(): Module =
if (module.languageIds.includesPython())
toPythonModule()
else if (module.languageIds.contains("go"))
toGoModule()
else
toJavaModule()
}
Expand Down Expand Up @@ -287,6 +303,28 @@ public data class AndroidAddendumState(
)
}

public data class GoAddendumState(
var importPath: String = "",
var root: String = "",
var goDependencies: List<GoModuleDependencyState> = emptyList(),
) : ConvertableFromState<GoAddendum> {
override fun fromState(): GoAddendum = GoAddendum(
importPath = importPath,
root = Path(root),
goDependencies = goDependencies.map { it.fromState() },
)
}

public data class GoModuleDependencyState(
var importPath: String = "",
var root: String = "",
) : ConvertableFromState<GoModuleDependency> {
override fun fromState(): GoModuleDependency = GoModuleDependency(
importPath = importPath,
root = Path(root),
)
}

public data class ModuleCapabilitiesState(
var canRun: Boolean = false,
var canTest: Boolean = false,
Expand Down Expand Up @@ -318,6 +356,17 @@ public fun AndroidAddendum.toState(): AndroidAddendumState = AndroidAddendumStat
resourceFolders = resourceFolders.map { it.toString() },
)

public fun GoAddendum.toState(): GoAddendumState = GoAddendumState(
importPath = importPath,
root = root.toString(),
goDependencies = goDependencies.map { it.toState() },
)

public fun GoModuleDependency.toState(): GoModuleDependencyState = GoModuleDependencyState(
importPath = importPath,
root = root.toString(),
)

public fun ModuleCapabilities.toState(): ModuleCapabilitiesState = ModuleCapabilitiesState(
canRun = canRun,
canTest = canTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import org.jetbrains.plugins.bsp.magicmetamodel.ProjectDetails
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.BuildTargetId
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.BuildTargetInfo
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.Module
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.transformers.ModuleDetailsToGoModuleTransformer
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.transformers.ModuleDetailsToJavaModuleTransformer
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.transformers.ModuleDetailsToPythonModuleTransformer
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.transformers.ProjectDetailsToModuleDetailsTransformer
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.includesGo
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.includesPython
import java.nio.file.Path

Expand All @@ -32,13 +34,21 @@ public object TargetIdToModuleEntitiesMap {
projectBasePath,
hasDefaultPythonInterpreter,
)
val moduleDetailsToGoModuleTransformer = ModuleDetailsToGoModuleTransformer(
targetsMap,
projectDetails,
moduleNameProvider,
projectBasePath,
)

val transformer = ProjectDetailsToModuleDetailsTransformer(projectDetails)

return projectDetails.targetsId.associate {
val moduleDetails = transformer.moduleDetailsForTargetId(it)
val module = if (moduleDetails.target.languageIds.includesPython()) {
moduleDetailsToPythonModuleTransformer.transform(moduleDetails)
} else if (moduleDetails.target.languageIds.includesGo()) {
moduleDetailsToGoModuleTransformer.transform(moduleDetails)
} else {
moduleDetailsToJavaModuleTransformer.transform(moduleDetails)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel

import org.jetbrains.plugins.bsp.magicmetamodel.impl.ModuleState
import org.jetbrains.plugins.bsp.magicmetamodel.impl.toState
import java.nio.file.Path

public data class GoModuleDependency(
val importPath: String,
val root: Path,
)

public data class GoModule(
val module: GenericModuleInfo,
val sourceRoots: List<GenericSourceRoot>,
val resourceRoots: List<ResourceRoot>,
val importPath: String,
val root: Path,
val goDependencies: List<GoModuleDependency>,
) : WorkspaceModelEntity(), Module {
override fun toState(): ModuleState = ModuleState(
module = module.toState(),
sourceRoots = sourceRoots.map { it.toState() },
resourceRoots = resourceRoots.map { it.toState() },
goAddendum = GoAddendum(importPath, root, goDependencies).toState(),
)

override fun getModuleName(): String = module.name
}

public data class GoAddendum(
var importPath: String,
var root: Path,
val goDependencies: List<GoModuleDependency>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public fun LanguageIds.includesKotlin(): Boolean = contains("kotlin")
public fun LanguageIds.includesJava(): Boolean = contains("java")
public fun LanguageIds.includesScala(): Boolean = contains("scala")
public fun LanguageIds.includesAndroid(): Boolean = contains("android")
public fun LanguageIds.includesGo(): Boolean = contains("go")

public fun LanguageIds.includesJavaOrScala(): Boolean = includesJava() || includesScala()
public fun LanguageIds.isJvmTarget(): Boolean = (includesJava() || includesKotlin()) && !includesAndroid()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.jetbrains.workspacemodel.entities.AndroidTargetType.TEST
import org.jetbrains.workspacemodel.entities.androidAddendumEntity
import org.jetbrains.workspacemodel.entities.jvmBinaryJarsEntity
import java.nio.file.Path
import kotlin.io.path.Path

public object WorkspaceModelToModulesMapTransformer {
public operator fun invoke(
Expand Down Expand Up @@ -95,6 +96,7 @@ public object WorkspaceModelToModulesMapTransformer {
languageIds = entity.customImlData?.customModuleOptions.orEmpty(),
associates = entity.getKotlinSettings()?.getAssociates().orEmpty(),
)
// TODO: add support for GoModule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bump

val module = if (genericModuleInfo.languageIds.includesPython()) {
PythonModule(
module = genericModuleInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import com.intellij.platform.workspace.storage.impl.url.toVirtualFileUrl
import com.intellij.platform.workspace.storage.url.VirtualFileUrl
import com.intellij.platform.workspace.storage.url.VirtualFileUrlManager
import com.intellij.workspaceModel.ide.impl.LegacyBridgeJpsEntitySourceFactory
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.GoModule
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.JavaModule
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.Library
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.Module
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.ModuleName
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.PythonModule
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.WorkspaceModelUpdater
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.GoModuleUpdater
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.JavaModuleUpdater
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.PythonModuleUpdater
import org.jetbrains.plugins.bsp.magicmetamodel.impl.workspacemodel.impl.updaters.WorkspaceModelEntityUpdaterConfig
Expand All @@ -43,6 +45,7 @@ internal class WorkspaceModelUpdaterImpl(
private val javaModuleUpdater =
JavaModuleUpdater(workspaceModelEntityUpdaterConfig, projectBasePath, isAndroidSupportEnabled)
private val pythonModuleUpdater = PythonModuleUpdater(workspaceModelEntityUpdaterConfig, isPythonSupportEnabled)
private val goModuleUpdater = GoModuleUpdater(workspaceModelEntityUpdaterConfig)

private val workspaceModuleRemover = WorkspaceModuleRemover(workspaceModelEntityUpdaterConfig)
private val javaModuleToDummyJavaModulesTransformerHACK =
Expand All @@ -56,6 +59,7 @@ internal class WorkspaceModelUpdaterImpl(
javaModuleUpdater.addEntity(module)
}
is PythonModule -> pythonModuleUpdater.addEntity(module)
is GoModule -> goModuleUpdater.addEntity(module)
}
}

Expand Down
Loading