some renamings and removals suggested by Heiko

clarify command-related members of State:
  processors -> definedCommands
  commands -> remainingCommands
drop some Extracted members:
  curi replaced by currentRef.build
  cid replaced by currentRef.project
This commit is contained in:
Mark Harrah 2011-03-21 17:56:41 -04:00
parent caee48130e
commit f34c3b5837
5 changed files with 33 additions and 33 deletions

View File

@ -80,7 +80,7 @@ object Command
def process(command: String, state: State): State =
{
val parser = combine(state.processors)
val parser = combine(state.definedCommands)
Parser.result(parser(state), command) match
{
case Right(s) => s() // apply command. command side effects happen here

View File

@ -43,7 +43,7 @@ class xMain extends xsbti.AppMain
case Done => Exit(0)
case Reload =>
val app = state.configuration.provider
new Reboot(app.scalaProvider.version, state.commands, app.id, state.configuration.baseDirectory)
new Reboot(app.scalaProvider.version, state.remainingCommands, app.id, state.configuration.baseDirectory)
}
}
def next(state: State): State =
@ -73,7 +73,7 @@ object BuiltinCommands
def helpParser(s: State) =
{
val h = s.processors.flatMap(_.help)
val h = s.definedCommands.flatMap(_.help)
val helpCommands = h.flatMap(_.detail._1)
val args = (token(Space) ~> token( OpOrID.examples(helpCommands : _*) )).*
applyEffect(args)(runHelp(s, h))
@ -114,7 +114,7 @@ object BuiltinCommands
val reader = new FullReader(history, s.combinedParser)
val line = reader.readLine(prompt)
line match {
case Some(line) => s.copy(onFailure = Some(Shell), commands = line +: Shell +: s.commands)
case Some(line) => s.copy(onFailure = Some(Shell), remainingCommands = line +: Shell +: s.remainingCommands)
case None => s
}
}
@ -129,10 +129,10 @@ object BuiltinCommands
lazy val otherCommandParser = (s: State) => token(OptSpace ~> matched(s.combinedParser) )
def ifLast = Command(IfLast, IfLastBrief, IfLastDetailed)(otherCommandParser) { (s, arg) =>
if(s.commands.isEmpty) arg :: s else s
if(s.remainingCommands.isEmpty) arg :: s else s
}
def append = Command(Append, AppendLastBrief, AppendLastDetailed)(otherCommandParser) { (s, arg) =>
s.copy(commands = s.commands :+ arg)
s.copy(remainingCommands = s.remainingCommands :+ arg)
}
def setOnFailure = Command(OnFailure, OnFailureBrief, OnFailureDetailed)(otherCommandParser) { (s, arg) =>
@ -237,7 +237,7 @@ object BuiltinCommands
val extracted = Project extract s
import extracted._
val setting = EvaluateConfigurations.evaluateSetting(session.currentEval(), "<set>", imports(extracted), arg, 0)
val append = Load.transformSettings(Load.projectScope(currentRef), curi, rootProject, setting :: Nil)
val append = Load.transformSettings(Load.projectScope(currentRef), currentRef.build, rootProject, setting :: Nil)
val newSession = session.appendSettings( append map (a => (a, arg)))
reapply(newSession, structure, s)
}
@ -260,8 +260,8 @@ object BuiltinCommands
def autoImports(extracted: Extracted): EvalImports = new EvalImports(imports(extracted), "<auto-imports>")
def imports(extracted: Extracted): Seq[(String,Int)] =
{
import extracted._
structure.units(curi).imports.map(s => (s, -1))
val curi = extracted.currentRef.build
extracted.structure.units(curi).imports.map(s => (s, -1))
}
def listBuild(uri: URI, build: Load.LoadedBuildUnit, current: Boolean, currentID: String, log: Logger) =
@ -276,6 +276,7 @@ object BuiltinCommands
def projects = Command.command(ProjectsCommand, projectsBrief, projectsDetailed ) { s =>
val extracted = Project extract s
import extracted._
import currentRef.{build => curi, project => cid}
val log = logger(s)
listBuild(curi, structure.units(curi), true, cid, log)
for( (uri, build) <- structure.units if curi != uri) listBuild(uri, build, false, cid, log)
@ -340,16 +341,16 @@ object BuiltinCommands
def addAlias(s: State, name: String, value: String): State =
if(Command validID name) {
val removed = removeAlias(s, name)
if(value.isEmpty) removed else removed.copy(processors = newAlias(name, value) +: removed.processors)
if(value.isEmpty) removed else removed.copy(definedCommands = newAlias(name, value) +: removed.definedCommands)
} else {
System.err.println("Invalid alias name '" + name + "'.")
s.fail
}
def removeAliases(s: State): State = removeTagged(s, CommandAliasKey)
def removeAlias(s: State, name: String): State = s.copy(processors = s.processors.filter(c => !isAliasNamed(name, c)) )
def removeAlias(s: State, name: String): State = s.copy(definedCommands = s.definedCommands.filter(c => !isAliasNamed(name, c)) )
def removeTagged(s: State, tag: AttributeKey[_]): State = s.copy(processors = removeTagged(s.processors, tag))
def removeTagged(s: State, tag: AttributeKey[_]): State = s.copy(definedCommands = removeTagged(s.definedCommands, tag))
def removeTagged(as: Seq[Command], tag: AttributeKey[_]): Seq[Command] = as.filter(c => ! (c.tags contains tag))
def isAliasNamed(name: String, c: Command): Boolean = isNamed(name, getAlias(c))
@ -365,12 +366,12 @@ object BuiltinCommands
def aliasNames(s: State): Seq[String] = allAliases(s).map(_._1)
def allAliases(s: State): Seq[(String,String)] = aliases(s, (n,v) => true)
def aliases(s: State, pred: (String,String) => Boolean): Seq[(String,String)] =
s.processors.flatMap(c => getAlias(c).filter(tupled(pred)))
s.definedCommands.flatMap(c => getAlias(c).filter(tupled(pred)))
def newAlias(name: String, value: String): Command =
Command.make(name, (name, "'" + value + "'"), "Alias of '" + value + "'")(aliasBody(name, value)).tag(CommandAliasKey, (name, value))
def aliasBody(name: String, value: String)(state: State): Parser[() => State] =
OptSpace ~> Parser(Command.combine(removeAlias(state,name).processors)(state))(value)
OptSpace ~> Parser(Command.combine(removeAlias(state,name).definedCommands)(state))(value)
val CommandAliasKey = AttributeKey[(String,String)]("is-command-alias")
}

