Skip to content

Commit

Permalink
Compute package prefix from bsp
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed Mar 13, 2020
1 parent 229b615 commit 038b78b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ private[resolver] object BspResolverDescriptors {
scalacOptions: Try[ScalacOptionsResult] // TODO should be optional
)

private[resolver] case class SourceDirectory(directory: File, generated: Boolean)
private[resolver] case class SourceDirectory(directory: File, generated: Boolean, packagePrefix: Option[String])

}
32 changes: 19 additions & 13 deletions bsp/src/org/jetbrains/bsp/project/resolver/BspResolverLogic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.jetbrains.bsp.project.resolver

import java.io.File
import java.net.URI
import java.nio.file.Path
import java.nio.file.{Path, Paths}
import java.util.Collections

import ch.epfl.scala.bsp4j._
Expand Down Expand Up @@ -162,22 +162,35 @@ private[resolver] object BspResolverLogic {

sourceItems
.map { item =>
val packagePrefix = findPackagePrefix(sourcesItem, item.getUri)
val file = item.getUri.toURI.toFile
// bsp spec used to depend on uri ending in `/` to determine directory, use both kind and uri string to determine directory
if (item.getKind == SourceItemKind.DIRECTORY || item.getUri.endsWith("/"))
SourceDirectory(file, item.getGenerated)
SourceDirectory(file, item.getGenerated, packagePrefix)
else
// use the file's immediate parent as best guess of source dir
// IntelliJ project model doesn't have a concept of individual source files
SourceDirectory(file.getParentFile, item.getGenerated)
SourceDirectory(file.getParentFile, item.getGenerated, packagePrefix)
}
.distinct
}

private def findPackagePrefix(sourcesItem: SourcesItem, sourceUri: String): Option[String] = {
val roots = Option(sourcesItem.getRoots).map(_.asScala).getOrElse(Nil)
val matchedRoot = roots.find(root => sourceUri.startsWith(root))
matchedRoot.map { root =>
val rootPath = Paths.get(new URI(root))
val filePath = Paths.get(new URI(sourceUri))
val dirPath = if (sourceUri.endsWith("/")) filePath else filePath.getParent
val relativePath = rootPath.relativize(dirPath)
relativePath.toString.replace(File.separatorChar, '.')
}.filter(_.nonEmpty)
}

private def sourceDirectory(uri: String, generated: Boolean = false) = {
val file = uri.toURI.toFile
if (uri.endsWith("/")) SourceDirectory(file, generated)
else SourceDirectory(file.getParentFile, generated)
if (uri.endsWith("/")) SourceDirectory(file, generated, None)
else SourceDirectory(file.getParentFile, generated, None)
}

private def filterRoots(dirs: Seq[SourceDirectory]) = dirs.filter { dir =>
Expand Down Expand Up @@ -432,13 +445,6 @@ private[resolver] object BspResolverLogic {
projectNode
}

private def idToModule(modules: Seq[(scala.Seq[BuildTarget], DataNode[ModuleData])]) = for {
(targets,module) <- modules
target <- targets
} yield {
(TargetId(target.getId.getUri), module)
}

private[resolver] def moduleFilesDirectory(workspace: File) = new File(workspace, ".idea/modules")

private[resolver] def createModuleNode(projectRootPath: String,
Expand Down Expand Up @@ -517,7 +523,7 @@ private[resolver] object BspResolverLogic {

val contentRootData = allSourceRoots.map { case (sourceType, root) =>
val data = getContentRoot(root.directory, moduleBase)
data.storePath(sourceType, root.directory.getCanonicalPath)
data.storePath(sourceType, root.directory.getCanonicalPath, root.packagePrefix.orNull)
data.getRootPath -> data
}.toMap.values // effectively deduplicate by content root path. ContentRootData does not implement equals correctly

Expand Down
2 changes: 1 addition & 1 deletion project/dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Versions {
val bloopVersion = "1.4.0-RC1-74-e0147604"
val zincVersion = "1.1.1"
val intellijVersion = "201.6073.9"
val bspVersion = "2.0.0-M6"
val bspVersion = "2.0.0-M7"
val sbtStructureVersion: String = "2018.2.1+4-88400d3f"
val sbtIdeaShellVersion: String = "2018.3"
val sbtIdeaCompilerIndicesVersion = "0.1.3"
Expand Down

0 comments on commit 038b78b

Please sign in to comment.