src/Tools/jEdit/src/jedit/TheoryView.scala
author wenzelm
Sun Dec 28 18:40:38 2008 +0100 (2008-12-28)
changeset 34446 5c79f97ec1d1
parent 34445 61ffe9a35f1d
child 34447 dc8b486fde9f
permissions -rw-r--r--
superficial tuning;
improved comments and sectioning;
wenzelm@34407
     1
/*
wenzelm@34408
     2
 * jEdit text area as document text source
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Fabian Immler, TU Munich
wenzelm@34407
     5
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34318
     8
package isabelle.jedit
wenzelm@34318
     9
wenzelm@34446
    10
wenzelm@34318
    11
import isabelle.utils.EventSource
wenzelm@34318
    12
import isabelle.proofdocument.Text
wenzelm@34446
    13
import isabelle.prover.{Prover, Command, CommandChangeInfo}
wenzelm@34318
    14
import isabelle.prover.Command.Phase
wenzelm@34318
    15
wenzelm@34318
    16
import javax.swing.Timer
wenzelm@34446
    17
import javax.swing.event.{CaretListener, CaretEvent}
wenzelm@34318
    18
import java.awt.Graphics2D
wenzelm@34446
    19
import java.awt.event.{ActionEvent, ActionListener}
wenzelm@34446
    20
import java.awt.Color
wenzelm@34318
    21
wenzelm@34446
    22
import org.gjt.sp.jedit.buffer.{BufferListener, JEditBuffer}
wenzelm@34446
    23
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextAreaExtension, TextAreaPainter}
wenzelm@34318
    24
import org.gjt.sp.jedit.syntax.SyntaxStyle
wenzelm@34318
    25
wenzelm@34446
    26
wenzelm@34318
    27
object TheoryView {
wenzelm@34318
    28
wenzelm@34446
    29
  def choose_color(state: Command): Color = {
wenzelm@34318
    30
    if (state == null)
wenzelm@34318
    31
      Color.red
wenzelm@34318
    32
    else
wenzelm@34318
    33
      state.phase match {
wenzelm@34318
    34
        case Phase.UNPROCESSED => new Color(255, 255, 192)
wenzelm@34318
    35
        case Phase.FINISHED => new Color(192, 255, 192)
wenzelm@34318
    36
        case Phase.FAILED => new Color(255, 192, 192)
wenzelm@34318
    37
        case _ => Color.red
wenzelm@34318
    38
      }
wenzelm@34318
    39
  }
immler@34391
    40
wenzelm@34446
    41
  def choose_color(markup: String): Color = {
wenzelm@34446
    42
    // TODO: as properties
wenzelm@34446
    43
    markup match {
wenzelm@34445
    44
      // logical entities
wenzelm@34445
    45
      case Markup.TCLASS | Markup.TYCON | Markup.FIXED_DECL | Markup.FIXED | Markup.CONST_DECL
wenzelm@34445
    46
        | Markup.CONST | Markup.FACT_DECL | Markup.FACT | Markup.DYNAMIC_FACT
wenzelm@34445
    47
        | Markup.LOCAL_FACT_DECL | Markup.LOCAL_FACT => new Color(255, 0, 255)
wenzelm@34445
    48
      // inner syntax
wenzelm@34445
    49
      case Markup.TFREE | Markup.FREE => Color.blue
wenzelm@34445
    50
      case Markup.TVAR | Markup.SKOLEM | Markup.BOUND | Markup.VAR => Color.green
wenzelm@34445
    51
      case Markup.NUM | Markup.FLOAT | Markup.XNUM | Markup.XSTR | Markup.LITERAL
wenzelm@34445
    52
        | Markup.INNER_COMMENT => new Color(255, 128, 128)
wenzelm@34445
    53
      case Markup.SORT | Markup.TYP | Markup.TERM | Markup.PROP
wenzelm@34446
    54
        | Markup.ATTRIBUTE | Markup.METHOD => Color.yellow
wenzelm@34445
    55
      // embedded source text
wenzelm@34445
    56
      case Markup.ML_SOURCE | Markup.DOC_SOURCE | Markup.ANTIQ | Markup.ML_ANTIQ
wenzelm@34445
    57
        | Markup.DOC_ANTIQ => new Color(0, 192, 0)
wenzelm@34445
    58
      // outer syntax
wenzelm@34445
    59
      case Markup.IDENT | Markup.COMMAND | Markup.KEYWORD => Color.blue
wenzelm@34445
    60
      case Markup.VERBATIM => Color.green
wenzelm@34445
    61
      case Markup.COMMENT => Color.gray
wenzelm@34445
    62
      case Markup.CONTROL => Color.white
wenzelm@34445
    63
      case Markup.MALFORMED => Color.red
wenzelm@34445
    64
      case Markup.STRING | Markup.ALTSTRING => Color.orange
wenzelm@34445
    65
      // other
immler@34399
    66
      case _ => Color.white
immler@34391
    67
    }
immler@34391
    68
  }
wenzelm@34318
    69
}
wenzelm@34318
    70
wenzelm@34446
    71
wenzelm@34446
    72
class TheoryView (text_area: JEditTextArea)
wenzelm@34318
    73
    extends TextAreaExtension with Text with BufferListener {
immler@34406
    74
wenzelm@34446
    75
  private val buffer = text_area.getBuffer
immler@34406
    76
  buffer.addBufferListener(this)
wenzelm@34446
    77
wenzelm@34446
    78
wenzelm@34446
    79
  private var col: Text.Changed = null
wenzelm@34446
    80
wenzelm@34446
    81
  private val col_timer = new Timer(300, new ActionListener() {
wenzelm@34446
    82
    override def actionPerformed(e: ActionEvent) = commit()
wenzelm@34318
    83
  })
immler@34406
    84
wenzelm@34446
    85
  col_timer.stop
wenzelm@34446
    86
  col_timer.setRepeats(true)
wenzelm@34446
    87
wenzelm@34446
    88
wenzelm@34446
    89
  private val changes_source = new EventSource[Text.Changed]
wenzelm@34446
    90
  private val phase_overview = new PhaseOverviewPanel(Isabelle.prover(buffer))
wenzelm@34446
    91
wenzelm@34318
    92
wenzelm@34446
    93
  /* activation */
