@@ -9,50 +9,44 @@ import core._, core.Decorators.{sourcePos => _}
9
9
import Contexts ._ , NameOps ._ , Symbols ._ , StdNames ._
10
10
import util ._ , util .Positions ._
11
11
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 ) {
20
18
21
19
/** The position of `tree` */
22
20
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 {
30
21
31
22
/** 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)
53
47
}
54
- source.atPos(position)
55
- }
48
+ case _ =>
49
+ NoSourcePosition
56
50
}
57
51
}
58
52
@@ -63,19 +57,19 @@ object SourceTree {
63
57
Nil
64
58
else {
65
59
import ast .Trees ._
66
- def sourceTreeOfClass (tree : tpd.Tree ): Option [SourceNamedTree ] = tree match {
60
+ def sourceTreeOfClass (tree : tpd.Tree ): Option [SourceTree ] = tree match {
67
61
case PackageDef (_, stats) =>
68
62
stats.flatMap(sourceTreeOfClass).headOption
69
63
case tree : tpd.TypeDef if tree.symbol == sym =>
70
64
val sourceFile = new SourceFile (sym.sourceFile, Codec .UTF8 )
71
- Some (SourceNamedTree (tree, sourceFile))
65
+ Some (SourceTree (tree, sourceFile))
72
66
case _ =>
73
67
None
74
68
}
75
69
76
- def sourceImports (tree : tpd.Tree , sourceFile : SourceFile ): List [SourceImportTree ] = tree match {
70
+ def sourceImports (tree : tpd.Tree , sourceFile : SourceFile ): List [SourceTree ] = tree match {
77
71
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
79
73
case _ => Nil
80
74
}
81
75
0 commit comments