Skip to content

Commit db2e922

Browse files
committed
Support locally, implicitly. Assert are experimental
1 parent 051c726 commit db2e922

File tree

6 files changed

+94
-12
lines changed

6 files changed

+94
-12
lines changed

semanticdb/input/src/main/scala/example/Anonymous.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import scala.language.higherKinds
44
class Anonymous {
55
this: Anonymous =>
66

7-
/*def m1[T[_], B] = ???
7+
def m1[T[_], B] = ???
88
def m2: Map[_, List[_]] = ???
99
locally {
1010
??? match { case _: List[_] => }
11-
}*/
11+
}
1212
locally {
1313
val x: Int => Int = _ => ???
1414
}
15-
/*
15+
1616
trait Foo
17-
var x = new Foo {}*/
17+
var x = new Foo {}
1818
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package example
2+
3+
class PredefsDotty {
4+
locally {
5+
val x: Int => Int = _ => ???
6+
}
7+
//assert(true)
8+
//assert(false, "bonjour")
9+
}

semanticdb/input/src/main/scala/example/MethodUsages.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package example
22

33
class MethodUsages {
44
val m = new Methods[Int]
5-
/*m.m1
5+
m.m1
66
m.m2()
77
m.m3(0)
88
m.m4(0)(0)
@@ -13,7 +13,7 @@ class MethodUsages {
1313
m.m6(Nil)
1414
m.m7a(m, new m.List[Int])
1515
m.m7b(new m.List[Int])
16-
*/m.`m8().`()/*
16+
m.`m8().`()
1717
m.m9(null)
1818
m.m10(null)
1919
m.m11(Predef)
@@ -29,5 +29,5 @@ class MethodUsages {
2929
m.m17("")
3030
m.m18.m()
3131
m.m18(1)
32-
m.m18("")*/
32+
m.m18("")
3333
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package example
2+
3+
class SuperA {
4+
def a(x:Int) = x
5+
}
6+
7+
class SuperB extends SuperA {
8+
override def a(x:Int) = super.a(x)
9+
}

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
6666
def isUserCreated: Boolean = {
6767
val children: List[Position] =
6868
ChildTraverser.getChildren(tree)(reflect.rootContext).map(_.pos)
69+
/*println("call to isusercreated on " + iterateParent(tree.symbol))
70+
if (tree.pos.exists)
71+
println(tree.pos.start, tree.pos.end)
72+
println(children.map(x => (x.start, x.end)))*/
6973
return !((tree.pos.exists && tree.pos.start == tree.pos.end && children == Nil) || children
7074
.exists(_ == tree.pos))
7175
}
@@ -251,9 +255,16 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
251255
symbol.name != tpnme.THIS.toString
252256
}
253257

258+
def isAnonymousInit(implicit ctx: Context): Boolean = {
259+
return symbol.owner != NoSymbol &&
260+
(symbol.owner.isAnonymousFunction || symbol.owner.isAnonymousClass) &&
261+
symbol.name == "<init>"
262+
}
263+
254264
def isUseless(implicit ctx: Context): Boolean = {
255265
symbol == NoSymbol ||
256266
//symbol.isInitChild ||
267+
symbol.isAnonymousInit ||
257268
symbol.isDefaultGetter ||
258269
symbol.isWildCard ||
259270
symbol.isAnonymousClass ||
@@ -353,6 +364,15 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
353364
}
354365
}
355366

367+
def addOccurencePredef(parent: String, name: String, range: s.Range): Unit = {
368+
occurrences =
369+
occurrences :+
370+
s.SymbolOccurrence(
371+
Some(range),
372+
parent + name + "().",
373+
s.SymbolOccurrence.Role.DEFINITION
374+
)
375+
}
356376
def addSelfDefinition(name: String, range: s.Range): Unit = {
357377
var localsymbol = Symbols.Local(local_offset.toString)
358378
local_offset += 1
@@ -393,6 +413,8 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
393413
case _ =>
394414
symbolToSymbolString(symbol)
395415
}
416+
// We want to add symbols coming from our file
417+
// if (symbol.pos.sourceFile != sourceFile) return
396418
if (symbol_path == "" || symbol.isUselessOccurrence) return
397419

398420
val key = (symbol_path, range)
@@ -436,6 +458,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
436458
def addOccurenceTypeTree(typetree: TypeTree,
437459
type_symbol: s.SymbolOccurrence.Role,
438460
range: s.Range): Unit = {
461+
println(typetree.isUserCreated)
439462
if (typetree.isUserCreated) {
440463
addOccurence(typetree.symbol, type_symbol, range)
441464
}
@@ -571,8 +594,9 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
571594
s.SymbolOccurrence.Role.REFERENCE,
572595
posToRange(typetree.pos).get)
573596
}
574-
case _ =>
597+
case _ => {
575598
super.traverseTypeTree(tree)
599+
}
576600
}
577601
}
578602