wenzelm@34446
    94
wenzelm@34446
    95
  Isabelle.plugin.font_changed.add(font => update_font())
wenzelm@34446
    96
wenzelm@34446
    97
  private def update_font() = {
immler@34406
    98
    if (text_area != null) {
wenzelm@34440
    99
      if (Isabelle.plugin.font != null) {
wenzelm@34446
   100
        val painter = text_area.getPainter
wenzelm@34434
   101
        painter.setStyles(painter.getStyles.map(style =>
wenzelm@34446
   102
          new SyntaxStyle(style.getForegroundColor, style.getBackgroundColor, Isabelle.plugin.font)
wenzelm@34318
   103
        ))
wenzelm@34440
   104
        painter.setFont(Isabelle.plugin.font)
wenzelm@34446
   105
        repaint_all()
wenzelm@34318
   106
      }
wenzelm@34318
   107
    }
wenzelm@34318
   108
  }
wenzelm@34446
   109
wenzelm@34446
   110
  private val selected_state_controller = new CaretListener {
wenzelm@34446
   111
    override def caretUpdate(e: CaretEvent) = {
wenzelm@34446
   112
      val cmd = Isabelle.prover(buffer).document.getNextCommandContaining(e.getDot)
wenzelm@34446
   113
      if (cmd != null && cmd.start <= e.getDot &&
wenzelm@34446
   114
          Isabelle.prover_setup(buffer).selectedState != cmd)
wenzelm@34440
   115
        Isabelle.prover_setup(buffer).selectedState = cmd
wenzelm@34318
   116
    }
wenzelm@34318
   117
  }
wenzelm@34318
   118
wenzelm@34446
   119
  def activate() = {
wenzelm@34446
   120
    text_area.addCaretListener(selected_state_controller)
wenzelm@34446
   121
    phase_overview.textarea = text_area
wenzelm@34446
   122
    text_area.addLeftOfScrollBar(phase_overview)
wenzelm@34446
   123
    text_area.getPainter.addExtension(TextAreaPainter.LINE_BACKGROUND_LAYER + 1, this)
wenzelm@34446
   124
    update_font()
wenzelm@34446
   125
  }
wenzelm@34446
   126
wenzelm@34446
   127
  def deactivate() = {
wenzelm@34446
   128
    text_area.getPainter.removeExtension(this)
wenzelm@34446
   129
    text_area.removeLeftOfScrollBar(phase_overview)
wenzelm@34446
   130
    text_area.removeCaretListener(selected_state_controller)
wenzelm@34446
   131
  }
wenzelm@34446
   132
wenzelm@34446
   133
wenzelm@34446
   134
  /* painting */
wenzelm@34446
   135
wenzelm@34446
   136
  val repaint_delay = new isabelle.utils.Delay(100, () => repaint_all())
wenzelm@34446
   137
  Isabelle.prover(buffer).commandInfo.add(_ => repaint_delay.delay_or_ignore())
wenzelm@34446
   138
wenzelm@34446
   139
  private def from_current(pos: Int) =
wenzelm@34318
   140
    if (col != null && col.start <= pos)
