Skip to content

Commit 44d4cce

Browse files
committed
Add support for pattern bind
1 parent a3ceec2 commit 44d4cce

File tree

5 files changed

+78
-26
lines changed

5 files changed

+78
-26
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/PatternOpsImpl.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ trait PatternOpsImpl extends scala.tasty.reflect.PatternOps with CoreImpl {
3636
def PatternDeco(pattern: Pattern): PatternAPI = new PatternAPI {
3737
def pos(implicit ctx: Context): Position = pattern.sourcePos
3838
def tpe(implicit ctx: Context): Type = pattern.tpe.stripTypeVar
39+
def symbol(implicit ctx: Context): Symbol = pattern.symbol
3940
}
4041

4142
object Pattern extends PatternModule {

library/src/scala/tasty/reflect/PatternOps.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ trait PatternOps extends Core {
1414
def pos(implicit ctx: Context): Position
1515

1616
def tpe(implicit ctx: Context): Type
17+
18+
def symbol(implicit ctx: Context): Symbol
1719
}
1820
implicit def PatternDeco(pattern: Pattern): PatternAPI
1921

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package example
2+
3+
class CaseTest {
4+
def foo (x: Option[Int]) : Int =
5+
x match {
6+
case y @ Some(x) => x
7+
case None => 0
8+
}
9+
}

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,16 @@ class SemanticdbConsumer extends TastyConsumer {
321321
}
322322

323323
def addSelfDefinition(name: String, range: s.Range): Unit = {
324-
var localsymbol = Symbols.Local(local_offset.toString)
325-
local_offset += 1
326-
symbolsCache += ((name, range) -> localsymbol)
327-
occurrences =
328-
occurrences :+
329-
s.SymbolOccurrence(
330-
Some(range),
331-
localsymbol,
332-
s.SymbolOccurrence.Role.DEFINITION
333-
)
324+
var localsymbol = Symbols.Local(local_offset.toString)
325+
local_offset += 1
326+
symbolsCache += ((name, range) -> localsymbol)
327+
occurrences =
328+
occurrences :+
329+
s.SymbolOccurrence(
330+
Some(range),
331+
localsymbol,
332+
s.SymbolOccurrence.Role.DEFINITION
333+
)
334334
}
335335

336336
def symbolToSymbolString(symbol: Symbol): (String, Boolean) = {
@@ -377,10 +377,14 @@ class SemanticdbConsumer extends TastyConsumer {
377377
}
378378
}
379379

380+
val reserverdFunctions: List[String] = "apply" :: "unapply" :: Nil
380381
def addOccurenceTree(tree: Tree,
381382
type_symbol: s.SymbolOccurrence.Role,
382383
range: s.Range,
383384
force_add: Boolean = false): Unit = {
385+
if (type_symbol != s.SymbolOccurrence.Role.DEFINITION && reserverdFunctions
386+
.contains(tree.symbol.name))
387+
return
384388
if (tree.isUserCreated || (force_add && !(!tree.isUserCreated && iterateParent(
385389
tree.symbol) == "java/lang/Object#`<init>`()."))) {
386390
addOccurence(tree.symbol, type_symbol, range)
@@ -515,6 +519,30 @@ class SemanticdbConsumer extends TastyConsumer {
515519
}
516520
}
517521

522+
override def traversePattern(tree: Pattern)(implicit ctx: Context): Unit = {
523+
tree match {
524+
case Pattern.Bind(name, _) => {
525+
println("[bind]",
526+
tree.pos.startColumn,
527+
tree.pos.endColumn,
528+
tree.symbol,
529+
tree.symbol.pos.startColumn,
530+
tree.symbol.pos.endColumn)
531+
addOccurence(
532+
tree.symbol,
533+
s.SymbolOccurrence.Role.REFERENCE,
534+
s.Range(tree.symbol.pos.startLine,
535+
tree.symbol.pos.startColumn,
536+
tree.symbol.pos.endLine,
537+
tree.symbol.pos.startColumn + name.length)
538+
)
539+
super.traversePattern(tree)
540+
}
541+
case _ =>
542+
super.traversePattern(tree)
543+
}
544+
}
545+
518546
var fittedInitClassRange: Option[s.Range] = None
519547
var forceAddBecauseParents: Boolean = false
520548

@@ -531,7 +559,6 @@ class SemanticdbConsumer extends TastyConsumer {
531559
}
532560
case Term.Apply(_, _) => {
533561
super.traverseTree(tree)
534-
535562
}
536563
case ClassDef(classname, constr, parents, selfopt, statements) => {
537564
// we first add the class to the symbol list
@@ -561,27 +588,37 @@ class SemanticdbConsumer extends TastyConsumer {
561588
forceAddBecauseParents = true
562589
parents.foreach(_ match {
563590
case IsTypeTree(t) => traverseTypeTree(t)
564-
case IsTerm(t) => {println(t.pos.startColumn, t.pos.endColumn)
565-
traverseTree(t)}
591+
case IsTerm(t) => {
592+
println(t.pos.startColumn, t.pos.endColumn)
593+
traverseTree(t)
594+
}
566595
})
567596
forceAddBecauseParents = false
568597

569-
570-
571598
selfopt match {
572599
case Some(vdef @ ValDef(name, _, _)) => {
573-
val posColumn : Int = parents.foldLeft(vdef.pos.startColumn)((old : Int, ct : TermOrTypeTree) =>
574-
ct match {
575-
case IsTerm(t) => if (t.pos.endColumn + 3 < old) {t.pos.endColumn+3} else {old}
576-
case _ => old
577-
})
600+
val posColumn: Int = parents.foldLeft(vdef.pos.startColumn)(
601+
(old: Int, ct: TermOrTypeTree) =>
602+
ct match {
603+
case IsTerm(t) =>
604+
if (t.pos.endColumn + 3 < old) { t.pos.endColumn + 3 } else {
605+
old
606+
}
607+
case _ => old
608+
})
578609
println(posColumn)
579610
println(vdef)
580-
println(vdef.pos.startColumn, tree.pos.startColumn, tree.pos.endColumn)
581-
addSelfDefinition(name, s.Range(vdef.pos.startLine, posColumn, vdef.pos.endLine, posColumn + name.length))
611+
println(vdef.pos.startColumn,
612+
tree.pos.startColumn,
613+
tree.pos.endColumn)
614+
addSelfDefinition(name,
615+
s.Range(vdef.pos.startLine,
616+
posColumn,
617+
vdef.pos.endLine,
618+
posColumn + name.length))
582619
println(name)
583620
}
584-
case _ =>
621+
case _ =>
585622
}
586623
selfopt.foreach(traverseTree)
587624

@@ -640,7 +677,9 @@ class SemanticdbConsumer extends TastyConsumer {
640677
}
641678

642679
case Term.This(what) =>
643-
addOccurenceTree(tree, s.SymbolOccurrence.Role.REFERENCE, posToRange(tree.pos).get)
680+
addOccurenceTree(tree,
681+
s.SymbolOccurrence.Role.REFERENCE,
682+
posToRange(tree.pos).get)
644683

645684
case Term.Select(qualifier, _, _) => {
646685
val range = {

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,16 @@ class Tests {
196196
//def testObjects(): Unit = checkFile("example/Objects.scala")
197197
//@Test def testOverrides(): Unit = checkFile("example/Overrides.scala")
198198
//WIP @Test def testPrefixes(): Unit = checkFile("example/Prefixes.scala")
199-
@Test def testSelfs(): Unit = checkFile("example/Selfs.scala")
200-
@Test def testSelfUse(): Unit = checkFile("example/SelfUse.scala")
199+
//@Test def testSelfs(): Unit = checkFile("example/Selfs.scala")
200+
//@Test def testSelfUse(): Unit = checkFile("example/SelfUse.scala")
201201
//WIP @Test def testSynthetic(): Unit = checkFile("example/Synthetic.scala")
202202
//WIP @Test def testTraits(): Unit = checkFile("example/Traits.scala")
203203
//WIP @Test def testTypes(): Unit = checkFile("example/Types.scala")
204204
//@Test def testVals(): Unit = checkFile("example/Vals.scala")
205205
//@Test def testDependantModule(): Unit = checkFile("example/DependantModule.scala")
206206
//@Test def testNew(): Unit = checkFile("example/New.scala")
207207
//@Test def testIgnoredSymbol(): Unit = checkFile("example/IgnoredSymbol.scala")
208+
@Test def testCase(): Unit = checkFile("example/Case.scala")
208209

209210

210211
def testOutput(className: String, expected: String): Unit = {

0 commit comments

Comments
 (0)