Skip to content

Commit

Permalink
Pulling invocationText into the plugin due to some upstream changes. (
Browse files Browse the repository at this point in the history
  • Loading branch information
hmemcpy committed Oct 19, 2024
1 parent 11a1831 commit ae431a6
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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}"))

}

Expand Down
38 changes: 37 additions & 1 deletion src/main/scala/zio/intellij/inspections/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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)

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.intellij.inspections.simplifications

import org.jetbrains.plugins.scala.codeInspection.collections.{
invocationText,
Qualified,
Simplification,
SimplificationType
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit ae431a6

Please sign in to comment.