wenzelm@34318
   141
      if (pos < col.start + col.added) col.start
wenzelm@34318
   142
      else pos - col.added + col.removed
wenzelm@34318
   143
    else pos
wenzelm@34446
   144
wenzelm@34446
   145
  private def to_current(pos : Int) =
wenzelm@34318
   146
    if (col != null && col.start <= pos)
wenzelm@34318
   147
      if (pos < col.start + col.removed) col.start
wenzelm@34318
   148
      else pos + col.added - col.removed
wenzelm@34318
   149
    else pos
wenzelm@34446
   150
wenzelm@34446
   151
  def repaint(cmd: Command) =
wenzelm@34318
   152
  {
wenzelm@34446
   153
    val phase = cmd.phase
wenzelm@34446
   154
    if (text_area != null && phase != Phase.REMOVE && phase != Phase.REMOVED) {
wenzelm@34446
   155
      val start = text_area.getLineOfOffset(to_current(cmd.start))
wenzelm@34446
   156
      val stop = text_area.getLineOfOffset(to_current(cmd.stop) - 1)
immler@34406
   157
      text_area.invalidateLineRange(start, stop)
wenzelm@34446
   158
wenzelm@34440
   159
      if (Isabelle.prover_setup(buffer).selectedState == cmd)
wenzelm@34446
   160
        Isabelle.prover_setup(buffer).selectedState = cmd  // update State view
wenzelm@34318
   161
    }
wenzelm@34318
   162
  }
wenzelm@34446
   163
wenzelm@34446
   164
  def repaint_all() =
wenzelm@34318
   165
  {
immler@34406
   166
    if (text_area != null)
wenzelm@34446
   167
      text_area.invalidateLineRange(text_area.getFirstPhysicalLine, text_area.getLastPhysicalLine)
wenzelm@34318
   168
  }
immler@34391
   169
wenzelm@34446
   170
  def encolor(gfx: Graphics2D,
wenzelm@34446
   171
    y: Int, height: Int, begin: Int, finish: Int, color: Color, fill: Boolean) =
wenzelm@34446
   172
  {
wenzelm@34446
   173
    val start = text_area.offsetToXY(begin)
wenzelm@34446
   174
    val stop =
wenzelm@34446
   175
      if (finish < buffer.getLength) text_area.offsetToXY(finish)
wenzelm@34446
   176
      else {
wenzelm@34446
   177
        val p = text_area.offsetToXY(finish - 1)
wenzelm@34446
   178
        p.x = p.x + text_area.getPainter.getFontMetrics.charWidth(' ')
wenzelm@34446
   179
        p
immler@34391
   180
      }
wenzelm@34446
   181
wenzelm@34446
   182
    if (start != null && stop != null) {
wenzelm@34446
   183
      gfx.setColor(color)
wenzelm@34446
   184
      if (fill) gfx.fillRect(start.x, y, stop.x - start.x, height)
wenzelm@34446
   185
      else gfx.drawRect(start.x, y, stop.x - start.x, height)
wenzelm@34446
   186
    }
immler@34391
   187
  }
immler@34391
   188
wenzelm@34446
   189
wenzelm@34446
   190
  /* TextAreaExtension methods */
wenzelm@34446
   191
wenzelm@34446
   192
  override def paintValidLine(gfx: Graphics2D,
wenzelm@34446
   193
    screen_line: Int, pl: Int, start: Int, end: Int, y: Int) =
wenzelm@34446
   194
  {
wenzelm@34446
   195
    val saved_color = gfx.getColor
wenzelm@34446
   196
wenzelm@34446
   197
    val metrics = text_area.getPainter.getFontMetrics
wenzelm@34446
   198
    var e = Isabelle.prover(buffer).document.getNextCommandContaining(from_current(start))
immler@34391
   199
wenzelm@34446
   200
    // encolor phase
wenzelm@34446
   201
    while (e != null && to_current(e.start) < end) {
wenzelm@34446
   202
      val begin = start max to_current(e.start)
wenzelm@34446
   203
      val finish = end - 1 min to_current(e.stop)
wenzelm@34446
   204
      encolor(gfx, y, metrics.getHeight, begin, finish, TheoryView.choose_color(e), true)
wenzelm@34446
   205
immler@34391
   206
      // paint details of command
wenzelm@34446
   207
      for (node <- e.root_node.dfs) {
wenzelm@34446
   208
        val begin = to_current(node.start + e.start)
wenzelm@34446
   209
        val finish = to_current(node.end + e.start)
wenzelm@34446
   210
        if (finish > start && begin < end) {
wenzelm@34446
   211
          encolor(gfx, y + metrics.getHeight - 4, 2, begin max start, finish min end,
wenzelm@34446
   212
            TheoryView.choose_color(node.short), true)
wenzelm@34446
   213
        }
wenzelm@34318
   214
      }
wenzelm@34318
   215
      e = e.next
wenzelm@34318
   216
    }
immler@34391
   217
wenzelm@34446
   218
    gfx.setColor(saved_color)
wenzelm@34318
   219
  }
