File: ParserUtil.scala

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (52 lines) | stat: -rw-r--r-- 2,366 bytes parent folder | download | duplicates (3)
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
import sbt._
import sbt.complete.Parser._
import sbt.complete.Parsers._
import sbt.complete._

object ParserUtil {
  def notStartingWith(parser: Parser[String], c: Char): Parser[String] = parser & not(c ~> any.*, "value cannot start with " + c + ".")
  def concat(p: Parser[(String, String)]): Parser[String] = {
    p.map(x => x._1 + x._2)
  }

  def Opt(a: Parser[String]) = a.?.map(_.getOrElse(""))

  val StringBasicNotStartingWithDash = notStartingWith(StringBasic, '-')
  val IsDirectoryFilter = new SimpleFileFilter(_.isDirectory)
  val JarOrDirectoryParser = FileParser(GlobFilter("*.jar") || IsDirectoryFilter)
  def FileParser(fileFilter: FileFilter, dirFilter: FileFilter = AllPassFilter, base: File = file(".")) = {
    def matching(prefix: String): List[String] = {
      val preFile = file(prefix)
      val cwd = base
      val parent = Option(preFile.getParentFile).getOrElse(cwd)
      if (preFile.exists) {
        if (preFile.isDirectory) {
          preFile.*(IsDirectoryFilter.&&(dirFilter) || fileFilter).get.map(_.getPath).toList
        } else {
          List(preFile).filter(fileFilter.accept).map(_.getPath)
        }
      }
      else if (parent != null) {
        def ensureSuffix(s: String, suffix: String) = if (s.endsWith(suffix)) s else s + suffix
        def pathOf(f: File): String = {
          val f1 = if (preFile.getParentFile == null) f.relativeTo(cwd).getOrElse(f) else f
          if (f1.isDirectory && !fileFilter.accept(f1)) ensureSuffix(f1.getPath, "/") else f1.getPath
        }
        val childFilter = GlobFilter(preFile.name + "*") && ((IsDirectoryFilter && dirFilter) || fileFilter)
        val children = parent.*(childFilter).get
        children.map(pathOf).toList
      } else Nil
    }
    def displayPath = Completions.single(Completion.displayOnly("<path>"))
    token(StringBasic, TokenCompletions.fixed((seen, level) => if (seen.isEmpty) displayPath else matching(seen) match {
      case Nil => displayPath
      case x :: Nil =>
        if (fileFilter.accept(file(x)))
          Completions.strict(Set(Completion.tokenDisplay(x.stripPrefix(seen), x)))
        else
          Completions.strict(Set(Completion.suggestion(x.stripPrefix(seen))))
      case xs =>
        Completions.strict(xs.map(x => Completion.tokenDisplay(x.stripPrefix(seen), x)).toSet)
    })).filter(!_.startsWith("-"), x => x)
  }
}