Skip to content

Commit

Permalink
Merge pull request #505 from koxudaxi/Remove_projectRuffExecutablePat…
Browse files Browse the repository at this point in the history
…h_in_config_file

Remove projectRuffExecutablePath in config file
  • Loading branch information
koxudaxi committed Sep 13, 2024
2 parents 784dd00 + 9e53d31 commit 9ea2edd
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- Remove projectRuffExecutablePath in config file [[#505](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/505)]

## [0.0.39] - 2024-09-12

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.koxudaxi.ruff
pluginName = Ruff
pluginRepositoryUrl = https://github.com/koxudaxi/ruff-pycharm-plugin
# SemVer format -> https://semver.org
pluginVersion = 0.0.39
pluginVersion = 0.0.40

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 242.20224.89
Expand Down
33 changes: 20 additions & 13 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,13 @@ val lsp4ijSupported: Boolean

val lspSupported: Boolean get() = intellijLspClientSupported || lsp4ijSupported

fun detectRuffExecutable(project: Project, ruffConfigService: RuffConfigService, lsp: Boolean): File? {
fun detectRuffExecutable(project: Project, ruffConfigService: RuffConfigService, lsp: Boolean, ruffCacheService: RuffCacheService): File? {
project.pythonSdk?.let {
findRuffExecutableInSDK(it, lsp)
}.let {
when {
lsp -> ruffConfigService.projectRuffLspExecutablePath = it?.absolutePath
else -> ruffConfigService.projectRuffExecutablePath = it?.absolutePath
lsp -> ruffCacheService.setProjectRuffLspExecutablePath(it?.absolutePath)
else -> ruffCacheService.setProjectRuffExecutablePath(it?.absolutePath)
}
it
}?.let { return it }
Expand Down Expand Up @@ -238,13 +238,21 @@ val PsiFile.isApplicableTo: Boolean
else -> language.isKindOf(PythonLanguage.getInstance())
}

fun getRuffExecutable(project: Project, ruffConfigService: RuffConfigService, lsp: Boolean): File? {
return when {
lsp -> ruffConfigService.ruffLspExecutablePath
else -> ruffConfigService.ruffExecutablePath
}?.let { File(it) }?.takeIf { it.exists() } ?: detectRuffExecutable(
project, ruffConfigService, lsp
)
fun getRuffExecutable(project: Project, ruffConfigService: RuffConfigService, lsp: Boolean, ruffCacheService: RuffCacheService): File? {
with(ruffConfigService) {
return when {
lsp -> when {
alwaysUseGlobalRuff -> globalRuffLspExecutablePath
else -> ruffCacheService.getProjectRuffLspExecutablePath() ?: globalRuffLspExecutablePath
}
else -> when {
alwaysUseGlobalRuff -> globalRuffExecutablePath
else -> ruffCacheService.getProjectRuffExecutablePath() ?: globalRuffExecutablePath
}
}?.let { File(it) }?.takeIf { it.exists() } ?: detectRuffExecutable(
project, ruffConfigService, lsp, ruffCacheService
)
}
}

fun getConfigArgs(ruffConfigService: RuffConfigService): List<String>? {
Expand Down Expand Up @@ -398,10 +406,9 @@ fun generateCommandArgs(
withoutConfig: Boolean = false
): CommandArgs? {
val ruffConfigService = RuffConfigService.getInstance(project)
val ruffCacheService = RuffCacheService.getInstance(project)
val executable =
ruffConfigService.ruffExecutablePath?.let { File(it) }?.takeIf { it.exists() } ?: detectRuffExecutable(
project, ruffConfigService, false
) ?: return null
getRuffExecutable(project, ruffConfigService, false, ruffCacheService) ?: return null
val customConfigArgs = if (withoutConfig) null else ruffConfigService.ruffConfigPath?.let {
args + listOf(CONFIG_ARG, it)
}
Expand Down
21 changes: 21 additions & 0 deletions src/com/koxudaxi/ruff/RuffCacheService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class RuffCacheService(val project: Project) {
private var hasCheck: Boolean? = null
private var hasLsp: Boolean? = null
private var hasStableServer: Boolean? = null
private var projectRuffExecutablePath: String? = null
private var projectRuffLspExecutablePath: String? = null

fun getVersion(): RuffVersion? {
return version
Expand Down Expand Up @@ -76,6 +78,25 @@ class RuffCacheService(val project: Project) {
internal fun hasStableServer(): Boolean? {
return hasStableServer
}

internal fun getProjectRuffExecutablePath(): String? {
return projectRuffExecutablePath
}

@Synchronized
internal fun setProjectRuffExecutablePath(path: String?) {
projectRuffExecutablePath = path
}

internal fun getProjectRuffLspExecutablePath(): String? {
return projectRuffLspExecutablePath
}

@Synchronized
internal fun setProjectRuffLspExecutablePath(path: String?) {
projectRuffLspExecutablePath = path
}

internal
companion object {
fun hasFormatter(project: Project): Boolean = getInstance(project).hasFormatter()
Expand Down
5 changes: 3 additions & 2 deletions src/com/koxudaxi/ruff/RuffConfigPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RuffConfigPanel(project: Project) {

init {
val ruffConfigService = getInstance(project)
val ruffCacheService = RuffCacheService.getInstance(project)
runRuffOnSaveCheckBox.isSelected = ruffConfigService.runRuffOnSave
runRuffOnSaveCheckBox.isEnabled = true
runRuffOnReformatCodeCheckBox.isSelected = ruffConfigService.runRuffOnReformatCode
Expand Down Expand Up @@ -123,7 +124,7 @@ class RuffConfigPanel(project: Project) {
}
updateLspFields()
alwaysUseGlobalRuffCheckBox.addActionListener { updateProjectExecutableFields() }
when (val projectRuffExecutablePath = ruffConfigService.projectRuffExecutablePath?.takeIf { File(it).exists() } ?: getProjectRuffExecutablePath(project, false)) {
when (val projectRuffExecutablePath = ruffCacheService.getProjectRuffExecutablePath()?.takeIf { File(it).exists() } ?: getProjectRuffExecutablePath(project, false)) {
is String -> projectRuffExecutablePathField.text = projectRuffExecutablePath
else -> {
projectRuffExecutablePathField.text = ""
Expand All @@ -132,7 +133,7 @@ class RuffConfigPanel(project: Project) {
}


when (val projectRuffLspExecutablePath = ruffConfigService.projectRuffLspExecutablePath?.takeIf { File(it).exists() } ?: getProjectRuffExecutablePath(project, true)) {
when (val projectRuffLspExecutablePath = ruffCacheService.getProjectRuffLspExecutablePath()?.takeIf { File(it).exists() } ?: getProjectRuffExecutablePath(project, true)) {
is String -> projectRuffLspExecutablePathField.text = projectRuffLspExecutablePath
else -> {
projectRuffLspExecutablePathField.text = ""
Expand Down
21 changes: 1 addition & 20 deletions src/com/koxudaxi/ruff/RuffConfigService.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.koxudaxi.ruff

import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil
import org.jetbrains.annotations.SystemDependent
Expand All @@ -17,8 +14,6 @@ class RuffConfigService : PersistentStateComponent<RuffConfigService> {
var globalRuffExecutablePath: @SystemDependent String? = null
var globalRuffLspExecutablePath: @SystemDependent String? = null
var alwaysUseGlobalRuff: Boolean = false
var projectRuffExecutablePath: @SystemDependent String? = null
var projectRuffLspExecutablePath: @SystemDependent String? = null
var ruffConfigPath: @SystemDependent String? = null
var disableOnSaveOutsideOfProject: Boolean = true
var useRuffLsp: Boolean = false
Expand All @@ -35,20 +30,6 @@ class RuffConfigService : PersistentStateComponent<RuffConfigService> {
override fun loadState(config: RuffConfigService) {
XmlSerializerUtil.copyBean(config, this)
}
val ruffExecutablePath: @SystemDependent String?
get() {
return when {
alwaysUseGlobalRuff -> globalRuffExecutablePath
else -> projectRuffExecutablePath ?: globalRuffExecutablePath
}
}
val ruffLspExecutablePath: @SystemDependent String?
get() {
return when {
alwaysUseGlobalRuff -> globalRuffLspExecutablePath
else -> projectRuffLspExecutablePath ?: globalRuffLspExecutablePath
}
}
companion object {
fun getInstance(project: Project): RuffConfigService {
return project.getService(RuffConfigService::class.java)
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/ruff/RuffPackageManagerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class RuffPackageManagerListener(private val project: Project) : PyPackageManage
val ruffConfigService = RuffConfigService.getInstance(project)
val ruffCacheService = RuffCacheService.getInstance(project)
val ruffLspClientManager = RuffLspClientManager.getInstance(project)
ruffConfigService.projectRuffExecutablePath = findRuffExecutableInSDK(sdk, false)?.absolutePath
ruffConfigService.projectRuffLspExecutablePath = findRuffExecutableInSDK(sdk, true)?.absolutePath
ruffCacheService.setProjectRuffExecutablePath(findRuffExecutableInSDK(sdk, false)?.absolutePath)
ruffCacheService.setProjectRuffLspExecutablePath(findRuffExecutableInSDK(sdk, true)?.absolutePath)
if (!lspSupported || !ruffConfigService.enableLsp) return
ruffCacheService.setVersion {
ApplicationManager.getApplication().invokeLater {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RuffLspServerSupportProvider : LspServerSupportProvider {
if (ruffCacheService.getVersion() == null) return
val descriptor = when {
ruffConfigService.useRuffServer && ruffCacheService.hasLsp() == true -> {
getRuffExecutable(project, ruffConfigService, false)?.let {
getRuffExecutable(project, ruffConfigService, false, ruffCacheService)?.let {
RuffServerServerDescriptor(
project,
it,
Expand All @@ -36,7 +36,7 @@ class RuffLspServerSupportProvider : LspServerSupportProvider {
}
}

else -> getRuffExecutable(project, ruffConfigService, true)?.let {
else -> getRuffExecutable(project, ruffConfigService, true, ruffCacheService)?.let {
RuffLspServerDescriptor(
project,
it,
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/ruff/lsp/lsp4ij/RuffLanguageServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class RuffLanguageServer(project: Project) : ProcessStreamConnectionProvider() {

val (ruffFile, args) = when {
ruffConfigService.useRuffServer && ruffCacheService.hasLsp() == true -> {
val executable = getRuffExecutable(project, ruffConfigService, false) ?: return null
val executable = getRuffExecutable(project, ruffConfigService, false, ruffCacheService) ?: return null
executable to project.LSP_ARGS + (getConfigArgs(ruffConfigService) ?: listOf())
}
else -> {
val executable = getRuffExecutable(project, ruffConfigService, true) ?: return null
val executable = getRuffExecutable(project, ruffConfigService, true, ruffCacheService) ?: return null
executable to listOf()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RuffLanguageServerFactory : LanguageServerFactory, LanguageServerEnablemen
val ruffCacheService = RuffCacheService.getInstance(project)
if (ruffCacheService.getVersion() == null) return false
if (ruffConfigService.useRuffServer && ruffCacheService.hasLsp() != true) return false
if (ruffConfigService.useRuffLsp && getRuffExecutable(project, ruffConfigService, true) == null ) return false
if (ruffConfigService.useRuffLsp && getRuffExecutable(project, ruffConfigService, true, ruffCacheService) == null ) return false
return true
}

Expand Down

0 comments on commit 9ea2edd

Please sign in to comment.