Skip to content

Commit

Permalink
Merge pull request #802 from MasseGuillaume/fix-synthetics2
Browse files Browse the repository at this point in the history
Fix legacy sythetics (fix #795)
  • Loading branch information
olafurpg authored Aug 20, 2018
2 parents e446562 + b51b64d commit 881e4cc
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ class DocSemanticdbIndex(val doc: SemanticDoc)
infoToDenotation(doc, s)
)
}
final override def synthetics: Seq[Synthetic] =
final override def synthetics: Seq[Synthetic] = {
doc.sdoc.synthetics.map { s =>
DocSemanticdbIndex.syntheticToLegacy(doc, s)
}
}
override final def messages: Seq[Message] =
doc.sdoc.diagnostics.map { diag =>
val pos = ScalametaInternals.positionFromRange(doc.input, diag.range)
Expand Down Expand Up @@ -179,9 +180,6 @@ object DocSemanticdbIndex {
}

def syntheticToLegacy(doc: SemanticDoc, synthetic: s.Synthetic): Synthetic = {
val pos =
ScalametaInternals.positionFromRange(doc.input, synthetic.range)
new LegacyCodePrinter(doc).convertSynthetic(synthetic, pos)
new LegacyCodePrinter(doc).convertSynthetic(synthetic)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets
import scala.meta._
import scala.meta.inputs.Input
import scala.meta.inputs.Position
import scala.meta.internal.ScalametaInternals
import scala.meta.internal.semanticdb.Scala._
import scala.meta.internal.{semanticdb => s}
import scalafix.internal.patch.DocSemanticdbIndex.InputSynthetic
Expand All @@ -25,7 +26,8 @@ class LegacyCodePrinter(doc: SemanticDoc) {
buf += PositionedSymbol(v0.Symbol(sym), start, end)
}
private def mkString[T](start: String, trees: Seq[T], end: String)(
fn: T => Unit): Unit = {
fn: T => Unit
): Unit = {
if (trees.isEmpty) ()
else {
text.append(start)
Expand Down Expand Up @@ -188,11 +190,13 @@ class LegacyCodePrinter(doc: SemanticDoc) {
mkString("[", targs, "]")(pprint)
}

def convertSynthetic(synthetic: s.Synthetic, pos: Position): v0.Synthetic = {
def convertSynthetic(synthetic: s.Synthetic): v0.Synthetic = {
val pos = ScalametaInternals.positionFromRange(doc.input, synthetic.range)
loop(synthetic.tree)
val input = Input.Stream(
InputSynthetic(text.result(), doc.input, pos.start, pos.end),
StandardCharsets.UTF_8)
StandardCharsets.UTF_8
)
val names = buf.result().map { sym =>
val symPos = Position.Range(input, sym.start, sym.end)
ResolvedName(symPos, sym.symbol, isDefinition = false)
Expand All @@ -203,7 +207,8 @@ class LegacyCodePrinter(doc: SemanticDoc) {
def convertDenotation(
signature: s.Signature,
dflags: Long,
name: String): v0.Denotation = {
name: String
): v0.Denotation = {

pprint(signature)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import scalafix.util.SemanticdbIndex
import scalafix.v0.Database
import scalafix.v0.Denotation
import scalafix.v0.ResolvedName
import scalafix.v0.Synthetic
import scalafix.v0
import scalafix.v1
import scalafix.internal.v1._
Expand Down Expand Up @@ -62,6 +63,9 @@ case class LegacyInMemorySemanticdbIndex(
override def denotation(tree: Tree): Option[Denotation] =
index(tree.pos.input.syntax).denotation(tree)

def synthetics(input: Input): Seq[Synthetic] =
index(input.syntax).synthetics

override def documents: Seq[v0.Document] =
throw new UnsupportedOperationException()

Expand All @@ -73,7 +77,9 @@ case class LegacyInMemorySemanticdbIndex(

object LegacyInMemorySemanticdbIndex {

def load(classpath: Classpath, sourceroot: AbsolutePath): SemanticdbIndex = {
def load(
classpath: Classpath,
sourceroot: AbsolutePath): LegacyInMemorySemanticdbIndex = {
val symtab = ClasspathOps.newSymbolTable(classpath).get
load(classpath, symtab, sourceroot)
}
Expand All @@ -82,7 +88,7 @@ object LegacyInMemorySemanticdbIndex {
classpath: Classpath,
symtab: SymbolTable,
sourceroot: AbsolutePath
): SemanticdbIndex = {
): LegacyInMemorySemanticdbIndex = {
val sourceuri = sourceroot.toURI
val buf = Map.newBuilder[String, SemanticdbIndex]
classpath.entries.foreach { entry =>
Expand Down
12 changes: 12 additions & 0 deletions scalafix-tests/shared/src/main/scala/test/SyntheticTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.collection.breakOut
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

class SyntheticTest(xs: List[Int]) {
def m1(xs: Set[Int]): List[Int] =
xs.to

xs.map(x => x)(breakOut): Set[Int]

Future.sequence(List(Future(1)))(breakOut, global): Future[Seq[Int]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ abstract class BaseSemanticSuite(filename: String)
extends FunSuite
with BeforeAndAfterAll
with DiffAssertions {
private var _db: SemanticdbIndex = _
private var _db: LegacyInMemorySemanticdbIndex = _
private var _input: Input = _
implicit def index: SemanticdbIndex = _db
implicit def index: LegacyInMemorySemanticdbIndex = _db
def input: Input = _input
def source: Source = {
input.parse[Source].get
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package scalafix.tests.core

import scala.meta._
import System.{lineSeparator => nl}

class SyntheticSuite extends BaseSemanticSuite("SyntheticTest") {

test("text") {
val synthetics = index.synthetics(input)
val obtained = synthetics.sortBy(_.position.start).mkString(nl)
val expected =
"""|[200..205): *(canBuildFrom[Int])
| [0..1): * => _star_.
| [2..14): canBuildFrom => scala/collection/immutable/List.canBuildFrom().
| [15..18): Int => scala/Int#
|[200..205): *[List]
| [0..1): * => _star_.
| [2..6): List => scala/package.List#
|[209..215): *[Int, Set[Int]]
| [0..1): * => _star_.
| [2..5): Int => scala/Int#
| [7..10): Set => scala/Predef.Set#
| [11..14): Int => scala/Int#
|[224..232): *(canBuildFrom[Int])
| [0..1): * => _star_.
| [2..14): canBuildFrom => scala/collection/immutable/Set.canBuildFrom().
| [15..18): Int => scala/Int#
|[224..232): *[List[Int], Int, Set[Int]]
| [0..1): * => _star_.
| [2..6): List => scala/collection/immutable/List#
| [7..10): Int => scala/Int#
| [13..16): Int => scala/Int#
| [18..21): Set => scala/Predef.Set#
| [22..25): Int => scala/Int#
|[247..262): *[Int, List]
| [0..1): * => _star_.
| [2..5): Int => scala/Int#
| [7..11): List => scala/collection/immutable/List#
|[263..267): *.apply[Future[Int]]
| [0..1): * => _star_.
| [2..7): apply => scala/collection/immutable/List.apply().
| [8..14): Future => scala/concurrent/Future#
| [15..18): Int => scala/Int#
|[268..277): *(global)
| [0..1): * => _star_.
| [2..8): global => scala/concurrent/ExecutionContext.Implicits.global.
|[268..274): *.apply[Int]
| [0..1): * => _star_.
| [2..7): apply => scala/concurrent/Future.apply().
| [8..11): Int => scala/Int#
|[280..288): *(canBuildFrom[Int])
| [0..1): * => _star_.
| [2..14): canBuildFrom => scala/collection/immutable/List.canBuildFrom().
| [15..18): Int => scala/Int#
|[280..288): *[List[Future[Int]], Int, List[Int]]
| [0..1): * => _star_.
| [2..6): List => scala/collection/immutable/List#
| [7..13): Future => scala/concurrent/Future#
| [14..17): Int => scala/Int#
| [21..24): Int => scala/Int#
| [26..30): List => scala/collection/immutable/List#
| [31..34): Int => scala/Int#""".stripMargin

assertNoDiff(obtained, expected)
}

test("parsable") {
val synthetics = index.synthetics(input)
synthetics.foreach(n => n.text.parse[Term])
}
}

0 comments on commit 881e4cc

Please sign in to comment.