wenzelm@34446
   220
wenzelm@34446
   221
wenzelm@34446
   222
  /* Text methods */
wenzelm@34318
   223
wenzelm@34446
   224
  def content(start: Int, stop: Int) = buffer.getText(start, stop - start)
wenzelm@34446
   225
  def length = buffer.getLength
wenzelm@34446
   226
  def changes = changes_source
wenzelm@34446
   227
wenzelm@34446
   228
wenzelm@34446
   229
wenzelm@34446
   230
  /* BufferListener methods */
wenzelm@34318
   231
wenzelm@34318
   232
  private def commit() {
wenzelm@34318
   233
    if (col != null)
wenzelm@34318
   234
      changes.fire(col)
wenzelm@34318
   235
    col = null
wenzelm@34446
   236
    if (col_timer.isRunning())
wenzelm@34446
   237
      col_timer.stop()
wenzelm@34446
   238
  }
wenzelm@34446
   239
wenzelm@34446
   240
  private def delay_commit() {
wenzelm@34446
   241
    if (col_timer.isRunning())
wenzelm@34446
   242
      col_timer.restart()
wenzelm@34318
   243
    else
wenzelm@34446
   244
      col_timer.start()
wenzelm@34318
   245
  }
wenzelm@34446
   246
wenzelm@34446
   247
wenzelm@34446
   248
  override def contentInserted(buffer: JEditBuffer,
wenzelm@34446
   249
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
wenzelm@34446
   250
wenzelm@34446
   251
  override def contentRemoved(buffer: JEditBuffer,
wenzelm@34446
   252
    start_line: Int, offset: Int, num_lines: Int, length: Int) { }
wenzelm@34318
   253
wenzelm@34446
   254
  override def preContentInserted(buffer: JEditBuffer,
wenzelm@34446
   255
    start_line: Int, offset: Int, num_lines: Int, length: Int) =
wenzelm@34446
   256
  {
immler@34364
   257
    if (col == null)
wenzelm@34446
   258
      col = new Text.Changed(offset, length, 0)
wenzelm@34446
   259
    else if (col.start <= offset && offset <= col.start + col.added)
wenzelm@34446
   260
      col = new Text.Changed(col.start, col.added + length, col.removed)
wenzelm@34446
   261
    else {
wenzelm@34318
   262
      commit()
wenzelm@34446
   263
      col = new Text.Changed(offset, length, 0)
wenzelm@34318
   264
    }
wenzelm@34446
   265
    delay_commit()
wenzelm@34446
   266
  }
wenzelm@34446
   267
wenzelm@34446
   268
  override def preContentRemoved(buffer: JEditBuffer,
wenzelm@34446
   269
    start_line: Int, start: Int, num_lines: Int, removed: Int) =
wenzelm@34446
   270
  {
wenzelm@34318
   271
    if (col == null)
wenzelm@34446
   272
      col = new Text.Changed(start, 0, removed)
wenzelm@34446
   273
    else if (col.start > start + removed || start > col.start + col.added) {
wenzelm@34318
   274
      commit()
wenzelm@34446
   275
      col = new Text.Changed(start, 0, removed)
wenzelm@34318
   276
    }
wenzelm@34318
   277
    else {
wenzelm@34318
   278
      val offset = start - col.start
wenzelm@34318
   279
      val diff = col.added - removed
wenzelm@34446
   280
      val (added, add_removed) =
wenzelm@34446
   281
        if (diff < offset)
wenzelm@34318
   282
          (offset max 0, diff - (offset max 0))
wenzelm@34446
   283
        else
wenzelm@34318
   284
          (diff - (offset min 0), offset min 0)
wenzelm@34446
   285
      col = new Text.Changed(start min col.start, added, col.removed - add_removed)
wenzelm@34318
   286
    }
wenzelm@34446
   287
    delay_commit()
wenzelm@34318
   288
  }
wenzelm@34318
   289
wenzelm@34446
   290
  override def bufferLoaded(buffer: JEditBuffer) { }
wenzelm@34446
   291
  override def foldHandlerChanged(buffer: JEditBuffer) { }
wenzelm@34446
   292
  override def foldLevelChanged(buffer: JEditBuffer, start_line: Int, end_line: Int) { }
wenzelm@34446
   293
  override def transactionComplete(buffer: JEditBuffer) { }
wenzelm@34318
   294
}