@@ -9,50 +9,44 @@ import core._, core.Decorators.{sourcePos => _}
99import Contexts ._ , NameOps ._ , Symbols ._ , StdNames ._
1010import util ._ , util .Positions ._
1111
12- /** A `tree` coming from `source` */
13- sealed trait SourceTree {
14-
15- /** The underlying tree. */
16- def tree : tpd.Tree
17-
18- /** The source from which `tree` comes. */
19- def source : SourceFile
12+ /**
13+ * A `tree` coming from `source`
14+ *
15+ * `tree` can be either an `Import` or a `NameTree`.
16+ */
17+ case class SourceTree (tree : tpd.Tree /* * really: tpd.Import | tpd.NameTree */ , source : SourceFile ) {
2018
2119 /** The position of `tree` */
2220 final def pos (implicit ctx : Context ): SourcePosition = source.atPos(tree.pos)
23- }
24-
25- /** An import coming from `source` */
26- case class SourceImportTree (tree : tpd.Import , source : SourceFile ) extends SourceTree
27-
28- /** A typechecked `tree` coming from `source` */
29- case class SourceNamedTree (tree : tpd.NameTree , source : SourceFile ) extends SourceTree {
3021
3122 /** The position of the name in `tree` */
32- def namePos (implicit ctx : Context ): SourcePosition = {
33- // FIXME: Merge with NameTree#namePos ?
34- val treePos = tree.pos
35- if (treePos.isZeroExtent || tree.name.toTermName == nme.ERROR )
36- NoSourcePosition
37- else {
38- // Constructors are named `<init>` in the trees, but `this` in the source.
39- val nameLength = tree.name match {
40- case nme.CONSTRUCTOR => nme.this_.toString.length
41- case other => other.stripModuleClassSuffix.show.toString.length
42- }
43- val position = {
44- // FIXME: This is incorrect in some cases, like with backquoted identifiers,
45- // see https://github.com/lampepfl/dotty/pull/1634#issuecomment-257079436
46- val (start, end) =
47- if (! treePos.isSynthetic)
48- (treePos.point, treePos.point + nameLength)
49- else
50- // If we don't have a point, we need to find it
51- (treePos.end - nameLength, treePos.end)
52- Position (start, end, start)
23+ def namePos (implicit ctx : Context ): SourcePosition = tree match {
24+ case tree : tpd.NameTree =>
25+ // FIXME: Merge with NameTree#namePos ?
26+ val treePos = tree.pos
27+ if (treePos.isZeroExtent || tree.name.toTermName == nme.ERROR )
28+ NoSourcePosition
29+ else {
30+ // Constructors are named `<init>` in the trees, but `this` in the source.
31+ val nameLength = tree.name match {
32+ case nme.CONSTRUCTOR => nme.this_.toString.length
33+ case other => other.stripModuleClassSuffix.show.toString.length
34+ }
35+ val position = {
36+ // FIXME: This is incorrect in some cases, like with backquoted identifiers,
37+ // see https://github.com/lampepfl/dotty/pull/1634#issuecomment-257079436
38+ val (start, end) =
39+ if (! treePos.isSynthetic)
40+ (treePos.point, treePos.point + nameLength)
41+ else
42+ // If we don't have a point, we need to find it
43+ (treePos.end - nameLength, treePos.end)
44+ Position (start, end, start)
45+ }
46+ source.atPos(position)
5347 }
54- source.atPos(position)
55- }
48+ case _ =>
49+ NoSourcePosition
5650 }
5751}
5852
@@ -63,19 +57,19 @@ object SourceTree {
6357 Nil
6458 else {
6559 import ast .Trees ._
66- def sourceTreeOfClass (tree : tpd.Tree ): Option [SourceNamedTree ] = tree match {
60+ def sourceTreeOfClass (tree : tpd.Tree ): Option [SourceTree ] = tree match {
6761 case PackageDef (_, stats) =>
6862 stats.flatMap(sourceTreeOfClass).headOption
6963 case tree : tpd.TypeDef if tree.symbol == sym =>
7064 val sourceFile = new SourceFile (sym.sourceFile, Codec .UTF8 )
71- Some (SourceNamedTree (tree, sourceFile))
65+ Some (SourceTree (tree, sourceFile))
7266 case _ =>
7367 None
7468 }
7569
76- def sourceImports (tree : tpd.Tree , sourceFile : SourceFile ): List [SourceImportTree ] = tree match {
70+ def sourceImports (tree : tpd.Tree , sourceFile : SourceFile ): List [SourceTree ] = tree match {
7771 case PackageDef (_, stats) => stats.flatMap(sourceImports(_, sourceFile))
78- case imp : tpd.Import => SourceImportTree (imp, sourceFile) :: Nil
72+ case imp : tpd.Import => SourceTree (imp, sourceFile) :: Nil
7973 case _ => Nil
8074 }
8175
0 commit comments