Skip to content

Commit dae928b

Browse files
authored
Merge pull request #581 from koxudaxi/feat-disable-import-optimizer
feat: Add opt-in setting for Ruff import optimizer
2 parents b7ac306 + 4d53eec commit dae928b

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## [Unreleased]
4+
- feat: Add opt-in setting for Ruff import optimizer [[#581](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/581)]
45

56
## [0.0.46] - 2025-03-20
67

src/com/koxudaxi/ruff/RuffConfigPanel.form

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</properties>
1111
<border type="none"/>
1212
<children>
13-
<grid id="da542" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
13+
<grid id="da542" layout-manager="GridLayoutManager" row-count="10" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1414
<margin top="0" left="0" bottom="0" right="0"/>
1515
<constraints>
1616
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -41,15 +41,15 @@
4141
</component>
4242
<component id="d3ece" class="javax.swing.JCheckBox" binding="showRuleCodeCheckBox">
4343
<constraints>
44-
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
44+
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
4545
</constraints>
4646
<properties>
4747
<text value="Show Rule Code on inspection message"/>
4848
</properties>
4949
</component>
5050
<component id="61d32" class="javax.swing.JCheckBox" binding="alwaysUseGlobalRuffCheckBox">
5151
<constraints>
52-
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
52+
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
5353
</constraints>
5454
<properties>
5555
<actionCommand value="Always use global ruff executable. (if the option isn't checked then use ruff of current project environment)"/>
@@ -66,7 +66,7 @@
6666
</component>
6767
<component id="d521d" class="javax.swing.JCheckBox" binding="useRuffFormatCheckBox">
6868
<constraints>
69-
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
69+
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
7070
</constraints>
7171
<properties>
7272
<text value="Use ruff format for version 0.0.289 or later"/>
@@ -75,7 +75,7 @@
7575
<grid id="36978" layout-manager="GridLayoutManager" row-count="6" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
7676
<margin top="0" left="0" bottom="0" right="0"/>
7777
<constraints>
78-
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
78+
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
7979
</constraints>
8080
<properties/>
8181
<border type="none" title="Language Server Protocol (LSP)"/>
@@ -235,12 +235,20 @@
235235
</grid>
236236
<component id="a7b0a" class="javax.swing.JCheckBox" binding="enableRuffLoggingCheckBox">
237237
<constraints>
238-
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
238+
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
239239
</constraints>
240240
<properties>
241241
<text value="Enable Ruff Logging"/>
242242
</properties>
243243
</component>
244+
<component id="f69fd" class="javax.swing.JCheckBox" binding="useRuffImportOptimizerCheckBox">
245+
<constraints>
246+
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
247+
</constraints>
248+
<properties>
249+
<text value="Use Import Optimizer"/>
250+
</properties>
251+
</component>
244252
</children>
245253
</grid>
246254
<vspacer id="a33f9">

src/com/koxudaxi/ruff/RuffConfigPanel.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class RuffConfigPanel(project: Project) {
4848
private lateinit var formattingFeatureCheckBox: JCheckBox
4949
private lateinit var diagnosticFeatureCheckBox: JCheckBox
5050
private lateinit var hoverFeatureCheckBox: JCheckBox
51+
private lateinit var useRuffImportOptimizerCheckBox: JCheckBox
5152

5253

5354

@@ -80,6 +81,8 @@ class RuffConfigPanel(project: Project) {
8081
formattingFeatureCheckBox.isSelected = ruffConfigService.formattingFeature
8182
diagnosticFeatureCheckBox.isSelected = ruffConfigService.diagnosticFeature
8283
hoverFeatureCheckBox.isSelected = ruffConfigService.hoverFeature
84+
useRuffImportOptimizerCheckBox.isSelected = ruffConfigService.useRuffImportOptimizer
85+
useRuffImportOptimizerCheckBox.isEnabled = true
8386

8487
lsp4ijLinkLabel.addMouseListener(object : MouseAdapter() {
8588
override fun mouseClicked(e: MouseEvent?) {
@@ -283,6 +286,8 @@ class RuffConfigPanel(project: Project) {
283286
get() = diagnosticFeatureCheckBox.isSelected
284287
val hoverFeature: Boolean
285288
get() = hoverFeatureCheckBox.isSelected
289+
val useRuffImportOptimizer: Boolean
290+
get() = useRuffImportOptimizerCheckBox.isSelected
286291
companion object {
287292
const val RUFF_EXECUTABLE_NOT_FOUND = "Ruff executable not found"
288293
const val RUFF_LSP_EXECUTABLE_NOT_FOUND = "Ruff-lsp executable not found"

src/com/koxudaxi/ruff/RuffConfigService.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class RuffConfigService : PersistentStateComponent<RuffConfigService> {
2626
var codeActionFeature: Boolean = true
2727
var diagnosticFeature: Boolean = true
2828
var formattingFeature: Boolean = true
29+
var useRuffImportOptimizer: Boolean = false
2930
var hoverFeature: Boolean = true
3031

3132
override fun getState(): RuffConfigService {

src/com/koxudaxi/ruff/RuffConfigurable.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class RuffConfigurable internal constructor(val project: Project) : Configurable
4848
ruffConfigService.codeActionFeature != configPanel.codeActionFeature ||
4949
ruffConfigService.diagnosticFeature != configPanel.diagnosticFeature ||
5050
ruffConfigService.formattingFeature != configPanel.formattingFeature ||
51-
ruffConfigService.hoverFeature != configPanel.hoverFeature
51+
ruffConfigService.hoverFeature != configPanel.hoverFeature ||
52+
ruffConfigService.useRuffImportOptimizer != configPanel.useRuffImportOptimizer
5253

5354

5455
}
@@ -110,6 +111,8 @@ class RuffConfigurable internal constructor(val project: Project) : Configurable
110111
ruffConfigService.formattingFeature = configPanel.formattingFeature
111112
ruffConfigService.hoverFeature = configPanel.hoverFeature
112113

114+
ruffConfigService.useRuffImportOptimizer = configPanel.useRuffImportOptimizer
115+
113116
if (wasUsingLsp4ij != willUseLsp4ij) {
114117
RuffLoggingService.log(project, "LSP client type changed, shutting down previous client")
115118

src/com/koxudaxi/ruff/RuffImportOptimizer.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import com.intellij.psi.PsiFile
77
import com.jetbrains.python.psi.PyUtil
88

99
class RuffImportOptimizer : ImportOptimizer {
10-
override fun supports(psiFile: PsiFile): Boolean = psiFile.isApplicableTo
10+
override fun supports(psiFile: PsiFile): Boolean {
11+
val config = psiFile.project.configService
12+
return config.useRuffImportOptimizer && psiFile.isApplicableTo
13+
}
1114

1215
override fun processFile(psiFile: PsiFile): Runnable {
1316
val project = psiFile.project
@@ -16,8 +19,7 @@ class RuffImportOptimizer : ImportOptimizer {
1619

1720
return Runnable {
1821
val stdin = sourceFile.asCurrentTextStdin ?: return@Runnable
19-
runRuffInBackground(project, stdin, project.OPTIMIZE_IMPORTS_ARGS, "Optimize Imports with Ruff") {
20-
result ->
22+
runRuffInBackground(project, stdin, project.OPTIMIZE_IMPORTS_ARGS, "Optimize Imports with Ruff") { result ->
2123
if (result != null && result.isNotBlank()) {
2224
ApplicationManager.getApplication().invokeLater {
2325
CommandProcessor.getInstance().executeCommand(

0 commit comments

Comments
 (0)