@@ -19,6 +19,7 @@ import parsing.JavaParsers.OutlineJavaParser
19
19
import parsing .Parsers .OutlineParser
20
20
import reporting .trace
21
21
import ast .desugar
22
+ import collection .mutable .ListBuffer
22
23
23
24
object SymbolLoaders {
24
25
import ast .untpd ._
@@ -105,8 +106,8 @@ object SymbolLoaders {
105
106
}
106
107
107
108
/** Enter all toplevel classes and objects in file `src` into package `owner`, provided
108
- * they are in the right package . Issue a warning if a class or object is in the wrong
109
- * package , i.e. if the file path differs from the declared package clause.
109
+ * they are in the right directory . Issue a warning if a class or object is in the wrong
110
+ * directory , i.e. if the file path is not a prefix of the declared package clause.
110
111
*
111
112
* All entered symbols are given a source completer of `src` as info.
112
113
*/
@@ -115,22 +116,25 @@ object SymbolLoaders {
115
116
scope : Scope = EmptyScope )(implicit ctx : Context ): Unit =
116
117
if src.exists && ! src.isDirectory
117
118
val completer = new SourcefileLoader (src)
118
- val filePath = owner.ownersIterator.takeWhile(! _.isRoot).map(_.name.toTermName).toList
119
-
120
- def addPrefix (pid : RefTree , path : List [TermName ]): List [TermName ] = pid match {
121
- case Ident (name : TermName ) => name :: path
122
- case Select (qual : RefTree , name : TermName ) => name :: addPrefix(qual, path)
123
- case _ => path
124
- }
119
+ val filePath = owner.ownersIterator.takeWhile(! _.isRoot).map(_.name.toTermName).toList.reverse
125
120
126
121
def enterScanned (unit : CompilationUnit )(implicit ctx : Context ) = {
122
+ val path = ListBuffer [TermName ]()
123
+
124
+ def addSuffix (pid : RefTree ): Unit = pid match
125
+ case Ident (name : TermName ) =>
126
+ path += name
127
+ case Select (qual : RefTree , name : TermName ) =>
128
+ addSuffix(qual)
129
+ path += name
130
+ case _ =>
127
131
128
- def checkPathMatches (path : List [ TermName ], what : String , tree : NameTree ): Boolean = {
129
- val ok = filePath == path
132
+ def checkPathMatches (what : String , tree : NameTree ): Boolean = {
133
+ val ok = path.startsWith(filePath)
130
134
if (! ok)
131
135
ctx.warning(i """ $what ${tree.name} is in the wrong directory.
132
- |It was declared to be in package ${path.reverse. mkString(" ." )}
133
- |But it is found in directory ${filePath.reverse. mkString(File .separator)}""" ,
136
+ |It was declared to be in package ${path.mkString(" ." )}
137
+ |But it is found in directory ${filePath.mkString(File .separator)}""" ,
134
138
tree.sourcePos.focus)
135
139
ok
136
140
}
@@ -144,25 +148,26 @@ object SymbolLoaders {
144
148
case _ =>
145
149
tree
146
150
147
- def traverse (tree : Tree , path : List [ TermName ] ): Unit = simpleDesugar(tree) match {
151
+ def traverse (tree : Tree ): Unit = simpleDesugar(tree) match {
148
152
case tree @ PackageDef (pid, body) =>
149
- val path1 = addPrefix(pid, path)
150
- for (stat <- body) traverse(stat, path1)
153
+ val size = path.size
154
+ addSuffix(pid)
155
+ for stat <- body do traverse(stat)
156
+ path.takeInPlace(size)
151
157
case tree : TypeDef if tree.isClassDef =>
152
- if (checkPathMatches(path, " class" , tree))
158
+ if (checkPathMatches(" class" , tree))
153
159
// It might be a case class or implicit class,
154
160
// so enter class and module to be on the safe side
155
161
enterClassAndModule(owner, tree.name, completer, scope = scope)
156
162
case tree : ModuleDef =>
157
- if (checkPathMatches(path, " object" , tree))
163
+ if (checkPathMatches(" object" , tree))
158
164
enterModule(owner, tree.name, completer, scope = scope)
159
165
case _ =>
160
166
}
161
167
162
168
traverse(
163
169
if (unit.isJava) new OutlineJavaParser (unit.source).parse()
164
- else new OutlineParser (unit.source).parse(),
165
- Nil )
170
+ else new OutlineParser (unit.source).parse())
166
171
}
167
172
168
173
val unit = CompilationUnit (ctx.getSource(src.path))
0 commit comments