forked from intellij-rust/intellij-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will help to fix [cargo-issue] [cargo-issue]: rust-lang/cargo#2693
- Loading branch information
Showing
3 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/kotlin/org/rust/ide/inspections/CargoTestInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.rust.ide.inspections | ||
|
||
import com.intellij.codeInspection.LocalQuickFixBase | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
import org.rust.lang.core.psi.* | ||
import org.rust.lang.core.psi.util.childOfType | ||
|
||
val macroRe = """^\[\w+\].*""".toRegex() | ||
|
||
class CargoTestInspection : RustLocalInspectionTool() { | ||
override fun getDisplayName(): String = "Fix Cargo test" | ||
|
||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor { | ||
return object : RustVisitor() { | ||
override fun visitMethodCallExpr(o: RustMethodCallExpr) { | ||
if (o.identifier?.text != "with_stdout") return | ||
val firstArg = o.argList?.exprList?.firstOrNull() ?: return | ||
val stringLit = firstArg.childOfType<RustLitExpr>(strict = false) ?: return | ||
if (stringLit.text.lines().any { it.matches(macroRe) }) { | ||
holder.registerProblem(o, "Status is printed to stdout", WithStdout2WithStderr(o, stringLit)) | ||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
class WithStdout2WithStderr(val o: RustMethodCallExpr, val stringLit: PsiElement) : LocalQuickFixBase("Fix Cargo test") { | ||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val (stderr, stdout) = stringLit.text.trim('"').lines().partition { it.matches(macroRe) } | ||
val stderrText = "\\\n" + stderr.joinToString("\n") | ||
val stderrLiteral = stringLiteral(stderrText) | ||
var stdoutText = stdout.joinToString("\n") | ||
if (stdoutText.startsWith("\\\n\n")) { | ||
stdoutText = stdoutText.drop(2) | ||
} | ||
val stdoutLiteral = stringLiteral(stdoutText) | ||
stringLit.replace(stderrLiteral) | ||
o.identifier!!.replace(identifier("with_stderr")) | ||
|
||
val newExpr = methodCall(o, "with_stdout", stdoutLiteral) | ||
o.replace(newExpr) | ||
} | ||
|
||
private fun identifier(text: String): PsiElement { | ||
val fileText = "struct $text;" | ||
return elementFromText<RustStructItem>(fileText).identifier | ||
} | ||
|
||
private fun methodCall(o: RustMethodCallExpr, methodName: String, arg: PsiElement): PsiElement { | ||
val fileText = "fn main() { ${o.text}.$methodName(${arg.text})}}" | ||
return elementFromText<RustMethodCallExpr>(fileText) | ||
} | ||
|
||
private fun stringLiteral(text: String): PsiElement { | ||
return elementFromText<RustLiteral.Text>("fn main() { \"$text\" }") | ||
} | ||
|
||
private inline fun<reified T: PsiElement> elementFromText(text: String): T { | ||
val file = RustElementFactory.createFileFromText(o.project, text) | ||
return file!!.childOfType<T>()!! | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters