Skip to content

Commit

Permalink
feat: add listener/callback to return the sdks for a workspace folder
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Oct 8, 2024
1 parent d49cab2 commit 0bb3452
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
27 changes: 1 addition & 26 deletions src/main/kotlin/snyk/common/lsp/LanguageServerWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.io.toNioPathOrNull
import com.intellij.openapi.vfs.VirtualFile
import io.snyk.plugin.getCliFile
Expand Down Expand Up @@ -401,35 +397,14 @@ class LanguageServerWrapper(
try {
val param = ExecuteCommandParams()
param.command = COMMAND_WORKSPACE_FOLDER_SCAN
param.arguments = listOf(folder, getSdks(project))
logger.warn(Gson().toJson(param))
param.arguments = listOf(folder)
languageServer.workspaceService.executeCommand(param)
} catch (e: Exception) {
logger.error("error calling scan command from language server. re-initializing", e)
restart()
}
}

private fun getSdks(project: Project): List<LsSdk> {
val list: MutableList<LsSdk> = mutableListOf()
val modules = ModuleManager.getInstance(project).modules
for (module in modules) {
val moduleSdk = ModuleRootManager.getInstance(module).sdk
addSdkToList(moduleSdk, list)
}
return list.toList()
}

private fun addSdkToList(
sdk: Sdk?,
list: MutableList<LsSdk>
) {
if (sdk != null && sdk.homeDirectory != null) {
list.add(LsSdk(sdk.sdkType.name, FileUtil.toSystemDependentName(sdk.homeDirectory!!.path)))
}
}


private fun restart() {
runInBackground("Snyk: restarting language server...") {
shutdown()
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/snyk/common/lsp/SnykLanguageClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectLocator
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.guessProjectForFile
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.io.toNioPathOrNull
import com.intellij.openapi.vfs.VfsUtilCore
Expand All @@ -35,13 +36,16 @@ import org.eclipse.lsp4j.ProgressParams
import org.eclipse.lsp4j.PublishDiagnosticsParams
import org.eclipse.lsp4j.ShowMessageRequestParams
import org.eclipse.lsp4j.WorkDoneProgressCreateParams
import org.eclipse.lsp4j.WorkspaceFolder
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest
import org.eclipse.lsp4j.services.LanguageClient
import org.jetbrains.concurrency.runAsync
import snyk.common.ProductType
import snyk.common.editor.DocumentChanger
import snyk.common.lsp.progress.ProgressManager
import snyk.common.lsp.settings.FolderConfigSettings
import snyk.sdk.SdkHelper
import snyk.trust.WorkspaceTrustService
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.CompletableFuture
Expand Down Expand Up @@ -278,6 +282,13 @@ class SnykLanguageClient :
}
}

@JsonRequest(value = "$/snyk.getSDKs")
fun getSdks(workspaceFolder: WorkspaceFolder) : List<LsSdk> {
val project = guessProjectForFile(workspaceFolder.uri.toVirtualFile()) ?: return emptyList()
return SdkHelper.getSdks(project)
}


@JsonNotification(value = "$/snyk.addTrustedFolders")
fun addTrustedPaths(param: SnykTrustedFoldersParams) {
if (disposed) return
Expand Down
31 changes: 31 additions & 0 deletions src/main/kotlin/snyk/sdk/SdkHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package snyk.sdk

import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.util.io.FileUtil
import snyk.common.lsp.LsSdk

class SdkHelper {
companion object {
fun getSdks(project: Project): List<LsSdk> {
val list: MutableList<LsSdk> = mutableListOf()
val modules = ModuleManager.getInstance(project).modules
for (module in modules) {
val moduleSdk = ModuleRootManager.getInstance(module).sdk
addSdkToList(moduleSdk, list)
}
return list.toList()
}

private fun addSdkToList(
sdk: Sdk?,
list: MutableList<LsSdk>
) {
if (sdk != null && sdk.homeDirectory != null) {
list.add(LsSdk(sdk.sdkType.name, FileUtil.toSystemDependentName(sdk.homeDirectory!!.path)))
}
}
}
}

0 comments on commit 0bb3452

Please sign in to comment.