Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use configured sdk in language server [IDE-688] #620

Merged
merged 9 commits into from
Oct 10, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
- add inline value support for display of vulnerability count
- added ai fix feedback support
- enable for IntelliJ 2024.3 platform
- require LS protocol version 16
- transmit project sdks to language server when requested by a scan

### Fixes
- add name to code vision provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.UUID
storages = [Storage("snyk.settings.xml", roamingType = RoamingType.DISABLED)],
)
class SnykApplicationSettingsStateService : PersistentStateComponent<SnykApplicationSettingsStateService> {
val requiredLsProtocolVersion = 15
val requiredLsProtocolVersion = 16

var useTokenAuthentication = false
var currentLSProtocolVersion: Int? = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.snyk.plugin.SnykFile
import io.snyk.plugin.getSnykCachedResults
import io.snyk.plugin.toLanguageServerURL
import io.snyk.plugin.toSnykFileSet
import io.snyk.plugin.ui.toolwindow.SnykPluginDisposable
import org.eclipse.lsp4j.DidSaveTextDocumentParams
import org.eclipse.lsp4j.TextDocumentIdentifier
import org.jetbrains.annotations.TestOnly
Expand Down Expand Up @@ -122,6 +123,7 @@ class LanguageServerBulkFileListener : SnykBulkFileListener() {

VirtualFileManager.getInstance().asyncRefresh()
invokeLater {
if (SnykPluginDisposable.getInstance(project).isDisposed() || project.isDisposed) return@invokeLater
DaemonCodeAnalyzer.getInstance(project).restart()
}
}
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 = "workspace/snyk.sdks")
fun getSdks(workspaceFolder: WorkspaceFolder) : CompletableFuture<List<LsSdk>> {
val project = guessProjectForFile(workspaceFolder.uri.toVirtualFile()) ?: return CompletableFuture.completedFuture(emptyList())
return CompletableFuture.completedFuture(SdkHelper.getSdks(project))
}


@JsonNotification(value = "$/snyk.addTrustedFolders")
fun addTrustedPaths(param: SnykTrustedFoldersParams) {
if (disposed) return
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/snyk/common/lsp/Types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import javax.swing.Icon

typealias FilterableIssueType = String

data class LsSdk(val type: String, val path: String)

data class CliError(
val code: Int? = 0,
val error: String? = null,
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)))
}
}
}
}
Loading