Skip to content

Commit

Permalink
Merge pull request #164 from koxudaxi/fix_content_vanishes_when_set_f…
Browse files Browse the repository at this point in the history
…orce_exclude

Fix content vanishes when set --force-exclude
  • Loading branch information
koxudaxi authored May 10, 2023
2 parents afda2bc + 0e19abf commit d90e859
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]
- Move inspection to external_annotator [[#158](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/158)]
- Add --force-exclude to default arguments [[#162](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/162)]
- Fix content vanishes when set --force-exclude [[#164](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/164)]

## [0.0.13] - 2023-04-25
- Add disableOnSaveOutsideOfProject option [[#155](https://github.com/koxudaxi/ruff-pycharm-plugin/pull/155)]
Expand Down
18 changes: 18 additions & 0 deletions src/com/koxudaxi/ruff/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ val USER_SITE_RUFF_PATH = PythonSdkUtil.getUserSite() + File.separator + "bin" +

val json = Json { ignoreUnknownKeys = true }

val ARGS_BASE = listOf("--exit-zero", "--no-cache", "--force-exclude")
val FIX_ARGS = ARGS_BASE + listOf("--fix")
val NO_FIX_ARGS = ARGS_BASE + listOf("--no-fix", "--format", "json")

fun detectRuffExecutable(project: Project, ruffConfigService: RuffConfigService): File? {
project.pythonSdk?.let {
Expand Down Expand Up @@ -257,4 +260,19 @@ fun Document.getStartEndRange(startLocation: Location, endLocation: Location, of
else -> getLineStartOffset(lastLine) + endLocation.column + offset
}
return TextRange(start, end)
}

fun checkFixResult(pyFile: PsiFile, fixResult: String?): String? {
if (fixResult == null) return null
if (fixResult.isNotBlank()) return fixResult
val noFixResult = runRuff(pyFile, NO_FIX_ARGS) ?: return null

// check the file is excluded
if (noFixResult == "[]\n") return null
return fixResult
}

fun format(pyFile: PsiFile): String? {
val fixResult = runRuff(pyFile, FIX_ARGS) ?: return null
return checkFixResult(pyFile, fixResult)
}
4 changes: 1 addition & 3 deletions src/com/koxudaxi/ruff/RuffExternalAnnotator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import com.jetbrains.python.psi.impl.PyFileImpl

class RuffExternalAnnotator :
ExternalAnnotator<RuffExternalAnnotator.RuffExternalAnnotatorInfo, RuffExternalAnnotator.RuffExternalAnnotatorResult>() {
private val argsBase =
listOf("--exit-zero", "--no-cache", "--no-fix", "--format", "json", "--force-exclude")
private val highlightSeverityLevels = mapOf<HighlightDisplayLevel, HighlightSeverity>(
HighlightDisplayLevel.ERROR to HighlightSeverity.ERROR,
HighlightDisplayLevel.WARNING to HighlightSeverity.WARNING,
Expand All @@ -47,7 +45,7 @@ class RuffExternalAnnotator :
val highlightDisplayLevel = highlightSeverityLevels[level] ?: return null
val problemHighlightType = problemHighlightTypeLevels[level] ?: return null
val showRuleCode = RuffConfigService.getInstance(file.project).showRuleCode
val commandArgs = generateCommandArgs(file, argsBase)
val commandArgs = generateCommandArgs(file, NO_FIX_ARGS)
return RuffExternalAnnotatorInfo(showRuleCode, highlightDisplayLevel, problemHighlightType, commandArgs)
}

Expand Down
1 change: 0 additions & 1 deletion src/com/koxudaxi/ruff/RuffFileDocumentManagerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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(val project: Project) : FileDocumentManagerListener {
private val ruffConfigService = RuffConfigService.getInstance(project)
Expand Down
7 changes: 3 additions & 4 deletions src/com/koxudaxi/ruff/RuffFixService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ import com.intellij.psi.PsiFile
class RuffFixService(val project: Project) {
private val undoManager by lazy { UndoManager.getInstance(project) }
private val psiDocumentManager by lazy { PsiDocumentManager.getInstance(project) }
private val argsBase = listOf("--exit-zero", "--no-cache", "--fix", "--force-exclude")

fun fix(psiFile: PsiFile) {
val document = psiDocumentManager.getDocument(psiFile) ?: return
fix(document, psiFile)
}

fun fix(document: Document, psiFile: PsiFile) =
runRuffInBackground(psiFile, argsBase) {
if (it !is String) return@runRuffInBackground
runRuffInBackground(psiFile, FIX_ARGS) {
val formatted = checkFixResult(psiFile, it) ?: return@runRuffInBackground
runInEdt {
runWriteAction {
CommandProcessor.getInstance().executeCommand(
project,
{
if (!undoManager.isUndoInProgress) {
document.setText(it)
document.setText(formatted)
}
},
"Run ruff",
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 @@ -10,7 +10,6 @@ import com.jetbrains.python.psi.PyUtil


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


Expand All @@ -20,7 +19,7 @@ class RuffPostFormatProcessor : PostFormatProcessor {
if (!pyFile.isApplicableTo) return TextRange.EMPTY_RANGE

val formatted = executeOnPooledThread(null) {
runRuff(pyFile, argsBase)
format(pyFile)
} ?: return TextRange.EMPTY_RANGE
val sourceDiffRange = diffRange(source.text, formatted) ?: return TextRange.EMPTY_RANGE

Expand All @@ -39,6 +38,7 @@ class RuffPostFormatProcessor : PostFormatProcessor {
} ?: TextRange.EMPTY_RANGE
}


private fun diffRange(s1: String, s2: String): TextRange? {
if (s1 == s2) return null
if (s2.isEmpty()) return TextRange(0, s1.length)
Expand Down

0 comments on commit d90e859

Please sign in to comment.