/
ControlAction.scala
102 lines (90 loc) · 3.26 KB
/
ControlAction.scala
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package info.kwarc.mmt.api.frontend.actions
import info.kwarc.mmt.api._
import info.kwarc.mmt.api.frontend._
import info.kwarc.mmt.api.parser.SourceRef
/** shared base class for actions controlling the shell or other actions */
sealed abstract class ControlAction extends Action
case object Clear extends ControlAction {
def apply(): Unit = {
controller.clear
}
def toParseString = "clear"
}
object ClearCompanion extends ObjectActionCompanion(Clear, "clear the current state of the controller", "clear")
/** release all resources and exit
*
* concrete syntax: exit
*/
case object Exit extends ControlAction {
def apply(): Unit = {
controller.cleanup
sys.exit()
}
def toParseString = "exit"
}
object ExitCompanion extends ObjectActionCompanion(Exit, "release all resources and exit MMT", "exit")
case object NoAction extends ControlAction {
def apply(): Unit = {}
def toParseString = "noop"
}
object NoActionCompanion extends ObjectActionCompanion(NoAction, "do nothing", "noop")
case class SetBase(base: Path) extends ControlAction {
def apply() =controller.setBase(base)
def toParseString = s"base $base"
}
object SetBaseCompanion extends ActionCompanion("set the current base path", "base") {
import Action._
def parserActual(implicit state: ActionState) = path ^^ { p => SetBase(p) }
}
case class Navigate(p: Path) extends ControlAction {
def apply(): Unit = {controller.navigate(p)}
def toParseString = s"navigate $p"
}
object NavigateCompanion extends ActionCompanion("navigate to knowledge item", "navigate") {
import Action._
def parserActual(implicit state: ActionState) = path ^^ { p => Navigate(p) }
}
case class NavigateSource(ref: SourceRef) extends ControlAction {
def apply(): Unit = {controller.navigateSource(ref)}
def toParseString = s"navigateSource $ref"
}
object NavigateSourceCompanion extends ActionCompanion("navigate to physical location", "navigateSource") {
import Action._
def parserActual(implicit state: ActionState) = str ^^ { s => NavigateSource(SourceRef.fromString(s)) }
}
case class SuppressErrors(act: Action) extends ControlAction with ActionWithErrorRecovery {
override def init(c: Controller): Unit = {
super.init(c)
act.init(c)
}
def apply(errorCont: Option[ErrorHandler]) = {
act match {
case a: ActionWithErrorRecovery =>
val onlyFatalErrors = errorCont.map(ec => new HandlerWithTreshold(ec, Level.Fatal))
a(onlyFatalErrors)
case a => a()
}
}
def toParseString = s"suppressErrors " + act.toParseString
}
object SuppressErrorsCompanion extends ActionCompanion("run an action without reporting errors", "suppressErrors") {
import Action._
def parserActual(implicit state: ActionState) = action ^^ { a => SuppressErrors(a) }
}
/** Implements handling of [[ControlAction]]s */
trait ControlActionHandling {
self: Controller =>
/** set the base path of this [[Controller]] */
def setBase(base: Path): Unit = {
state.nsMap = state.nsMap(base)
report("response", "base: " + getBase)
}
/** navigates to a given path, handling [[Navigate]] */
def navigate(p: Path): Unit = {
notifyListeners.onNavigate(p)
}
/** navigates to a given path, handling [[Navigate]] */
def navigateSource(r: SourceRef): Unit = {
notifyListeners.onNavigateSource(r)
}
}