From 072922236cf15f8eda9c457d535404d16839ce8a Mon Sep 17 00:00:00 2001 From: Igal Tabachnik Date: Tue, 15 Oct 2024 14:50:30 +0300 Subject: [PATCH] Pulling `invocationText` into the plugin due to some upstream changes. --- .../DiscardingZIOValueInspection.scala | 6 +-- .../zio/intellij/inspections/package.scala | 38 ++++++++++++++++++- .../SimplifyBuildUseInspection.scala | 6 +-- .../SimplifyExitCodeInspection.scala | 4 +- .../SimplifyFlattenInspection.scala | 6 +-- .../SimplifyIgnoreInspection.scala | 2 +- .../SimplifyInfallibleEffectInspection.scala | 5 +-- .../SimplifyMapBothInspection.scala | 2 +- .../SimplifyMapInspection.scala | 4 +- .../SimplifyOrElseInspection.scala | 6 +-- .../SimplifySleepInspection.scala | 2 +- .../SimplifyTapInspection.scala | 4 +- .../SimplifyToLayerInspection.scala | 2 +- .../SimplifyUnitInspection.scala | 2 +- .../SimplifyZipInspection.scala | 4 +- 15 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/main/scala/zio/intellij/inspections/mistakes/DiscardingZIOValueInspection.scala b/src/main/scala/zio/intellij/inspections/mistakes/DiscardingZIOValueInspection.scala index d4dd8541..e103f6a0 100644 --- a/src/main/scala/zio/intellij/inspections/mistakes/DiscardingZIOValueInspection.scala +++ b/src/main/scala/zio/intellij/inspections/mistakes/DiscardingZIOValueInspection.scala @@ -3,7 +3,7 @@ package zio.intellij.inspections.mistakes import com.intellij.codeInspection.{LocalInspectionTool, ProblemHighlightType, ProblemsHolder} import org.jetbrains.annotations.Nls import org.jetbrains.plugins.scala.codeInspection.PsiElementVisitorSimple -import org.jetbrains.plugins.scala.codeInspection.collections.{invocationText, Simplification, SimplificationType} +import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType} import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScFunctionExpr, ScReferenceExpression} import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction import org.jetbrains.plugins.scala.lang.psi.types.ScType @@ -12,7 +12,7 @@ import org.jetbrains.plugins.scala.lang.psi.types.result.Typeable import zio.intellij.inspections.mistakes.DiscardingZIOValueInspectionBase.ReturnType import zio.intellij.inspections.streamMethods.`.runDrain` import zio.intellij.inspections.zioMethods.`.map` -import zio.intellij.inspections.{lambda, ZInspection} +import zio.intellij.inspections.{ZInspection, invocationTextFor, lambda} import zio.intellij.utils.StringUtils.ScExpressionExt import zio.intellij.utils.TypeCheckUtils.{`ZStream[R, E, O]`, fromZioLike} import zio.intellij.utils.fromSameClass @@ -61,7 +61,7 @@ object DiscardingZIOValueMapToFlatMapSmartInspection extends SimplificationType } protected def replacement(expr: ScExpression, qual: ScExpression, body: ScExpression): Simplification = - replace(expr).withText(invocationText(qual, s"$replacement${body.getWrappedText}")) + replace(expr).withText(invocationTextFor(qual, s"$replacement${body.getWrappedText}")) } diff --git a/src/main/scala/zio/intellij/inspections/package.scala b/src/main/scala/zio/intellij/inspections/package.scala index cd8ef422..fbfc0261 100644 --- a/src/main/scala/zio/intellij/inspections/package.scala +++ b/src/main/scala/zio/intellij/inspections/package.scala @@ -2,7 +2,7 @@ package zio.intellij import com.intellij.psi.{PsiAnnotation, PsiClass, PsiElement} import org.jetbrains.plugins.scala.codeInspection.collections._ -import org.jetbrains.plugins.scala.extensions.PsiClassExt +import org.jetbrains.plugins.scala.extensions.{PsiClassExt, childOf} import org.jetbrains.plugins.scala.lang.psi.api.base.patterns.{ScPattern, ScReferencePattern, ScWildcardPattern} import org.jetbrains.plugins.scala.lang.psi.api.expr._ import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunctionDefinition @@ -16,6 +16,42 @@ import zio.intellij.utils.types._ package object inspections { + // copied over from the Scala plugin because of some upstream changes + // TODO consider removing in the future, replacing with whatever is the new thing + // also, renaming from just `invocationText` to ensure using the correct (our) method + def invocationTextFor(qual: PsiElement, methName: String, args: ScExpression*): String = { + def argsText = argListText(args) + + if (qual == null) { + s"$methName$argsText" + } else { + val qualText = qual.getText + qual match { + case _ childOf ScInfixExpr(`qual`, _, _) if args.size == 1 => + s"$qualText $methName ${args.head.getText}" + case _ childOf ScPostfixExpr(`qual`, _) if args.size == 1 => + s"$qualText $methName ${args.head.getText}" + case _: ScInfixExpr => s"($qualText).$methName$argsText" + case _: ScFor => s"($qualText).$methName$argsText" + case _ => s"$qualText.$methName$argsText" + } + + } + } + + def argListText(args: Seq[ScExpression]): String = { + args match { + case Seq(p: ScParenthesisedExpr) => p.getText + case Seq(b @ ScBlock(_: ScFunctionExpr)) => b.getText + case Seq(ScBlock(stmt: ScBlockStatement)) => s"(${stmt.getText})" + case Seq(b: ScBlock) => b.getText + case Seq((_: ScFunctionExpr) childOf (b: ScBlockExpr)) => b.getText + case Seq(other) => s"(${other.getText})" + case seq if seq.size > 1 => seq.map(_.getText).mkString("(", ", ", ")") + case _ => "" + } + } + object collectionMethods { private[inspections] val `.map` = invocation("map").from(likeCollectionClasses) } diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyBuildUseInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyBuildUseInspection.scala index d9867840..fad2c781 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyBuildUseInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyBuildUseInspection.scala @@ -1,11 +1,11 @@ package zio.intellij.inspections.simplifications -import org.jetbrains.plugins.scala.codeInspection.collections.{invocationText, Simplification, SimplificationType} +import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType} import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScReferenceExpression, ScUnderscoreSection} import zio.intellij.inspections.layerMethods.`.build` import zio.intellij.inspections.managedMethods.`.use` import zio.intellij.inspections.zioMethods.`.provide` -import zio.intellij.inspections.{lambda, ZInspection} +import zio.intellij.inspections.{ZInspection, invocationTextFor, lambda} class SimplifyBuildUseInspection extends ZInspection(BuildUseSimplificationType) @@ -13,7 +13,7 @@ object BuildUseSimplificationType extends SimplificationType { override def hint: String = "Replace with .provideLayer" def replacement(expr: ScExpression, zio: ScExpression, layer: ScExpression): Simplification = - replace(expr).withText(invocationText(zio, "provideLayer", layer)).highlightAll + replace(expr).withText(invocationTextFor(zio, "provideLayer", layer)).highlightAll override def getSimplification(expr: ScExpression): Option[Simplification] = expr match { diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyExitCodeInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyExitCodeInspection.scala index e26451ff..4fcc1627 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyExitCodeInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyExitCodeInspection.scala @@ -1,6 +1,6 @@ package zio.intellij.inspections.simplifications -import org.jetbrains.plugins.scala.codeInspection.collections.{invocationText, Simplification, SimplificationType} +import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType} import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression import zio.intellij.inspections._ import zio.intellij.inspections.zioMethods._ @@ -12,7 +12,7 @@ object ExitCodeSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression) = - replace(expr).withText(invocationText(qual, s"exitCode")) + replace(expr).withText(invocationTextFor(qual, s"exitCode")) expr match { case qual `.map` `_ => x`(exitCodeSuccess() | exitCodeFailure()) => Some(replacement(qual)) diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyFlattenInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyFlattenInspection.scala index a4497f66..b2862e8b 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyFlattenInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyFlattenInspection.scala @@ -1,10 +1,10 @@ package zio.intellij.inspections.simplifications -import org.jetbrains.plugins.scala.codeInspection.collections.{invocationText, Simplification, SimplificationType} +import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType} import org.jetbrains.plugins.scala.extensions.BooleanExt import org.jetbrains.plugins.scala.lang.psi.api.expr.ScExpression import org.jetbrains.plugins.scala.lang.psi.impl.ScalaPsiElementFactory -import zio.intellij.inspections.ZInspection +import zio.intellij.inspections.{ZInspection, invocationTextFor} import zio.intellij.inspections.zioMethods._ class SimplifyFlattenInspection extends ZInspection(MapFlattenInspection) @@ -15,7 +15,7 @@ object MapFlattenInspection extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = expr match { case `.flatten`(qual `.map` f) => - val newText = invocationText(qual, "flatMap", f) + val newText = invocationTextFor(qual, "flatMap", f) sameType(expr, newText).option { replace(expr).withText(newText).highlightFrom(qual) } diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyIgnoreInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyIgnoreInspection.scala index 7cedd7fa..291a24f4 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyIgnoreInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyIgnoreInspection.scala @@ -11,7 +11,7 @@ object IgnoreSimplificationType extends SimplificationType { override def hint: String = "Replace with .ignore" override def getSimplification(expr: ScExpression): Option[Simplification] = { - def replacement(qual: ScExpression) = replace(expr).withText(invocationText(qual, "ignore")) + def replacement(qual: ScExpression) = replace(expr).withText(invocationTextFor(qual, "ignore")) expr match { case qual `.catchAll` `_ => ZIO.unit`() => Some(replacement(qual)) case qual `.foldCause` (`_ => ()`(), `_ => ()`()) => Some(replacement(qual)) diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyInfallibleEffectInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyInfallibleEffectInspection.scala index 38bfa9e0..2eacb203 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyInfallibleEffectInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyInfallibleEffectInspection.scala @@ -1,7 +1,6 @@ package zio.intellij.inspections.simplifications import org.jetbrains.plugins.scala.codeInspection.collections.{ - invocationText, Qualified, Simplification, SimplificationType @@ -71,7 +70,7 @@ sealed abstract class BaseErrorModificationSimplificationType(qual: Qualified, m val hint = s"Replace with .$methodName" private def replacement(expr: ScExpression, zio: ScExpression, g: ScExpression): Simplification = - replace(expr).withText(invocationText(zio, methodName, g)).highlightAll + replace(expr).withText(invocationTextFor(zio, methodName, g)).highlightAll override def getSimplification(expr: ScExpression): Option[Simplification] = expr match { @@ -99,7 +98,7 @@ sealed abstract class BaseErrorRecoverySimplificationType(qual: Qualified, metho val hint = s"Replace with .map($methodStr)" private def replacement(expr: ScExpression, zio: ScExpression): Option[Simplification] = - createExpression(methodStr, expr).map(m => replace(expr).withText(invocationText(zio, "map", m)).highlightAll) + createExpression(methodStr, expr).map(m => replace(expr).withText(invocationTextFor(zio, "map", m)).highlightAll) override def getSimplification(expr: ScExpression): Option[Simplification] = expr match { diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapBothInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapBothInspection.scala index 179e8a2d..52dad990 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapBothInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapBothInspection.scala @@ -19,7 +19,7 @@ object MapBothSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, a: ScExpression, b: ScExpression) = replace(expr) - .withText(invocationText(qual, s"mapBoth(${b.getBracedText}, ${a.getBracedText})")) + .withText(invocationTextFor(qual, s"mapBoth(${b.getBracedText}, ${a.getBracedText})")) .highlightFrom(qual) def toFunctionExpr(e: ScExpression): ScExpression = diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapInspection.scala index f16c7855..081bc3bd 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyMapInspection.scala @@ -13,7 +13,7 @@ object AsSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, arg: ScExpression) = - replace(expr).withText(invocationText(qual, s"as${arg.getWrappedText}")) + replace(expr).withText(invocationTextFor(qual, s"as${arg.getWrappedText}")) expr match { case qual `.map` `_ => x`(x) => Some(replacement(qual, x)) case _ => None @@ -26,7 +26,7 @@ object MapErrorSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, arg: ScExpression) = - replace(expr).withText(invocationText(qual, s"orElseFail${arg.getWrappedText}")) + replace(expr).withText(invocationTextFor(qual, s"orElseFail${arg.getWrappedText}")) expr match { case qual `.mapError` `_ => x`(x) => Some(replacement(qual, x)) case _ => None diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyOrElseInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyOrElseInspection.scala index 428dad4f..52301032 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyOrElseInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyOrElseInspection.scala @@ -1,9 +1,9 @@ package zio.intellij.inspections.simplifications -import org.jetbrains.plugins.scala.codeInspection.collections.{invocationText, Simplification, SimplificationType} +import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType} import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScBlock, ScBlockStatement, ScExpression} import zio.intellij.inspections.zioMethods.`.orElse` -import zio.intellij.inspections.{`ZIO.fail`, ZInspection} +import zio.intellij.inspections.{ZInspection, `ZIO.fail`, invocationTextFor} class SimplifyOrElseInspection extends ZInspection(OrElseFailSimplificationType) @@ -22,7 +22,7 @@ object OrElseFailSimplificationType extends SimplificationType { } def replacement(zio: ScExpression, error: ScExpression): Simplification = - replace(expr).withText(invocationText(zio, replaceWith, error)) + replace(expr).withText(invocationTextFor(zio, replaceWith, error)) expr match { case zio `.orElse` `ZIO.fail`(_, error) => Some(replacement(zio, error)) diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifySleepInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifySleepInspection.scala index fd6bbffb..d1016ab9 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifySleepInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifySleepInspection.scala @@ -13,7 +13,7 @@ object SleepSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, duration: ScExpression) = - replace(expr).withText(invocationText(qual, s"delay${duration.getWrappedText}")).highlightAll + replace(expr).withText(invocationTextFor(qual, s"delay${duration.getWrappedText}")).highlightAll expr match { case `ZIO.sleep`(_, duration) `.*>` io => Some(replacement(io, duration)) case `ZIO.sleep`(_, duration) `.flatMap` lambda(_, io) => Some(replacement(io, duration)) diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyTapInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyTapInspection.scala index 836c4935..750d0778 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyTapInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyTapInspection.scala @@ -71,7 +71,7 @@ sealed abstract class BaseRefactoringType(invocation: Qualified, replaceWith: St override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, param: ScParameter, body: ScExpression) = replace(expr) - .withText(invocationText(qual, s"$replaceWith(${simplifyFunctionCall(param, body)})")) + .withText(invocationTextFor(qual, s"$replaceWith(${simplifyFunctionCall(param, body)})")) .highlightFrom(qual) expr match { @@ -100,7 +100,7 @@ object TapBothSimplificationType extends SimplificationType { override def getSimplification(expr: ScExpression): Option[Simplification] = { def replacement(qual: ScExpression, tapError: ScExpression, tap: ScExpression) = replace(expr) - .withText(invocationText(qual, s"tapBoth(${tapError.getBracedText}, ${tap.getBracedText})")) + .withText(invocationTextFor(qual, s"tapBoth(${tapError.getBracedText}, ${tap.getBracedText})")) .highlightFrom(qual) expr match { diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyToLayerInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyToLayerInspection.scala index 9a778521..01a8a9fd 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyToLayerInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyToLayerInspection.scala @@ -18,7 +18,7 @@ sealed abstract class BaseToLayerSimplificationType(methodName: String, methodEx case methodExtractor(_, effect) if fromZio(effect) => val replacementText = effect match { case _: ScFor => s"${effect.getText.parenthesize(true)}.$methodName" - case _ => invocationText(effect, methodName) + case _ => invocationTextFor(effect, methodName) } Some(replace(expr).withText(replacementText).highlightFrom(expr)) case _ => None diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyUnitInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyUnitInspection.scala index 03e848ed..fbe86de1 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyUnitInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyUnitInspection.scala @@ -11,7 +11,7 @@ object UnitSimplificationType extends SimplificationType { override def hint: String = "Replace with .unit" override def getSimplification(expr: ScExpression): Option[Simplification] = { - def replacement(qual: ScExpression) = replace(expr).withText(invocationText(qual, "unit")) + def replacement(qual: ScExpression) = replace(expr).withText(invocationTextFor(qual, "unit")) expr match { case qual `.*>` `ZIO.unit`(_, _) => Some(replacement(qual)) case qual `.as` unit() => Some(replacement(qual)) diff --git a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyZipInspection.scala b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyZipInspection.scala index 4164af2d..80c77dbe 100644 --- a/src/main/scala/zio/intellij/inspections/simplifications/SimplifyZipInspection.scala +++ b/src/main/scala/zio/intellij/inspections/simplifications/SimplifyZipInspection.scala @@ -18,7 +18,7 @@ sealed class BaseZipOneSimplificationType(invocation: Qualified, replaceWith: St override def getSimplification(expr: ScExpression): Option[Simplification] = expr match { case qual invocation `_ => x`(x) => - Some(replace(expr).withText(invocationText(qual, s"$replaceWith${x.getWrappedText}"))) + Some(replace(expr).withText(invocationTextFor(qual, s"$replaceWith${x.getWrappedText}"))) case _ => None } @@ -57,7 +57,7 @@ sealed abstract class BaseZipToSucceedSimplificationType extends SimplificationT * @return a SimplificationBuilder */ protected def simplify(toSimplify: ScExpression, toKeep: ScExpression, zioArg: ScExpression): SimplificationBuilder = - replace(toSimplify).withText(invocationText(toKeep, s"as${zioArg.getWrappedText}")).highlightAll + replace(toSimplify).withText(invocationTextFor(toKeep, s"as${zioArg.getWrappedText}")).highlightAll } object ZipRightSimplificationType extends BaseZipOneSimplificationType(`.flatMap`, "zipRight")