View File

@ -50,10 +50,8 @@ final case class Project(id: String, base: File, aggregate: Seq[ProjectReference
}
final case class Extracted(structure: Load.BuildStructure, session: SessionSettings, currentRef: ProjectRef, rootProject: URI => String)
{
lazy val currentUnit = structure units curi
lazy val currentProject = currentUnit defined cid
def curi = currentRef.build
def cid = currentRef.project
lazy val currentUnit = structure units currentRef.build
lazy val currentProject = currentUnit defined currentRef.project
}
object Project extends Init[Scope]
@ -104,9 +102,9 @@ object Project extends Init[Scope]
val prompt = get(shellPrompt)
val watched = get(watch)
val commandDefs = allCommands.distinct.flatten[Command].map(_ tag (projectCommand, true))
val newProcessors = commandDefs ++ BuiltinCommands.removeTagged(s.processors, projectCommand)
val newDefinedCommands = commandDefs ++ BuiltinCommands.removeTagged(s.definedCommands, projectCommand)
val newAttrs = setCond(Watched.Configuration, watched, s.attributes).put(historyPath.key, history)
s.copy(attributes = setCond(shellPrompt.key, prompt, newAttrs), processors = newProcessors)
s.copy(attributes = setCond(shellPrompt.key, prompt, newAttrs), definedCommands = newDefinedCommands)
}
def setCond[T](key: AttributeKey[T], vopt: Option[T], attributes: AttributeMap): AttributeMap =
vopt match { case Some(v) => attributes.put(key, v); case None => attributes.remove(key) }

View File

@ -25,7 +25,8 @@ object ProjectNavigation
final class ProjectNavigation(s: State)
{
val extracted = Project extract s
import extracted.{curi => uri, cid => pid, structure, session}
import extracted.{currentRef, structure, session}
import currentRef.{build => uri, project => pid}
val builds = structure.units.keys.toSet
val projects = Load.getBuild(structure.units, uri).defined.keys

View File

@ -8,14 +8,14 @@ package sbt
final case class State(
configuration: xsbti.AppConfiguration,
processors: Seq[Command],
definedCommands: Seq[Command],
exitHooks: Set[ExitHook],
onFailure: Option[String],
commands: Seq[String],
remainingCommands: Seq[String],
attributes: AttributeMap,
next: Next.Value
) extends Identity {
lazy val combinedParser = Command.combine(processors)(this)
lazy val combinedParser = Command.combine(definedCommands)(this)
}
trait Identity {
@ -48,35 +48,35 @@ object State
{
implicit def stateOps(s: State): StateOps = new StateOps {
def process(f: (String, State) => State): State =
s.commands match {
case Seq(x, xs @ _*) => f(x, s.copy(commands = xs))
s.remainingCommands match {
case Seq(x, xs @ _*) => f(x, s.copy(remainingCommands = xs))
case Seq() => exit(true)
}
s.copy(commands = s.commands.drop(1))
def ::: (newCommands: Seq[String]): State = s.copy(commands = newCommands ++ s.commands)
s.copy(remainingCommands = s.remainingCommands.drop(1))
def ::: (newCommands: Seq[String]): State = s.copy(remainingCommands = newCommands ++ s.remainingCommands)
def :: (command: String): State = (command :: Nil) ::: this
def ++ (newCommands: Seq[Command]): State = s.copy(processors = (s.processors ++ newCommands).distinct)
def ++ (newCommands: Seq[Command]): State = s.copy(definedCommands = (s.definedCommands ++ newCommands).distinct)
def + (newCommand: Command): State = this ++ (newCommand :: Nil)
def baseDir: File = s.configuration.baseDirectory
def setNext(n: Next.Value) = s.copy(next = n)
def continue = setNext(Next.Continue)
def reboot(full: Boolean) = throw new xsbti.FullReload(s.commands.toArray, full)
def reboot(full: Boolean) = throw new xsbti.FullReload(s.remainingCommands.toArray, full)
def reload = setNext(Next.Reload)
def exit(ok: Boolean) = setNext(if(ok) Next.Done else Next.Fail)
def get[T](key: AttributeKey[T]) = s.attributes.get(key)
def put[T](key: AttributeKey[T], value: T) = s.copy(attributes = s.attributes.put(key, value))
def fail =
{
val remaining = s.commands.dropWhile(_ != FailureWall)
val remaining = s.remainingCommands.dropWhile(_ != FailureWall)
if(remaining.isEmpty)
applyOnFailure(s, Nil, exit(ok = false))
else
applyOnFailure(s, remaining, s.copy(commands = remaining))
applyOnFailure(s, remaining, s.copy(remainingCommands = remaining))
}
private[this] def applyOnFailure(s: State, remaining: Seq[String], noHandler: => State): State =
s.onFailure match
{
case Some(c) => s.copy(commands = c +: remaining, onFailure = None)
case Some(c) => s.copy(remainingCommands = c +: remaining, onFailure = None)
case None => noHandler
}