Skip to content

Commit 391f1a8

Browse files
authored
Merge pull request #503 from koxudaxi/fix-multi-project-formatting-isolation
Fix: Isolate Ruff formatting to the active project
2 parents 7f84a18 + ae5ad5f commit 391f1a8

8 files changed

+40
-35
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## [Unreleased]
4+
- Fix: Isolate Ruff formatting to the active project [[#503](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/503)]
45

56
## [0.0.38] - 2024-09-12
67

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pluginGroup = com.koxudaxi.ruff
44
pluginName = Ruff
55
pluginRepositoryUrl = https://github.com/koxudaxi/ruff-pycharm-plugin
66
# SemVer format -> https://semver.org
7-
pluginVersion = 0.0.38
7+
pluginVersion = 0.0.39
88

99
# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
1010
pluginSinceBuild = 242.20224.89

resources/META-INF/plugin.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
<externalAnnotator language="Python" implementationClass="com.koxudaxi.ruff.RuffExternalAnnotator"/>
2121
<platform.backend.documentation.targetProvider
2222
implementation="com.koxudaxi.ruff.RuffNoqaDocumentationTargetProvider"/>
23+
<actionOnSave id="BlackFormatterActionOnSave" implementation="com.koxudaxi.ruff.RuffActionOnSave" order="last"/>
2324
</extensions>
2425
<projectListeners>
25-
<listener class="com.koxudaxi.ruff.RuffFileDocumentManagerListener"
26-
topic="com.intellij.openapi.fileEditor.FileDocumentManagerListener"/>
2726
<listener class="com.koxudaxi.ruff.RuffPackageManagerListener"
2827
topic="com.jetbrains.python.packaging.PyPackageManager$Listener"/>
2928
</projectListeners>

src/com/koxudaxi/ruff/RuffConfigService.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import com.intellij.openapi.project.Project
88
import com.intellij.util.xmlb.XmlSerializerUtil
99
import org.jetbrains.annotations.SystemDependent
1010

11-
@State(name = "RuffConfigService", storages = [Storage("ruff.xml")])
1211
@Service(Service.Level.PROJECT)
12+
@State(name = "RuffConfigService", storages = [Storage("ruff.xml")])
1313
class RuffConfigService : PersistentStateComponent<RuffConfigService> {
1414
var runRuffOnSave: Boolean = false
1515
var runRuffOnReformatCode: Boolean = true

src/com/koxudaxi/ruff/RuffConfigurable.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ package com.koxudaxi.ruff
33
import com.intellij.openapi.options.Configurable
44
import com.intellij.openapi.project.Project
55
import com.koxudaxi.ruff.lsp.ClientType
6-
import com.koxudaxi.ruff.lsp.intellij.RuffIntellijLspClient
7-
import com.koxudaxi.ruff.lsp.lsp4ij.RuffLsp4IntellijClient
86
import javax.swing.JComponent
97

108

119
class RuffConfigurable internal constructor(val project: Project) : Configurable {
12-
private val ruffConfigService: RuffConfigService = RuffConfigService.getInstance(project)
1310
private val configPanel: RuffConfigPanel = RuffConfigPanel(project)
14-
private val ruffCacheService: RuffCacheService = RuffCacheService.getInstance(project)
15-
private val ruffLspClientManager = RuffLspClientManager.getInstance(project)
1611
override fun getDisplayName(): String {
1712
return "Ruff"
1813
}
@@ -29,6 +24,7 @@ class RuffConfigurable internal constructor(val project: Project) : Configurable
2924
override fun reset() {}
3025

3126
override fun isModified(): Boolean {
27+
val ruffConfigService: RuffConfigService = RuffConfigService.getInstance(project)
3228
return ruffConfigService.runRuffOnSave != configPanel.runRuffOnSave ||
3329
ruffConfigService.runRuffOnReformatCode != configPanel.runRuffOnReformatCode ||
3430
ruffConfigService.showRuleCode != configPanel.showRuleCode ||
@@ -46,6 +42,9 @@ class RuffConfigurable internal constructor(val project: Project) : Configurable
4642
}
4743

4844
override fun apply() {
45+
val ruffConfigService: RuffConfigService = RuffConfigService.getInstance(project)
46+
val ruffCacheService: RuffCacheService = RuffCacheService.getInstance(project)
47+
val ruffLspClientManager = RuffLspClientManager.getInstance(project)
4948
ruffConfigService.runRuffOnSave = configPanel.runRuffOnSave
5049
ruffConfigService.runRuffOnReformatCode = configPanel.runRuffOnReformatCode
5150
ruffConfigService.showRuleCode = configPanel.showRuleCode

src/com/koxudaxi/ruff/RuffFileDocumentManagerListener.kt

-22
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.koxudaxi.ruff
2+
3+
import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener.ActionOnSave
4+
import com.intellij.openapi.editor.Document
5+
import com.intellij.openapi.project.Project
6+
import com.intellij.psi.PsiDocumentManager
7+
8+
class RuffActionOnSave : ActionOnSave() {
9+
override fun isEnabledForProject(project: Project): Boolean {
10+
val ruffConfigService = RuffConfigService.getInstance(project)
11+
return ruffConfigService.runRuffOnSave
12+
}
13+
override fun processDocuments(project: Project, documents: Array<Document>) {
14+
val ruffApplyService = RuffApplyService.getInstance(project)
15+
val ruffConfigService = RuffConfigService.getInstance(project)
16+
val ruffCacheService = RuffCacheService.getInstance(project)
17+
val psiDocumentManager = PsiDocumentManager.getInstance(project)
18+
val withFormat = ruffConfigService.useRuffFormat && ruffCacheService.hasFormatter()
19+
for (document in documents) {
20+
val psiFile = psiDocumentManager.getPsiFile(document) ?: continue
21+
if (!psiFile.isApplicableTo) continue
22+
if (ruffConfigService.disableOnSaveOutsideOfProject && !psiFile.virtualFile.isInProjectDir(project)) return
23+
ruffApplyService.apply(document, psiFile.sourceFile, withFormat)
24+
}
25+
}
26+
}

src/com/koxudaxi/ruff/RuffPackageManagerListener.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import com.intellij.openapi.application.ApplicationManager
55
import com.intellij.openapi.project.Project
66
import com.intellij.openapi.projectRoots.Sdk
77
import com.jetbrains.python.packaging.PyPackageManager
8+
import com.jetbrains.python.sdk.pythonSdk
89
import com.koxudaxi.ruff.lsp.ClientType
910

10-
class RuffPackageManagerListener(project: Project) : PyPackageManager.Listener {
11-
private val ruffConfigService = RuffConfigService.getInstance(project)
12-
private val ruffCacheService = RuffCacheService.getInstance(project)
13-
private val ruffLspClientManager = RuffLspClientManager.getInstance(project)
11+
class RuffPackageManagerListener(private val project: Project) : PyPackageManager.Listener {
1412

1513
override fun packagesRefreshed(sdk: Sdk) {
14+
if (project.pythonSdk != sdk) return
15+
val ruffConfigService = RuffConfigService.getInstance(project)
16+
val ruffCacheService = RuffCacheService.getInstance(project)
17+
val ruffLspClientManager = RuffLspClientManager.getInstance(project)
1618
ruffConfigService.projectRuffExecutablePath = findRuffExecutableInSDK(sdk, false)?.absolutePath
1719
ruffConfigService.projectRuffLspExecutablePath = findRuffExecutableInSDK(sdk, true)?.absolutePath
1820
if (!lspSupported || !ruffConfigService.enableLsp) return

0 commit comments

Comments
 (0)