Skip to content

Commit 4a68508

Browse files
committed
improve position for self symbols
1 parent 7757bf6 commit 4a68508

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

semanticdb/src/dotty/semanticdb/SemanticdbConsumer.scala

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import scala.meta.internal.{semanticdb => s}
1111
import dotty.semanticdb.Scala.{Descriptor => d}
1212
import dotty.semanticdb.Scala._
1313

14-
class SemanticdbConsumer extends TastyConsumer {
14+
import scala.io.Source
15+
16+
class SemanticdbConsumer(sourceFile: java.nio.file.Path) extends TastyConsumer {
1517
var stack: List[String] = Nil
1618

1719
val semantic: s.TextDocument = s.TextDocument()
@@ -24,6 +26,8 @@ class SemanticdbConsumer extends TastyConsumer {
2426
val symbolsCache: HashMap[(String, s.Range), String] = HashMap()
2527
var local_offset: Int = 0
2628

29+
val sourceCode = Source.fromFile(sourceFile.toFile).mkString
30+
2731
final def apply(reflect: Reflection)(root: reflect.Tree): Unit = {
2832
import reflect._
2933

@@ -373,7 +377,6 @@ class SemanticdbConsumer extends TastyConsumer {
373377
if (symbolPathsMap.contains(key)) return
374378
if (is_global) {
375379
symbolPathsMap += key
376-
println("duplicates", key)
377380
}
378381
println(symbol_path, range, symbol.owner.flags, is_global)
379382
occurrences =
@@ -591,34 +594,34 @@ class SemanticdbConsumer extends TastyConsumer {
591594
parents.foreach(_ match {
592595
case IsTypeTree(t) => traverseTypeTree(t)
593596
case IsTerm(t) => {
594-
println(t.pos.startColumn, t.pos.endColumn)
595597
traverseTree(t)
596598
}
597599
})
598600
forceAddBecauseParents = false
599601

600602
selfopt match {
601-
case Some(vdef @ ValDef(name, _, _)) => {
602-
val posColumn: Int = parents.foldLeft(vdef.pos.startColumn)(
603+
case Some(vdef @ ValDef(name, _, _)) if name != "_" => {
604+
// To find the current position, we will heuristically
605+
// reparse the source code.
606+
// The process is done in three steps:
607+
// 1) Find a position before the '{' of the self but after any
608+
// non related '{'. Here, it will be the largest end pos of a parent
609+
// 2) Find the first '{'
610+
// 3) Iterate until the character we are seeing is a letter
611+
val startPosSearch: Int = parents.foldLeft(tree.pos.endColumn)(
603612
(old: Int, ct: TermOrTypeTree) =>
604613
ct match {
605-
case IsTerm(t) =>
606-
if (t.pos.endColumn + 3 < old) { t.pos.endColumn + 3 } else {
607-
old
608-
}
614+
case IsTerm(t) if t.pos.endColumn < old => t.pos.endColumn
609615
case _ => old
610616
})
611-
println(posColumn)
612-
println(vdef)
613-
println(vdef.pos.startColumn,
614-
tree.pos.startColumn,
615-
tree.pos.endColumn)
617+
var posColumn = sourceCode.indexOf("{", startPosSearch)
618+
while (!sourceCode(posColumn).isLetter && posColumn < sourceCode.length) posColumn += 1
619+
616620
addSelfDefinition(name,
617621
s.Range(vdef.pos.startLine,
618622
posColumn,
619623
vdef.pos.endLine,
620624
posColumn + name.length))
621-
println(name)
622625
}
623626
case _ =>
624627
}

semanticdb/test/dotty/semanticdb/Tests.scala

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Tests {
129129
scalac
130130
} else {
131131
val (classpaths, classnames) = tasty_classes.unzip
132-
val sdbconsumer = new SemanticdbConsumer
132+
val sdbconsumer = new SemanticdbConsumer(scalaFile)
133133

134134
val _ = ConsumeTasty(classpaths.head, classnames, sdbconsumer)
135135
sdbconsumer.toSemanticdb(scalac.text)
@@ -191,12 +191,12 @@ class Tests {
191191
//@Test def testLocals(): Unit = checkFile("example/Locals.scala")
192192
//@Test def testMacroAnnotations(): Unit = checkFile("example/MacroAnnotations.scala")
193193
//WIP(assert) @Test def testMethods(): Unit = checkFile("example/Methods.scala")
194-
@Test def testMultiArguments(): Unit = checkFile("example/MultiArguments.scala")
194+
//@Test def testMultiArguments(): Unit = checkFile("example/MultiArguments.scala")
195195
//@Test def testMethodUsages(): Unit = checkFile("example/MethodUsages.scala")
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")
199+
@Test def testSelfs(): Unit = checkFile("example/Selfs.scala")
200200
//@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")
@@ -206,13 +206,4 @@ class Tests {
206206
//@Test def testNew(): Unit = checkFile("example/New.scala")
207207
//@Test def testIgnoredSymbol(): Unit = checkFile("example/IgnoredSymbol.scala")
208208
//@Test def testCase(): Unit = checkFile("example/Case.scala")
209-
210-
211-
def testOutput(className: String, expected: String): Unit = {
212-
val out = new StringBuilder
213-
ConsumeTasty(tastyClassDirectory, List(className), new SemanticdbConsumer {
214-
override def println(x: Any): Unit = out.append(x).append(";")
215-
})
216-
assertEquals(expected, out.result())
217-
}
218209
}

0 commit comments

Comments
 (0)