Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created August 25, 2025 06:55
Show Gist options
  • Save xuwei-k/d480c4025bf51ed972b22e0bc54946c6 to your computer and use it in GitHub Desktop.
Save xuwei-k/d480c4025bf51ed972b22e0bc54946c6 to your computer and use it in GitHub Desktop.
import scala.meta.*
import scala.meta.tokens.Token
import scalafix.v1.*
class ScalikejdbcContextFunction extends SyntacticRule("ScalikejdbcContextFunction") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case Term.ApplyInfix.After_4_6_0(
Term.Name("DB"),
readOnly @ Term.Name("localTx"),
_,
Term.ArgClause(
List(
p @ Term.PartialFunction(
List(
c @ Case(
Pat.Given(session @ Type.Name("DBSession")),
None,
_
)
)
)
),
None
)
) =>
Seq(
Patch.replaceTree(readOnly, ".withLocalTxSession"),
Patch.removeTokens(
session.tokens ++ Seq(
c.tokens.find(_.is[Token.KwCase]),
c.tokens.find(_.is[Token.KwGiven]),
c.tokens.find(_.is[Token.FunctionArrow]),
p.tokens.find(_.is[Token.LF])
).flatten
)
).asPatch
}.asPatch
}
}
package fix
import scala.meta.*
import scala.meta.tokens.Token
import scalafix.v1.*
class ScalikejdbcContextFunction extends SyntacticRule("ScalikejdbcContextFunction") {
override def fix(implicit doc: SyntacticDocument): Patch = {
doc.tree.collect {
case Term.ApplyInfix.After_4_6_0(
Term.Name("DB"),
readOnly @ Term.Name("readOnly"),
_,
Term.ArgClause(
List(
p @ Term.PartialFunction(
List(
c @ Case(
Pat.Given(session @ Type.Name("DBSession")),
None,
_
)
)
)
),
None
)
) =>
Seq(
Patch.replaceTree(readOnly, ".withReadOnlySession"),
Patch.removeTokens(
session.tokens ++ Seq(
c.tokens.find(_.is[Token.KwCase]),
c.tokens.find(_.is[Token.KwGiven]),
c.tokens.find(_.is[Token.FunctionArrow]),
p.tokens.find(_.is[Token.LF])
).flatten
)
).asPatch
}.asPatch
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment