Skip to content

Commit

Permalink
More os-independent way of splitting path into components.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikko Peltonen authored and Mikko Peltonen committed Feb 4, 2010
1 parent 3b6e838 commit 51429b5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/scala/IdeaPlugin.scala
Expand Up @@ -23,8 +23,12 @@ trait IdeaPlugin extends BasicDependencyProject {
}

def projectRelativePath(target: File): String = {
val projectPathComponents = projectPath.getAbsolutePath.split(File.separator).map(Some(_)).toList
val targetPathComponents = target.getAbsolutePath.split(File.separator).map(Some(_)).toList
def pathComponentsOf(f: File): List[String] = {
val p = f.getParentFile
if (p == null) Nil else List(f.getName) ::: pathComponentsOf(p)
}
val projectPathComponents = pathComponentsOf(projectPath).reverseMap(Some(_))
val targetPathComponents = pathComponentsOf(target).reverseMap(Some(_))
val pathComponents = projectPathComponents.zipAll(targetPathComponents, None, None).dropWhile(x => x._1 == x._2)
val (projectBranch, targetBranch) = List.unzip(pathComponents)
val prefix = projectBranch.takeWhile(_ != None).foldLeft("")((acc, x) => acc + "../")
Expand Down

0 comments on commit 51429b5

Please sign in to comment.