Skip to content

Commit

Permalink
Fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hmemcpy committed Nov 15, 2024
1 parent 991c1fc commit 13b4fd2
Show file tree
Hide file tree
Showing 39 changed files with 657 additions and 472 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.jetbrains.sbtidea.{AutoJbr, JbrPlatform}

lazy val scala213 = "2.13.14"
lazy val scala213 = "2.13.15"
lazy val scalaPluginVersion = "2024.3.18"
lazy val minorVersion = "0"
lazy val buildVersion = sys.env.getOrElse("ZIO_INTELLIJ_BUILD_NUMBER", minorVersion)
Expand Down
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.{Simplification, SimplificationType}
import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType, invocationText}
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.{ZInspection, invocationTextFor, lambda}
import zio.intellij.inspections.{ZInspection, 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(invocationTextFor(qual, s"$replacement${body.getWrappedText}"))
replace(expr).withText(invocationText(qual, s"$replacement${body.getWrappedText}")).highlightFrom(qual)

}

Expand Down
41 changes: 3 additions & 38 deletions src/main/scala/zio/intellij/inspections/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,6 @@ 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 Expand Up @@ -212,8 +176,9 @@ package object inspections {

def unapply(expr: ScExpression): Option[(ScReferenceExpression, ScExpression)] =
expr match {
case MethodRepr(_, _, Some(ref), Seq(e)) => Some((ref, e))
case _ => None
case MethodRepr(_, _, Some(ref), Seq(e)) => Some((ref, e))
case MethodRepr(_, Some(ref: ScReferenceExpression), _, Seq(e)) => Some((ref, e))
case _ => None
}
}

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.{Simplification, SimplificationType}
import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType, invocationText}
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.{ZInspection, invocationTextFor, lambda}
import zio.intellij.inspections.{ZInspection, 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(invocationTextFor(zio, "provideLayer", layer)).highlightAll
replace(expr).withText(invocationText(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.{Simplification, SimplificationType}
import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType, invocationText}
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(invocationTextFor(qual, s"exitCode"))
replace(expr).withText(invocationText(qual, s"exitCode")).highlightFrom(qual)

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.{Simplification, SimplificationType}
import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType, invocationText}
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, invocationTextFor}
import zio.intellij.inspections.ZInspection
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 = invocationTextFor(qual, "flatMap", f)
val newText = invocationText(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(invocationTextFor(qual, "ignore"))
def replacement(qual: ScExpression) = replace(expr).withText(invocationText(qual, "ignore")).highlightFrom(qual)
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,10 +1,6 @@
package zio.intellij.inspections.simplifications

import org.jetbrains.plugins.scala.codeInspection.collections.{
Qualified,
Simplification,
SimplificationType
}
import org.jetbrains.plugins.scala.codeInspection.collections.{Qualified, Simplification, SimplificationType, invocationText}
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScExpression, ScReferenceExpression}
import org.jetbrains.plugins.scala.lang.psi.api.statements.ScFunction
import org.jetbrains.plugins.scala.lang.psi.types.result.Typeable
Expand Down Expand Up @@ -70,7 +66,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(invocationTextFor(zio, methodName, g)).highlightAll
replace(expr).withText(invocationText(zio, methodName, g)).highlightAll

