From ac4712cac72fb73b7725f53b42db7484ec609102 Mon Sep 17 00:00:00 2001 From: Matej Urbas Date: Sun, 6 Apr 2014 22:48:22 +0100 Subject: [PATCH] Moved ExampleSource into a separate file. --- .../scala/sbt/complete/ExampleSource.scala | 49 +++++++++++++++++++ .../src/main/scala/sbt/complete/Parser.scala | 21 -------- .../src/main/scala/sbt/complete/Parsers.scala | 25 ---------- 3 files changed, 49 insertions(+), 46 deletions(-) create mode 100644 util/complete/src/main/scala/sbt/complete/ExampleSource.scala diff --git a/util/complete/src/main/scala/sbt/complete/ExampleSource.scala b/util/complete/src/main/scala/sbt/complete/ExampleSource.scala new file mode 100644 index 000000000..be46bc587 --- /dev/null +++ b/util/complete/src/main/scala/sbt/complete/ExampleSource.scala @@ -0,0 +1,49 @@ +package sbt.complete + +import java.io.File + +/** + * These sources of examples are used in parsers for user input completion. An example of such a source is the + * [[sbt.complete.FileExamples]] class, which provides a list of suggested files to the user as they press the + * TAB key in the console. + */ +trait ExampleSource +{ + /** + * @return a (possibly lazy) list of completion example strings. These strings are continuations of user's input. The + * user's input is incremented with calls to [[withAddedPrefix]]. + */ + def apply(): Iterable[String] + + /** + * @param addedPrefix a string that just typed in by the user. + * @return a new source of only those examples that start with the string typed by the user so far (with addition of + * the just added prefix). + */ + def withAddedPrefix(addedPrefix: String): ExampleSource +} + +/** + * Provides path completion examples based on files in the base directory. + * @param base the directory within which this class will search for completion examples. + * @param prefix the part of the path already written by the user. + */ +class FileExamples(base: File, prefix: String = "") extends ExampleSource { + private val relativizedPrefix: String = "." + File.separator + prefix + + override def apply(): Iterable[String] = files(base).map(_.toString.substring(relativizedPrefix.length)) + + override def withAddedPrefix(addedPrefix: String): FileExamples = new FileExamples(base, prefix + addedPrefix) + + protected def fileStartsWithPrefix(path: File): Boolean = path.toString.startsWith(relativizedPrefix) + + protected def directoryStartsWithPrefix(path: File): Boolean = { + val pathString = path.toString + pathString.startsWith(relativizedPrefix) || relativizedPrefix.startsWith(pathString) + } + + protected def files(directory: File): Iterable[File] = { + val (subDirectories, filesOnly) = directory.listFiles().toStream.partition(_.isDirectory) + filesOnly.filter(fileStartsWithPrefix) ++ subDirectories.filter(directoryStartsWithPrefix).flatMap(files) + } +} \ No newline at end of file diff --git a/util/complete/src/main/scala/sbt/complete/Parser.scala b/util/complete/src/main/scala/sbt/complete/Parser.scala index 48d137e00..83ac69f2d 100644 --- a/util/complete/src/main/scala/sbt/complete/Parser.scala +++ b/util/complete/src/main/scala/sbt/complete/Parser.scala @@ -718,27 +718,6 @@ private final class Examples[T](delegate: Parser[T], fixed: Set[String]) extends Completions(fixed map(f => Completion.suggestion(f)) ) override def toString = "examples(" + delegate + ", " + fixed.take(2) + ")" } - -/** - * These sources of examples are used in parsers for user input completion. An example of such a source is the - * [[sbt.complete.Parsers.FileExamples]] class, which provides a list of suggested files to the user as they press the - * TAB key in the console. - */ -abstract class ExampleSource -{ - /** - * @return a (possibly lazy) list of completion example strings. These strings are continuations of user's input. The - * user's input is incremented with calls to [[withAddedPrefix]]. - */ - def apply(): Iterable[String] - - /** - * @param addedPrefix a string that just typed in by the user. - * @return a new source of only those examples that start with the string typed by the user so far (with addition of - * the just added prefix). - */ - def withAddedPrefix(addedPrefix: String): ExampleSource -} private final class DynamicExamples[T](delegate: Parser[T], exampleSource: ExampleSource, maxNumberOfExamples: Int) extends ValidParser[T] { def derive(c: Char) = examples(delegate derive c, exampleSource.withAddedPrefix(c.toString), maxNumberOfExamples) diff --git a/util/complete/src/main/scala/sbt/complete/Parsers.scala b/util/complete/src/main/scala/sbt/complete/Parsers.scala index 375a622f9..911253332 100644 --- a/util/complete/src/main/scala/sbt/complete/Parsers.scala +++ b/util/complete/src/main/scala/sbt/complete/Parsers.scala @@ -128,31 +128,6 @@ trait Parsers /** Returns true if `c` is an ASCII letter or digit. */ def alphanum(c: Char) = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') - /** - * Provides path completion examples based on files in the base directory. - * @param base the directory within which this class will search for completion examples. - * @param prefix the part of the path already written by the user. - */ - class FileExamples(base: File, prefix: String = "") extends ExampleSource { - private val relativizedPrefix: String = "." + File.separator + prefix - - override def apply(): Iterable[String] = files(base).map(_.toString.substring(relativizedPrefix.length)) - - override def withAddedPrefix(addedPrefix: String): FileExamples = new FileExamples(base, prefix + addedPrefix) - - protected def fileStartsWithPrefix(path: File): Boolean = path.toString.startsWith(relativizedPrefix) - - protected def directoryStartsWithPrefix(path: File): Boolean = { - val pathString = path.toString - pathString.startsWith(relativizedPrefix) || relativizedPrefix.startsWith(pathString) - } - - protected def files(directory: File): Iterable[File] = { - val (subDirectories, filesOnly) = directory.listFiles().toStream.partition(_.isDirectory) - filesOnly.filter(fileStartsWithPrefix) ++ subDirectories.filter(directoryStartsWithPrefix).flatMap(files) - } - } - /** * @param base the directory used for completion proposals (when the user presses the TAB key). Only paths under this * directory will be proposed.