@@ -597,6 +621,14 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
597621
var fittedInitClassRange: Option[s.Range] = None
598622
var forceAddBecauseParents: Boolean = false
599623

624+
def getNumberParametersInit(defdef: DefDef)(implicit ctx: Context): Int = {
625+
defdef match {
626+
case DefDef(_, typeParams, paramss, _, _) =>
627+
paramss.foldLeft(0)((old, c) => old + c.length) + typeParams.length
628+
case _ => 0
629+
}
630+
}
631+
600632
override def traverseTree(tree: Tree)(implicit ctx: Context): Unit = {
601633
tree match {
602634
case Import(path, selectors) =>
@@ -645,7 +677,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
645677
forceAddBecauseParents = false
646678

647679
selfopt match {
648-
case Some(vdef @ ValDef(name, _, _)) if name != "_" => {
680+
case Some(vdef @ ValDef(name, type_, _)) if name != "_" => {
649681
// To find the current position, we will heuristically
650682
// reparse the source code.
651683
// The process is done in three steps:
@@ -668,11 +700,13 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
668700
posColumn,
669701
0,
670702
posColumn + name.length))
703+
println(type_)
704+
traverseTypeTree(type_)
671705
}
672706
case _ =>
673707
}
674708

675-
statements.foreach(traverseTree)
709+
statements.takeRight(statements.length - getNumberParametersInit(constr)).foreach(traverseTree)
676710

677711
}
678712
case IsDefinition(cdef) => {
@@ -740,7 +774,7 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
740774
r.endCharacter + 1)
741775
else r
742776
}
743-
addOccurenceTree(tree, s.SymbolOccurrence.Role.REFERENCE, range)
777+
addOccurenceTree(tree, s.SymbolOccurrence.Role.REFERENCE, range, forceAddBecauseParents)
744778
super.traverseTree(tree)
745779
}
746780

@@ -752,6 +786,33 @@ class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
752786
super.traverseTree(tree)
753787
}
754788

789+
case Term.Inlined(Some(c), b, d) => {
790+
def extractPos(x: TermOrTypeTree) = x match {
791+
case IsTerm(t) => t.pos
792+
case IsTypeTree(t) => t.pos
793+
}
794+
def extractSymbol(x: TermOrTypeTree) = x match {
795+
case IsTerm(t) => t.symbol
796+
case IsTypeTree(t) => t.symbol
797+
}
798+
def getPredefFunction(pos: Int): String = {
799+
sourceCode(pos) match {
800+
case 'l' => "locally"
801+
case 'i' => "implicitly"
802+
case 'a' => "assert"
803+
}
804+
}
805+
val parentSymbol = iterateParent(extractSymbol(c))
806+
if (parentSymbol == "dotty/DottyPredef.") {
807+
val pos = extractPos(c)
808+
val function = getPredefFunction(pos.start)
809+
val range = s.Range(pos.startLine, pos.startColumn, pos.startLine, pos.startColumn + function.length)
810+
addOccurencePredef(parentSymbol, function, range)
811+
}
812+
813+
super.traverseTree(tree)
814+
}
815+
755816
case PackageClause(_) =>
756817
val key = (tree.symbol.trueName, tree.pos.start)
757818
if (!package_definitions(key)) {

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,11 @@ class Tests {
206206
@Test def testCase(): Unit = checkFile("example/Case.scala")
207207
@Test def testApply(): Unit = checkFile("example/Apply.scala")
208208
@Test def testMethodUsages(): Unit = checkFile("example/MethodUsages.scala")
209+
@Test def testSuper(): Unit = checkFile("example/Super.scala")
209210
@Test def testTypeBug(): Unit = checkFile("example/TypeBug.scala")*/
210211
//@Test def testTraits(): Unit = checkFile("example/Traits.scala")
211212
//@Test def testSynthetic(): Unit = checkFile("example/Synthetic.scala")
212213
//@Test def testBinaryOp(): Unit = checkFile("example/BinaryOp.scala")
213-
}
214+
//@Test def testAnonymous(): Unit = checkFile("example/Anonymous.scala")
215+
@Test def testDottyPredef(): Unit = checkFile("example/DottyPredef.scala")
216+
}

0 commit comments

Comments
 (0)