Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Mar 31, 2024
1 parent 7807a02 commit 803febd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
44 changes: 26 additions & 18 deletions core/src/main/scala-latest-js/scalameta_ast/MainCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait MainCompat {
scalafmtConfig: String,
line: Int,
column: Int,
) = {
): List[(String, Int)] = {
import scala.meta._
val convert = implicitly[Convert[String, Input]]
val main = new ScalametaAST
Expand All @@ -49,7 +49,6 @@ trait MainCompat {
scalafmtConfig = hoconToMetaConfig(scalafmtConfig)
).result
val parsed: Term = implicitly[Parse[Term]].apply(Input.String(res), scala.meta.dialects.Scala3).get
val tokens = parsed.tokens
val cursorPos = {
if (src.isEmpty) {
Position.Range(input, 0, 0)
Expand All @@ -69,37 +68,46 @@ trait MainCompat {
}
}

val t1 = tree.collect {
val t1: List[Tree] = tree.collect {
case x if (x.pos.start <= cursorPos && cursorPos <= x.pos.end) && ((x.pos.end - x.pos.start) >= 1) =>
x
}

implicit class ListOps[A](xs: List[A]) {
def minValues[B: Ordering](f: A => B): List[A] = {
xs.groupBy(f).minBy(_._1)._2
}
}

val t2 = if (t1.size > 1) {
val ss = t1.collect { case t @ PrimitiveTree() => t }
val ss: List[Tree] = t1.minValues(t => t.pos.end - t.pos.start)
if (ss.isEmpty) {
t1
} else {
ss
if (ss.size > 1) {
ss.minValues(_.structure.length) match {
case Nil => ss
case aa => aa
}
} else {
ss
}
}
} else {
t1
}

val t3 = t2.groupBy(_.pos.start).minByOption(_._1).map(_._2.groupBy(_.pos.end))

t3.toList.map(_.minBy(_._1)._2).flatMap { cursorTrees =>
cursorTrees.flatMap { cursorTree =>
val current = cursorTree.structure
t2.flatMap { cursorTree =>
val current = cursorTree.structure
if (false) {
println("current = " + current)
}
val currentSize = current.length
tree.structure.sliding(currentSize).zipWithIndex.find(_._1 == current).map(_._2).map { pos =>
if (false) {
println("current = " + current)
}
val currentSize = current.length
tree.structure.sliding(currentSize).zipWithIndex.find(_._1 == current).map(_._2).map { pos =>
if (false) {
println(Seq("pos" -> pos, "size" -> currentSize))
}
(current, pos)
println(Seq("pos" -> pos, "size" -> currentSize))
}
(current, pos)
}
}
}
Expand Down
45 changes: 37 additions & 8 deletions core/src/test/scala-latest-js/scalameta_ast/MainSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ class MainSpec extends AnyFreeSpec {
check(pos) match {
case List((t, _)) =>
assert(t.startsWith("Defn.Class(Nil"), pos)
case other =>
assert(false, other)
}
}

def checkTemplate(pos: Int)(implicit p: Position) = {
check(pos) match {
case List((t, _)) =>
assert(t.startsWith("Template(Nil, Nil, "), pos)
case other =>
assert(false, other)
}
}

def checkDef(pos: Int)(implicit p: Position) = {
check(pos) match {
case List((t, _)) =>
assert(t.startsWith("""Defn.Def(Nil, Term.Name("a"),"""), pos)
case other =>
assert(false, other)
}
}

Expand All @@ -70,33 +90,42 @@ class MainSpec extends AnyFreeSpec {
(6 to 7).foreach { pos =>
assert(check(pos) == List(("""Type.Name("A")""", 16)), pos)
}
(8 to 14).foreach { pos =>
checkClass(pos)
(8 to 10).foreach { pos =>
checkTemplate(pos)
}
(11 to 14).foreach { pos =>
checkDef(pos)
}
(15 to 16).foreach { pos =>
assert(check(pos) == List(("""Term.Name("a")""", 165)), pos)
}
(17 to 18).foreach { pos =>
assert(check(pos) == List(("""Term.Name("x")""", 276)), pos)
}
checkClass(19) // TODO detect `Def` not `Class`
assert(check(19) == List(("""Term.Param(Nil, Term.Name("x"), Some(Type.Name("Y")), None)""", 260)))
(20 to 21).foreach { pos =>
assert(check(pos) == List(("""Type.Name("Y")""", 297)), pos)
}
(22 to 23).foreach { pos =>
checkClass(pos) // TODO detect `Def` not `Class`
}
assert(
check(22) == List(
(
"""Term.ParamClause(List(Term.Param(Nil, Term.Name("x"), Some(Type.Name("Y")), None)), None)""",
238
)
)
)
checkDef(23)
(24 to 25).foreach { pos =>
assert(check(pos) == List(("""Type.Name("Z")""", 337)), pos)
}
(26 to 30).foreach { pos =>
checkClass(pos) // TODO detect `Def` not `Class`
checkDef(pos)
}
(31 to 32).foreach { pos =>
assert(check(pos) == List(("""Term.Name("b")""", 354)), pos)
}
(33 to 35).foreach { pos =>
checkClass(pos)
checkTemplate(pos)
}
}
}
Expand Down

0 comments on commit 803febd

Please sign in to comment.