Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: koxudaxi/ruff-pycharm-plugin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.0.5
Choose a base ref
...
head repository: koxudaxi/ruff-pycharm-plugin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.0.6
Choose a head ref
  • 10 commits
  • 9 files changed
  • 2 contributors

Commits on Jan 17, 2023

  1. Copy the full SHA
    39f90b5 View commit details
  2. Merge pull request #55 from koxudaxi/changelog-update-v0.0.5

    Changelog update - `v0.0.5`
    koxudaxi authored Jan 17, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    25ada23 View commit details

Commits on Jan 22, 2023

  1. Copy the full SHA
    fee4622 View commit details
  2. Update CHANGELOG.md

    koxudaxi committed Jan 22, 2023
    Copy the full SHA
    7a895b4 View commit details
  3. Update version

    koxudaxi committed Jan 22, 2023
    Copy the full SHA
    783f509 View commit details
  4. Merge pull request #59 from koxudaxi/support_file_path

    Support file path when run ruff command
    koxudaxi authored Jan 22, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    bc6f304 View commit details
  5. Copy the full SHA
    fe5490f View commit details
  6. Remove braces

    koxudaxi committed Jan 22, 2023
    Copy the full SHA
    448a23e View commit details
  7. Update CHANGELOG.md

    koxudaxi committed Jan 22, 2023
    Copy the full SHA
    d711194 View commit details
  8. Merge pull request #60 from koxudaxi/Support_showing_rule_code_in_ins…

    …pection_message
    
    Support showing rule code in inspection message
    koxudaxi authored Jan 22, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    9a87ce5 View commit details
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## Unreleased
- Support file path when run ruff command [[#59](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/59)]
- Support showing rule code in inspection message [[#60](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/60)]

## 0.0.5 - 2023-01-17
- Fix system ruff detection [[#53](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/53)]

## 0.0.4 - 2023-01-05
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
pluginGroup = com.koxudaxi.ruff
pluginName = Ruff
# SemVer format -> https://semver.org
pluginVersion = 0.0.5
pluginVersion = 0.0.6

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 222
13 changes: 12 additions & 1 deletion src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ fun runRuff(module: Module, stdin: ByteArray?, vararg args: String): String {
val executable = module.pythonSdk?.let { getRuffExecutableInSDK(it) } ?: getRuffExecutable()
?: throw PyExecutionException("Cannot find Ruff", "ruff", emptyList(), ProcessOutput())

return runCommand(executable, module.basePath, stdin, *args)
return runCommand(executable, module.project.basePath, stdin, *args)
}

inline fun <reified T> runRuffInBackground(
@@ -156,3 +156,14 @@ inline fun <reified T> executeOnPooledThread(
}

fun parseJsonResponse(response: String): List<Result> = json.decodeFromString(response)

val PsiFile.projectRelativeFilePath: String?
get() {
val canonicalPath = virtualFile.canonicalPath ?: return null
return project.basePath?.takeIf { canonicalPath.startsWith(it) }?.let {
canonicalPath.substring(it.length + 1)
}
}

fun getStdinFileNameArgs(psiFile: PsiFile) =
psiFile.projectRelativeFilePath?.let { listOf("--stdin-filename", it, "-") } ?: listOf("-")
10 changes: 9 additions & 1 deletion src/com/koxudaxi/ruff/RuffConfigPanel.form
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
</properties>
<border type="none"/>
<children>
<grid id="da542" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="da542" layout-manager="GridLayoutManager" row-count="4" 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"/>
@@ -39,6 +39,14 @@
<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"/>
</constraints>
<properties>
<text value="Show Rule Code on inspection message"/>
</properties>
</component>
</children>
</grid>
<vspacer id="a33f9">
5 changes: 5 additions & 0 deletions src/com/koxudaxi/ruff/RuffConfigPanel.kt
Original file line number Diff line number Diff line change
@@ -10,16 +10,21 @@ class RuffConfigPanel(project: Project) {
lateinit var configPanel: JPanel
private lateinit var runRuffOnSaveCheckBox: JCheckBox
private lateinit var runRuffOnReformatCodeCheckBox: JCheckBox
private lateinit var showRuleCodeCheckBox: JCheckBox

init {
val ruffConfigService = getInstance(project)
runRuffOnSaveCheckBox.isSelected = ruffConfigService.runRuffOnSave
runRuffOnSaveCheckBox.isEnabled = true
runRuffOnReformatCodeCheckBox.isSelected = ruffConfigService.runRuffOnReformatCode
runRuffOnReformatCodeCheckBox.isEnabled = true
showRuleCodeCheckBox.isSelected = ruffConfigService.showRuleCode
showRuleCodeCheckBox.isEnabled = true
}
val runRuffOnSave: Boolean
get() = runRuffOnSaveCheckBox.isSelected
val runRuffOnReformatCode: Boolean
get() = runRuffOnReformatCodeCheckBox.isSelected
val showRuleCode: Boolean
get() = showRuleCodeCheckBox.isSelected
}
1 change: 1 addition & 0 deletions src/com/koxudaxi/ruff/RuffConfigService.kt
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import com.intellij.util.xmlb.XmlSerializerUtil
class RuffConfigService : PersistentStateComponent<RuffConfigService> {
var runRuffOnSave: Boolean = false
var runRuffOnReformatCode: Boolean = true
var showRuleCode: Boolean = false
override fun getState(): RuffConfigService {
return this
}
4 changes: 3 additions & 1 deletion src/com/koxudaxi/ruff/RuffConfigurable.kt
Original file line number Diff line number Diff line change
@@ -25,12 +25,14 @@ class RuffConfigurable internal constructor(project: Project) : Configurable {

override fun isModified(): Boolean {
return (ruffConfigService.runRuffOnSave != configPanel.runRuffOnSave) ||
(ruffConfigService.runRuffOnReformatCode != configPanel.runRuffOnReformatCode)
(ruffConfigService.runRuffOnReformatCode != configPanel.runRuffOnReformatCode) ||
(ruffConfigService.showRuleCode != configPanel.showRuleCode)
}

override fun apply() {
ruffConfigService.runRuffOnSave = configPanel.runRuffOnSave
ruffConfigService.runRuffOnReformatCode = configPanel.runRuffOnReformatCode
ruffConfigService.showRuleCode = configPanel.showRuleCode
}

override fun disposeUIResources() {
16 changes: 11 additions & 5 deletions src/com/koxudaxi/ruff/RuffInspection.kt
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@ import com.jetbrains.python.inspections.PyInspection
import com.jetbrains.python.inspections.PyInspectionVisitor
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.types.TypeEvalContext
import com.jetbrains.python.sdk.pythonSdk

class RuffInspection : PyInspection() {
override fun buildVisitor(
@@ -24,8 +23,9 @@ class RuffInspection : PyInspection() {

inner class Visitor(holder: ProblemsHolder, context: TypeEvalContext) :
PyInspectionVisitor(holder, context) {
private val args =
listOf("--exit-zero", "--no-cache", "--no-fix", "--format", "json", "-")
private val argsBase =
listOf("--exit-zero", "--no-cache", "--no-fix", "--format", "json")

override fun visitPyFile(node: PyFile) {
super.visitPyFile(node)

@@ -38,13 +38,19 @@ class RuffInspection : PyInspection() {
val stdin = pyFile.textToCharArray().toByteArrayAndClear()

val response = executeOnPooledThread(null) {
runRuff(module, stdin, *args.toTypedArray())
runRuff(
module,
stdin,
*(argsBase + getStdinFileNameArgs(pyFile)).toTypedArray()
)
} ?: return
val showRuleCode = RuffConfigService.getInstance(pyFile.project).showRuleCode

parseJsonResponse(response).forEach {
val psiElement = getPyElement(it, pyFile, document) ?: return@forEach
registerProblem(
psiElement,
it.message,
if(showRuleCode) "${it.code} ${it.message}" else it.message,
it.fix?.let { fix ->
RuffQuickFix.create(fix, document)
})
4 changes: 2 additions & 2 deletions src/com/koxudaxi/ruff/RuffPostFormatProcessor.kt
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ import com.jetbrains.python.sdk.pythonSdk


class RuffPostFormatProcessor : PostFormatProcessor {
private val args = listOf("--exit-zero", "--no-cache", "--fix", "-")
private val argsBase = listOf("--exit-zero", "--no-cache", "--fix")
override fun processElement(source: PsiElement, settings: CodeStyleSettings): PsiElement = source


@@ -27,7 +27,7 @@ class RuffPostFormatProcessor : PostFormatProcessor {
val stdin = source.textToCharArray().toByteArrayAndClear()

val formatted = executeOnPooledThread(null) {
runRuff(module, stdin, *args.toTypedArray())
runRuff(module, stdin, *(argsBase + getStdinFileNameArgs(source)).toTypedArray())
} ?: return TextRange.EMPTY_RANGE
val diffRange = diffRange(source.text, formatted) ?: return TextRange.EMPTY_RANGE