Skip to content

Commit

Permalink
Merge pull request #59 from koxudaxi/support_file_path
Browse files Browse the repository at this point in the history
Support file path when run ruff command
  • Loading branch information
koxudaxi authored Jan 22, 2023
2 parents 25ada23 + 783f509 commit bc6f304
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 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
- Support file path when run ruff command [[#59](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/59)]

## 0.0.5 - 2023-01-17
- Fix system ruff detection [[#53](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/53)]
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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("-")
12 changes: 8 additions & 4 deletions src/com/koxudaxi/ruff/RuffInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -38,7 +38,11 @@ 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
parseJsonResponse(response).forEach {
val psiElement = getPyElement(it, pyFile, document) ?: return@forEach
Expand Down
4 changes: 2 additions & 2 deletions src/com/koxudaxi/ruff/RuffPostFormatProcessor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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

Expand Down

0 comments on commit bc6f304

Please sign in to comment.