-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
metals/src/main/scala/scala/meta/internal/metals/FileSystemProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package scala.meta.internal.metals | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
|
||
import scala.meta.internal.metals.MetalsEnrichments._ | ||
import scala.meta.internal.metals.clients.language.MetalsLanguageClient | ||
import scala.meta.internal.io.FileIO | ||
import scala.meta.io.AbsolutePath | ||
import scala.annotation.tailrec | ||
import java.nio.file.Files | ||
import java.util.stream.Collectors | ||
import java.nio.charset.StandardCharsets | ||
|
||
final case class FSReadDirectoryResponse( | ||
name: String, | ||
isFile: Boolean | ||
) | ||
|
||
final case class FSReadFileResponse( | ||
name: String, | ||
value: String, | ||
error: String | ||
) | ||
|
||
final case class FSStatResponse( | ||
name: String, | ||
isFile: Boolean | ||
) | ||
|
||
final class FileSystemProvider( | ||
languageClient: MetalsLanguageClient, | ||
clientConfig: ClientConfiguration, | ||
buildTargets: BuildTargets | ||
)(implicit ec: ExecutionContext) { | ||
|
||
private val jarFileSystemRoot = "metals_library_dependencies:/" | ||
|
||
private case class JarInfo(path: AbsolutePath, isSource: Boolean) | ||
private var jarNameToPath = Map.empty[String, JarInfo] | ||
|
||
private def isTopLevel(uriAsStr: String) = | ||
uriAsStr == jarFileSystemRoot | ||
|
||
private def isJarLevel(uriAsStr: String) = { | ||
val possibleJarName = uriAsStr.stripPrefix(jarFileSystemRoot) | ||
possibleJarName.nonEmpty && !possibleJarName.contains('/') | ||
} | ||
|
||
private def getJarName(uriAsStr: String): Option[String] = { | ||
val possibleJarName = uriAsStr.stripPrefix(jarFileSystemRoot) | ||
if (possibleJarName.nonEmpty) { | ||
Some(possibleJarName.takeWhile(_ != '/')) | ||
} else None | ||
} | ||
|
||
private def getTopLevelJars: List[FSReadDirectoryResponse] = { | ||
if (jarNameToPath.isEmpty) { | ||
val workspaceJars = buildTargets.allWorkspaceJars.toList | ||
val sourceAndSourcelessJars = workspaceJars.map(jar => { | ||
val sourceJarName = jar.filename.replace(".jar", "-sources.jar") | ||
buildTargets | ||
.sourceJarFile(sourceJarName) | ||
.map(sourceJar => JarInfo(sourceJar, isSource = true)) | ||
.getOrElse(JarInfo(jar, isSource = false)) | ||
}) | ||
jarNameToPath = | ||
sourceAndSourcelessJars.map(f => f.path.filename -> f).toMap | ||
} | ||
jarNameToPath.keys.map(FSReadDirectoryResponse(_, false)).toList | ||
} | ||
|
||
private def getJarDirs(uriAsStr: String, jarName: String): List[String] = { | ||
@tailrec | ||
def getDirs(remainingUri: String, dirs: List[String]): List[String] = { | ||
if (remainingUri.isEmpty) | ||
dirs | ||
else { | ||
val (head, tail) = remainingUri.stripPrefix("/").span(_ != '/') | ||
getDirs(tail, head :: dirs) | ||
} | ||
} | ||
val remainingUri = | ||
uriAsStr.stripPrefix(jarFileSystemRoot).stripPrefix(jarName) | ||
getDirs(remainingUri, List.empty).reverse | ||
} | ||
|
||
private def accessJar[A]( | ||
uriAsStr: String, | ||
convert: AbsolutePath => A | ||
): Option[A] = { | ||
|
||
@tailrec | ||
def resolve(path: AbsolutePath, dirs: List[String]): AbsolutePath = { | ||
if (dirs.isEmpty) | ||
path | ||
else | ||
resolve(path.resolve(dirs.head), dirs.tail) | ||
} | ||
|
||
for { | ||
jarName <- getJarName(uriAsStr) | ||
jarInfo = jarNameToPath(jarName) | ||
jarDirs = getJarDirs(uriAsStr, jarName) | ||
} yield { | ||
FileIO | ||
.withJarFileSystem( | ||
jarInfo.path, | ||
create = false, | ||
// TODO check if close is correct - copied from FindTextInDependencyJars | ||
close = !jarInfo.isSource | ||
) { root => convert(resolve(root, jarDirs)) } | ||
} | ||
} | ||
|
||
private def getJarChildren( | ||
uriAsStr: String | ||
): List[FSReadDirectoryResponse] = { | ||
accessJar( | ||
uriAsStr, | ||
uriPath => | ||
Files | ||
.list(uriPath.toNIO) | ||
.map(path => | ||
FSReadDirectoryResponse( | ||
path.filename, | ||
isFile = !Files.isDirectory(path) | ||
) | ||
) | ||
.collect(Collectors.toList()) | ||
.asScala | ||
.toList | ||
).getOrElse(List.empty) | ||
} | ||
|
||
private def readFileContents(uriAsStr: String): FSReadFileResponse = { | ||
accessJar( | ||
uriAsStr, | ||
path => { | ||
val contents = | ||
new String(Files.readAllBytes(path.toNIO), StandardCharsets.UTF_8) | ||
FSReadFileResponse(path.filename, contents, null) | ||
} | ||
).getOrElse(FSReadFileResponse(uriAsStr, null, "Unknown error")) | ||
} | ||
|
||
private def getSystemStatInfo(uriAsStr: String): FSStatResponse = { | ||
accessJar( | ||
uriAsStr, | ||
path => | ||
FSStatResponse(path.filename, isFile = !Files.isDirectory(path.toNIO)) | ||
) | ||
// TODO introduce error class | ||
.getOrElse(FSStatResponse("Fake", false)) | ||
} | ||
|
||
def createLibraryFileSystem(): Unit = | ||
if (clientConfig.isLibraryFileSystemSupported()) { | ||
val params = | ||
ClientCommands.CreateLibraryFileSystem.toExecuteCommandParams( | ||
jarFileSystemRoot | ||
) | ||
languageClient.metalsExecuteClientCommand(params) | ||
} | ||
|
||
def readDirectory( | ||
uriAsStr: String | ||
): Future[Array[FSReadDirectoryResponse]] = | ||
Future { | ||
val contents = if (isTopLevel(uriAsStr)) { | ||
getTopLevelJars | ||
} else | ||
getJarChildren(uriAsStr) | ||
contents.toArray | ||
} | ||
|
||
def readFile(uriAsStr: String): Future[FSReadFileResponse] = Future { | ||
readFileContents(uriAsStr) | ||
} | ||
|
||
def getSystemStat(uriAsStr: String): Future[FSStatResponse] = | ||
Future { | ||
if (isTopLevel(uriAsStr) || isJarLevel(uriAsStr)) | ||
FSStatResponse("/", isFile = false) | ||
else | ||
getSystemStatInfo(uriAsStr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters