Skip to content

Commit

Permalink
Add "Fix all unsafe" action and intention
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Jan 23, 2025
1 parent d445192 commit b5fd956
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 14 deletions.
10 changes: 7 additions & 3 deletions src/main/kotlin/insyncwithfoo/ryecharm/ruff/actions/FixAll.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private val AnActionEvent.editor: Editor?
get() = dataContext.getData(CommonDataKeys.EDITOR)


internal class FixAll : AnAction(), DumbAware {
internal abstract class FixAll(private val unsafe: Boolean) : AnAction(), DumbAware {

override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return noProjectFound()
Expand All @@ -38,7 +38,7 @@ internal class FixAll : AnAction(), DumbAware {
if (ruff == null) {
project.couldNotConstructCommandFactory<Ruff>(
"""
|Was trying to fix all safely fixable violations.
|Was trying to fix all violations.
""".trimMargin()
)
return
Expand All @@ -49,7 +49,7 @@ internal class FixAll : AnAction(), DumbAware {
?: return project.noDocumentFound()
val path = file.virtualFile.toNioPath()

val command = ruff.fixAll(document.text, path)
val command = ruff.fixAll(document.text, path, unsafeFixes = unsafe)

project.runCommandAndLoadResult(command, file)
}
Expand All @@ -70,3 +70,7 @@ internal class FixAll : AnAction(), DumbAware {
}

}


internal class FixAllSafe : FixAll(unsafe = false)
internal class FixAllUnsafe : FixAll(unsafe = true)
6 changes: 5 additions & 1 deletion src/main/kotlin/insyncwithfoo/ryecharm/ruff/commands/Ruff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ internal class Ruff private constructor(
return OptimizeImportsCommand().build(arguments, text)
}

fun fixAll(text: String, stdinFilename: Path?): Command {
fun fixAll(text: String, stdinFilename: Path?, unsafeFixes: Boolean): Command {
val arguments = CommandArguments("--fix", "--fix-only", "--exit-zero", "--quiet", "-")

if (unsafeFixes) {
arguments += "--unsafe-fixes"
}

if (stdinFilename != null) {
arguments["--stdin-filename"] = stdinFilename.toString()
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/insyncwithfoo/ryecharm/ruff/intentions/FixAll.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ import insyncwithfoo.ryecharm.runInForeground
import insyncwithfoo.ryecharm.runWriteCommandAction


internal class FixAll : ExternalIntentionAction, LowPriorityAction {
internal abstract class FixAll(private val unsafe: Boolean) : ExternalIntentionAction, LowPriorityAction {

override fun startInWriteAction() = false

override fun getFamilyName() = message("intentions.ruff.fixAll.familyName")

override fun getText() = familyName

override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean {
Expand Down Expand Up @@ -53,7 +51,7 @@ internal class FixAll : ExternalIntentionAction, LowPriorityAction {
return
}

val command = ruff.fixAll(document.text, path)
val command = ruff.fixAll(document.text, path, unsafeFixes = unsafe)

project.runCommandAndLoadResult(command, file)
}
Expand All @@ -74,3 +72,13 @@ internal class FixAll : ExternalIntentionAction, LowPriorityAction {
}

}


internal class FixAllSafe : FixAll(unsafe = false) {
override fun getFamilyName() = message("intentions.ruff.fixAll.familyName.safe")
}


internal class FixAllUnsafe : FixAll(unsafe = true) {
override fun getFamilyName() = message("intentions.ruff.fixAll.familyName.unsafe")
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal class RuffOnSaveTasksRunner : ActionOnSave() {
var newText = originalText

if (configurations.fixOnSave) {
val command = ruff.fixAll(newText, path)
val command = ruff.fixAll(newText, path, unsafeFixes = false)
newText = getResultOrNull(command) ?: return null
}

Expand Down
21 changes: 18 additions & 3 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@

<intentionAction>
<language/>
<className>insyncwithfoo.ryecharm.ruff.intentions.FixAll</className>
<className>insyncwithfoo.ryecharm.ruff.intentions.FixAllSafe</className>
<category>RyeCharm</category>
</intentionAction>

<intentionAction>
<language/>
<className>insyncwithfoo.ryecharm.ruff.intentions.FixAllUnsafe</className>
<category>RyeCharm</category>
</intentionAction>

Expand Down Expand Up @@ -450,12 +456,21 @@

<!--suppress PluginXmlCapitalization -->
<action
id="insyncwithfoo.ryecharm.ruff.actions.FixAll"
class="insyncwithfoo.ryecharm.ruff.actions.FixAll"
id="insyncwithfoo.ryecharm.ruff.actions.FixAllSafe"
class="insyncwithfoo.ryecharm.ruff.actions.FixAllSafe"
text="Ruff: Fix all safely fixable violations"
description="Fix all safely fixable violations found in the current editor"
icon="AllIcons.Actions.Edit"
/>

<!--suppress PluginXmlCapitalization -->
<action
id="insyncwithfoo.ryecharm.ruff.actions.FixAllUnsafe"
class="insyncwithfoo.ryecharm.ruff.actions.FixAllUnsafe"
text="Ruff: Fix all fixable violations, unsafe included"
description="Fix all violations found in the current editor, unsafe included"
icon="AllIcons.Actions.Edit"
/>
</actions>

<actions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sorted([]) # C413

def foo_bar() -> None:
if baz_qux:
lorem_ipsum()
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os # F401

list(sorted([])) # C413

def foo_bar():
if baz_qux:
lorem_ipsum()

return None # RET501
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html lang="en">
<body>
Runs <code>ruff check --fix --unsafe-fixes</code>.
</body>
</html>
3 changes: 2 additions & 1 deletion src/main/resources/messages/ryecharm.properties
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ intentions.main.editScriptMetadataFragment.familyName = Edit script metadata fra
intentions.main.editScriptMetadataFragment.filename = Script metadata ({0}).toml
intentions.main.editScriptMetadataFragment.progressTitle = Editing block...

intentions.ruff.fixAll.familyName = Fix all safely fixable violations
intentions.ruff.fixAll.familyName.safe = Fix all safely fixable violations
intentions.ruff.fixAll.familyName.unsafe = Fix all fixable violations, unsafe included
intentions.ruff.organizeImports.familyName = Organize imports

intentions.ruff.fixViolation.familyName = {0}: {1} ({2})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ internal class RuffTest : CommandFactoryTest() {
fun `test fixAll`() {
val text = randomText()
val path = randomPath().orRandomlyNull()
val unsafeFixes = boolean

val command = ruff.fixAll(text, path)
val command = ruff.fixAll(text, path, unsafeFixes = unsafeFixes)
val arguments = command.arguments

assertEquals("check", command.subcommand)
Expand All @@ -196,6 +197,10 @@ internal class RuffTest : CommandFactoryTest() {
if (path != null) {
assertTrue(arguments include listOf("--stdin-filename", path.toString()))
}

if (unsafeFixes) {
assertContains(arguments, "--unsafe-fixes")
}
}

@Test
Expand Down

0 comments on commit b5fd956

Please sign in to comment.