forked from scalameta/metals
/
SourcePathAdapter.scala
73 lines (66 loc) · 2.24 KB
/
SourcePathAdapter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scala.meta.internal.metals.debug
import java.net.URI
import scala.meta.internal.io.FileIO
import scala.meta.internal.metals.BuildTargets
import scala.meta.internal.metals.Directories
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.io.AbsolutePath
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
private[debug] final class SourcePathAdapter(
workspace: AbsolutePath,
buildTargets: BuildTargets,
supportVirtualDocuments: Boolean,
) {
// when virtual documents are supported there is no need to save jars on disk
private val saveJarFileToDisk = !supportVirtualDocuments
private val dependencies = workspace.resolve(Directories.dependencies)
def toDapURI(sourcePath: AbsolutePath): Option[URI] = {
if (
!supportVirtualDocuments && sourcePath.toNIO.startsWith(
dependencies.toNIO
)
) {
// if sourcePath is a dependency source file
// we retrieve the original source jar and we build the uri inside the source jar filesystem
for {
dependencySource <- sourcePath.toRelativeInside(dependencies)
dependencyFolder <- dependencySource.toNIO.iterator.asScala.headOption
jarName = dependencyFolder.toString
jarFile <- buildTargets.sourceJarFile(jarName)
relativePath <- sourcePath.toRelativeInside(
dependencies.resolve(jarName)
)
} yield FileIO.withJarFileSystem(jarFile, create = false)(root =>
root.resolve(relativePath.toString).toURI
)
} else {
Some(sourcePath.toURI)
}
}
def toMetalsPath(sourcePath: AbsolutePath): Option[AbsolutePath] = try {
Some(
if (sourcePath.isJarFileSystem)
if (saveJarFileToDisk) sourcePath.toFileOnDisk(workspace)
else sourcePath
else sourcePath
)
} catch {
case e: Throwable =>
scribe.error(s"Could not resolve $sourcePath", e)
None
}
}
private[debug] object SourcePathAdapter {
def apply(
buildTargets: BuildTargets,
targets: Seq[BuildTargetIdentifier],
supportVirtualDocuments: Boolean,
): SourcePathAdapter = {
val workspace = buildTargets.workspaceDirectory(targets.head).get
new SourcePathAdapter(
workspace,
buildTargets,
supportVirtualDocuments,
)
}
}