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

add disableOnSaveOutsideOfProject option #155

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [Unreleased]
- Add disableOnSaveOutsideOfProject option [[#155](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/155)]

## [0.0.12] - 2023-04-19
- Support ruff 0.0.260 [[#144](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/144)]
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.12
pluginVersion = 0.0.13

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 222
Expand Down
3 changes: 3 additions & 0 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ fun parseJsonResponse(response: String): List<Result> = try {
listOf()
}

fun VirtualFile.isInProjectDir(project: Project): Boolean =
project.basePath?.let { canonicalPath?.startsWith(it) } ?: false

fun getProjectRelativeFilePath(project: Project, virtualFile: VirtualFile): String? {
val canonicalPath = virtualFile.canonicalPath ?: return null
return project.basePath?.takeIf { canonicalPath.startsWith(it) }?.let {
Expand Down
16 changes: 12 additions & 4 deletions src/com/koxudaxi/ruff/RuffConfigPanel.form
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</properties>
<border type="none"/>
<children>
<grid id="da542" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="da542" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<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"/>
Expand All @@ -33,29 +33,37 @@
</hspacer>
<component id="d90c1" class="javax.swing.JCheckBox" binding="runRuffOnReformatCodeCheckBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Run ruff when Reformat Code"/>
</properties>
</component>
<component id="d3ece" class="javax.swing.JCheckBox" binding="showRuleCodeCheckBox">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<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"/>
</constraints>
<properties>
<text value="Show Rule Code on inspection message"/>
</properties>
</component>
<component id="61d32" class="javax.swing.JCheckBox" binding="alwaysUseGlobalRuffCheckBox">
<constraints>
<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"/>
<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"/>
</constraints>
<properties>
<actionCommand value="Always use global ruff executable. (if the option isn't checked then use ruff of current project environemnt)"/>
<text value="Always use Global Ruff executable. (if the option isn't checked then use ruff of current project environemnt)"/>
</properties>
</component>
<component id="288de" class="javax.swing.JCheckBox" binding="disableOnSaveOutsideOfProjectCheckBox">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Exclude files outside of the project"/>
</properties>
</component>
</children>
</grid>
<vspacer id="a33f9">
Expand Down
8 changes: 8 additions & 0 deletions src/com/koxudaxi/ruff/RuffConfigPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class RuffConfigPanel(project: Project) {
private lateinit var projectRuffExecutablePathField: JBTextField
private lateinit var ruffConfigPathField: TextFieldWithBrowseButton
private lateinit var clearRuffConfigPathButton: JButton
private lateinit var disableOnSaveOutsideOfProjectCheckBox: JCheckBox

init {
val ruffConfigService = getInstance(project)
Expand All @@ -37,6 +38,11 @@ class RuffConfigPanel(project: Project) {
showRuleCodeCheckBox.isEnabled = true
alwaysUseGlobalRuffCheckBox.isEnabled = true
alwaysUseGlobalRuffCheckBox.isSelected = ruffConfigService.alwaysUseGlobalRuff
disableOnSaveOutsideOfProjectCheckBox.isEnabled = ruffConfigService.runRuffOnSave
disableOnSaveOutsideOfProjectCheckBox.isSelected = ruffConfigService.disableOnSaveOutsideOfProject
runRuffOnSaveCheckBox.addActionListener {
disableOnSaveOutsideOfProjectCheckBox.isEnabled = runRuffOnSaveCheckBox.isSelected
}
setProjectRuffExecutablePath(project)

globalRuffExecutablePathField.apply {
Expand Down Expand Up @@ -85,6 +91,8 @@ class RuffConfigPanel(project: Project) {
get() = alwaysUseGlobalRuffCheckBox.isSelected
val ruffConfigPath: @SystemDependent String?
get() = ruffConfigPathField.text.takeIf { it.isNotBlank() }
val disableOnSaveOutsideOfProject: Boolean
get() = disableOnSaveOutsideOfProjectCheckBox.isSelected
companion object {
const val RUFF_EXECUTABLE_NOT_FOUND = "Ruff executable not found"
}
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 @@ -16,7 +16,7 @@ class RuffConfigService : PersistentStateComponent<RuffConfigService> {
var alwaysUseGlobalRuff: Boolean = false
var projectRuffExecutablePath: @SystemDependent String? = null
var ruffConfigPath: @SystemDependent String? = null

var disableOnSaveOutsideOfProject: Boolean = true

override fun getState(): RuffConfigService {
return this
Expand Down
4 changes: 3 additions & 1 deletion src/com/koxudaxi/ruff/RuffConfigurable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class RuffConfigurable internal constructor(project: Project) : Configurable {
ruffConfigService.showRuleCode != configPanel.showRuleCode ||
ruffConfigService.alwaysUseGlobalRuff != configPanel.alwaysUseGlobalRuff ||
ruffConfigService.globalRuffExecutablePath != configPanel.globalRuffExecutablePath ||
ruffConfigService.ruffConfigPath != configPanel.ruffConfigPath
ruffConfigService.ruffConfigPath != configPanel.ruffConfigPath ||
ruffConfigService.disableOnSaveOutsideOfProject != configPanel.disableOnSaveOutsideOfProject

}

Expand All @@ -40,6 +41,7 @@ class RuffConfigurable internal constructor(project: Project) : Configurable {
ruffConfigService.alwaysUseGlobalRuff = configPanel.alwaysUseGlobalRuff
ruffConfigService.globalRuffExecutablePath = configPanel.globalRuffExecutablePath
ruffConfigService.ruffConfigPath = configPanel.ruffConfigPath
ruffConfigService.disableOnSaveOutsideOfProject = configPanel.disableOnSaveOutsideOfProject
}

override fun disposeUIResources() {
Expand Down
6 changes: 4 additions & 2 deletions src/com/koxudaxi/ruff/RuffFileDocumentManagerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import com.intellij.openapi.editor.Document
import com.intellij.openapi.fileEditor.FileDocumentManagerListener
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiDocumentManager
import com.intellij.testFramework.utils.editor.getVirtualFile

class RuffFileDocumentManagerListener(project: Project) : FileDocumentManagerListener {
class RuffFileDocumentManagerListener(val project: Project) : FileDocumentManagerListener {
private val ruffConfigService = RuffConfigService.getInstance(project)
private val ruffFixService by lazy { RuffFixService.getInstance(project)}
private val psiDocumentManager by lazy { PsiDocumentManager.getInstance(project) }
override fun beforeDocumentSaving(document: Document) {
override fun beforeDocumentSaving(document: Document) {
if (!ruffConfigService.runRuffOnSave) return
val psiFile = psiDocumentManager.getPsiFile(document) ?: return
if (ruffConfigService.disableOnSaveOutsideOfProject && !psiFile.virtualFile.isInProjectDir(project)) return
if (!psiFile.isApplicableTo) return
ruffFixService.fix(document, psiFile)
}
Expand Down