override def getSimplification(expr: ScExpression): Option[Simplification] =
expr match {
Expand Down Expand Up @@ -98,7 +94,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(invocationTextFor(zio, "map", m)).highlightAll)
createExpression(methodStr, expr).map(m => replace(expr).withText(invocationText(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(invocationTextFor(qual, s"mapBoth(${b.getBracedText}, ${a.getBracedText})"))
.withText(invocationText(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(invocationTextFor(qual, s"as${arg.getWrappedText}"))
replace(expr).withText(invocationText(qual, s"as${arg.getWrappedText}")).highlightFrom(qual)
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(invocationTextFor(qual, s"orElseFail${arg.getWrappedText}"))
replace(expr).withText(invocationText(qual, s"orElseFail${arg.getWrappedText}")).highlightFrom(qual)
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.{Simplification, SimplificationType}
import org.jetbrains.plugins.scala.codeInspection.collections.{Simplification, SimplificationType, invocationText}
import org.jetbrains.plugins.scala.lang.psi.api.expr.{ScBlock, ScBlockStatement, ScExpression}
import zio.intellij.inspections.zioMethods.`.orElse`
import zio.intellij.inspections.{ZInspection, `ZIO.fail`, invocationTextFor}
import zio.intellij.inspections.{ZInspection, `ZIO.fail`}

class SimplifyOrElseInspection extends ZInspection(OrElseFailSimplificationType)

Expand All @@ -13,22 +13,22 @@ object OrElseFailSimplificationType extends SimplificationType {
override def hint: String = s"Replace with .$replaceWith"

override def getSimplification(expr: ScExpression): Option[Simplification] = {
def blockReplacement(zio: ScExpression, body: Seq[ScBlockStatement]): Simplification = {
def blockReplacement(qual: ScExpression, body: Seq[ScBlockStatement]): Simplification = {
// new Intellij version doesn't seem to like Windows line separators
// if ScalaPsiElementFactory.createBlockWithGivenExpressions can use "\n", so can we
val separator = "\n"
val blockBody = body.map(_.getText).mkString(separator, separator, separator)
replace(expr).withText(s"${zio.getText}.$replaceWith {$blockBody}")
replace(expr).withText(s"${qual.getText}.$replaceWith {$blockBody}").highlightFrom(qual)
}

def replacement(zio: ScExpression, error: ScExpression): Simplification =
replace(expr).withText(invocationTextFor(zio, replaceWith, error))
def replacement(qual: ScExpression, error: ScExpression): Simplification =
replace(expr).withText(invocationText(qual, replaceWith, error)).highlightFrom(qual)

expr match {
case zio `.orElse` `ZIO.fail`(_, error) => Some(replacement(zio, error))
case zio `.orElse` (block: ScBlock) =>
case qual `.orElse` `ZIO.fail`(_, error) => Some(replacement(qual, error))
case qual `.orElse` (block: ScBlock) =>
Option(block.statements).collect {
case statements :+ `ZIO.fail`(_, error) => blockReplacement(zio, statements :+ error)
case statements :+ `ZIO.fail`(_, error) => blockReplacement(qual, statements :+ error)
}
case _ => None
}
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(invocationTextFor(qual, s"delay${duration.getWrappedText}")).highlightAll
replace(expr).withText(invocationText(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(invocationTextFor(qual, s"$replaceWith(${simplifyFunctionCall(param, body)})"))
.withText(invocationText(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(invocationTextFor(qual, s"tapBoth(${tapError.getBracedText}, ${tap.getBracedText})"))
.withText(invocationText(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 _ => invocationTextFor(effect, methodName)
case _ => invocationText(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(invocationTextFor(qual, "unit"))
def replacement(qual: ScExpression) = replace(expr).withText(invocationText(qual, "unit")).highlightFrom(qual)
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(invocationTextFor(qual, s"$replaceWith${x.getWrappedText}")))
Some(replace(expr).withText(invocationText(qual, s"$replaceWith${x.getWrappedText}")).highlightFrom(qual))
case _ => None
}

Expand All @@ -33,8 +33,8 @@ sealed class BaseZipOneOperatorSimplificationType(invocation: Qualified, replace

def replacement(qual: ScExpression, x: ScExpression) =
x match {
case _: ScInfixExpr => replace(expr).withText(s"${qual.getBracedText} $replaceWith (${x.getBracedText})")
case _ => replace(expr).withText(s"${qual.getBracedText} $replaceWith ${x.getBracedText}")
case _: ScInfixExpr => replace(expr).withText(s"${qual.getBracedText} $replaceWith (${x.getBracedText})").highlightFrom(qual)
case _ => replace(expr).withText(s"${qual.getBracedText} $replaceWith ${x.getBracedText}").highlightFrom(qual)
}

expr match {
Expand All @@ -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(invocationTextFor(toKeep, s"as${zioArg.getWrappedText}")).highlightAll
replace(toSimplify).withText(invocationText(toKeep, s"as${zioArg.getWrappedText}")).highlightAll
}

object ZipRightSimplificationType extends BaseZipOneSimplificationType(`.flatMap`, "zipRight")
Expand Down
Loading

0 comments on commit 13b4fd2

Please sign in to comment.