Skip to content

Commit

Permalink
Merge pull request #503 from koxudaxi/fix-multi-project-formatting-is…
Browse files Browse the repository at this point in the history
…olation

Fix: Isolate Ruff formatting to the active project
  • Loading branch information
koxudaxi authored Sep 12, 2024
2 parents 7f84a18 + ae5ad5f commit 391f1a8
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 35 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]
- Fix: Isolate Ruff formatting to the active project [[#503](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/503)]

## [0.0.38] - 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.38
pluginVersion = 0.0.39

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 242.20224.89
Expand Down
3 changes: 1 addition & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@
<externalAnnotator language="Python" implementationClass="com.koxudaxi.ruff.RuffExternalAnnotator"/>
<platform.backend.documentation.targetProvider
implementation="com.koxudaxi.ruff.RuffNoqaDocumentationTargetProvider"/>
<actionOnSave id="BlackFormatterActionOnSave" implementation="com.koxudaxi.ruff.RuffActionOnSave" order="last"/>
</extensions>
<projectListeners>
<listener class="com.koxudaxi.ruff.RuffFileDocumentManagerListener"
topic="com.intellij.openapi.fileEditor.FileDocumentManagerListener"/>
<listener class="com.koxudaxi.ruff.RuffPackageManagerListener"
topic="com.jetbrains.python.packaging.PyPackageManager$Listener"/>
</projectListeners>
Expand Down
2 changes: 1 addition & 1 deletion src/com/koxudaxi/ruff/RuffConfigService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.intellij.openapi.project.Project
import com.intellij.util.xmlb.XmlSerializerUtil
import org.jetbrains.annotations.SystemDependent

@State(name = "RuffConfigService", storages = [Storage("ruff.xml")])
@Service(Service.Level.PROJECT)
@State(name = "RuffConfigService", storages = [Storage("ruff.xml")])
class RuffConfigService : PersistentStateComponent<RuffConfigService> {
var runRuffOnSave: Boolean = false
var runRuffOnReformatCode: Boolean = true
Expand Down
9 changes: 4 additions & 5 deletions src/com/koxudaxi/ruff/RuffConfigurable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ package com.koxudaxi.ruff
import com.intellij.openapi.options.Configurable
import com.intellij.openapi.project.Project
import com.koxudaxi.ruff.lsp.ClientType
import com.koxudaxi.ruff.lsp.intellij.RuffIntellijLspClient
import com.koxudaxi.ruff.lsp.lsp4ij.RuffLsp4IntellijClient
import javax.swing.JComponent


class RuffConfigurable internal constructor(val project: Project) : Configurable {
private val ruffConfigService: RuffConfigService = RuffConfigService.getInstance(project)
private val configPanel: RuffConfigPanel = RuffConfigPanel(project)
private val ruffCacheService: RuffCacheService = RuffCacheService.getInstance(project)
private val ruffLspClientManager = RuffLspClientManager.getInstance(project)
override fun getDisplayName(): String {
return "Ruff"
}
Expand All @@ -29,6 +24,7 @@ class RuffConfigurable internal constructor(val project: Project) : Configurable
override fun reset() {}

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

override fun apply() {
val ruffConfigService: RuffConfigService = RuffConfigService.getInstance(project)
val ruffCacheService: RuffCacheService = RuffCacheService.getInstance(project)
val ruffLspClientManager = RuffLspClientManager.getInstance(project)
ruffConfigService.runRuffOnSave = configPanel.runRuffOnSave
ruffConfigService.runRuffOnReformatCode = configPanel.runRuffOnReformatCode
ruffConfigService.showRuleCode = configPanel.showRuleCode
Expand Down
22 changes: 0 additions & 22 deletions src/com/koxudaxi/ruff/RuffFileDocumentManagerListener.kt

This file was deleted.

26 changes: 26 additions & 0 deletions src/com/koxudaxi/ruff/RuffFormatOnSaveAction.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.koxudaxi.ruff

import com.intellij.ide.actionsOnSave.impl.ActionsOnSaveFileDocumentManagerListener.ActionOnSave
import com.intellij.openapi.editor.Document
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager

class RuffActionOnSave : ActionOnSave() {
override fun isEnabledForProject(project: Project): Boolean {
val ruffConfigService = RuffConfigService.getInstance(project)
return ruffConfigService.runRuffOnSave
}
override fun processDocuments(project: Project, documents: Array<Document>) {
val ruffApplyService = RuffApplyService.getInstance(project)
val ruffConfigService = RuffConfigService.getInstance(project)
val ruffCacheService = RuffCacheService.getInstance(project)
val psiDocumentManager = PsiDocumentManager.getInstance(project)
val withFormat = ruffConfigService.useRuffFormat && ruffCacheService.hasFormatter()
for (document in documents) {
val psiFile = psiDocumentManager.getPsiFile(document) ?: continue
if (!psiFile.isApplicableTo) continue
if (ruffConfigService.disableOnSaveOutsideOfProject && !psiFile.virtualFile.isInProjectDir(project)) return
ruffApplyService.apply(document, psiFile.sourceFile, withFormat)
}
}
}
10 changes: 6 additions & 4 deletions src/com/koxudaxi/ruff/RuffPackageManagerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.projectRoots.Sdk
import com.jetbrains.python.packaging.PyPackageManager

Check failure on line 7 in src/com/koxudaxi/ruff/RuffPackageManagerListener.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of API marked for removal

'com.jetbrains.python.packaging.PyPackageManager' is deprecated and marked for removal
import com.jetbrains.python.sdk.pythonSdk
import com.koxudaxi.ruff.lsp.ClientType

class RuffPackageManagerListener(project: Project) : PyPackageManager.Listener {
private val ruffConfigService = RuffConfigService.getInstance(project)
private val ruffCacheService = RuffCacheService.getInstance(project)
private val ruffLspClientManager = RuffLspClientManager.getInstance(project)
class RuffPackageManagerListener(private val project: Project) : PyPackageManager.Listener {

Check failure on line 11 in src/com/koxudaxi/ruff/RuffPackageManagerListener.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of API marked for removal

'com.jetbrains.python.packaging.PyPackageManager' is deprecated and marked for removal

Check failure on line 11 in src/com/koxudaxi/ruff/RuffPackageManagerListener.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Usage of API marked for removal

'com.jetbrains.python.packaging.PyPackageManager' is deprecated and marked for removal

override fun packagesRefreshed(sdk: Sdk) {
if (project.pythonSdk != sdk) return
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
if (!lspSupported || !ruffConfigService.enableLsp) return
Expand Down

0 comments on commit 391f1a8

Please sign in to comment.