Skip to content

Commit 565abde

Browse files
committed
cleanup code
1 parent 2004ec5 commit 565abde

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

compiler/src/dotty/tools/dotc/semanticdb/ExtractSemanticDB.scala

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -595,20 +595,15 @@ object ExtractSemanticDB:
595595

596596
def write(source: SourceFile, occurrences: List[SymbolOccurrence], symbolInfos: List[SymbolInformation])(using Context): Unit =
597597
def absolutePath(path: Path): Path = path.toAbsolutePath.normalize
598-
def common(root: Path, i1: java.util.Iterator[Path], i2: java.util.Iterator[Path]) =
599-
require(root != null)
600-
var res: Path = root
601-
var next: Path = null
602-
while
603-
i1.hasNext && i2.hasNext
604-
&& { next = i1.next; next } == i2.next
605-
do res = res.resolve(next)
606-
res
607-
end common
598+
def commonPrefix[T](z: T)(i1: Iterable[T], i2: Iterable[T])(app: (T, T) => T): T =
599+
(i1 lazyZip i2).takeWhile(p => p(0) == p(1)).map(_(0)).foldLeft(z)(app)
608600
val sourcePath = absolutePath(source.file.jpath)
609601
val sourceRoot =
602+
// Here if `sourceRoot` and `sourcePath` do not share a common prefix then `relPath` will not be normalised,
603+
// containing ../.. etc, which is problematic when appending to `/META-INF/semanticdb/` and will not be accepted
604+
// by Files.createDirectories on JDK 11.
610605
val sourceRoot0 = absolutePath(Paths.get(ctx.settings.sourceroot.value))
611-
common(sourcePath.getRoot, sourcePath.iterator, sourceRoot0.iterator)
606+
commonPrefix(sourcePath.getRoot)(sourcePath.asScala, sourceRoot0.asScala)(_ resolve _)
612607
val semanticdbTarget =
613608
val semanticdbTargetSetting = ctx.settings.semanticdbTarget.value
614609
absolutePath(

0 commit comments

Comments
 (0)