src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Fri Oct 11 20:45:21 2013 +0200 (2013-10-11)
changeset 54325 2c4155003352
parent 53845 08721d7b2262
child 54461 6c360a6ade0e
permissions -rw-r--r--
clarified Editor.current_command: allow outdated snapshot;
more accurate Document_View.perspective based on current_command for proper state output (see also 88c6e630c15f and ef62204a126b);
wenzelm@52971
     1
/*  Title:      Tools/jEdit/src/jedit_editor.scala
wenzelm@52971
     2
    Author:     Makarius
wenzelm@52971
     3
wenzelm@52971
     4
PIDE editor operations for jEdit.
wenzelm@52971
     5
*/
wenzelm@52971
     6
wenzelm@52971
     7
package isabelle.jedit
wenzelm@52971
     8
wenzelm@52971
     9
wenzelm@52971
    10
import isabelle._
wenzelm@52971
    11
wenzelm@52971
    12
wenzelm@52971
    13
import org.gjt.sp.jedit.{jEdit, View}
wenzelm@52971
    14
wenzelm@52971
    15
wenzelm@52971
    16
class JEdit_Editor extends Editor[View]
wenzelm@52971
    17
{
wenzelm@52977
    18
  /* session */
wenzelm@52977
    19
wenzelm@52980
    20
  override def session: Session = PIDE.session
wenzelm@52971
    21
wenzelm@52980
    22
  override def flush()
wenzelm@52974
    23
  {
wenzelm@52974
    24
    Swing_Thread.require()
wenzelm@52974
    25
wenzelm@52974
    26
    session.update(
wenzelm@52974
    27
      (List.empty[Document.Edit_Text] /: JEdit_Lib.jedit_buffers().toList) {
wenzelm@52974
    28
        case (edits, buffer) =>
wenzelm@52974
    29
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@52974
    30
            PIDE.document_model(buffer) match {
wenzelm@52974
    31
              case Some(model) => model.flushed_edits() ::: edits
wenzelm@52974
    32
              case None => edits
wenzelm@52974
    33
            }
wenzelm@52974
    34
          }
wenzelm@52974
    35
      }
wenzelm@52974
    36
    )
wenzelm@52974
    37
  }
wenzelm@52974
    38
wenzelm@52977
    39
wenzelm@52977
    40
  /* current situation */
wenzelm@52977
    41
wenzelm@52980
    42
  override def current_context: View =
wenzelm@52971
    43
    Swing_Thread.require { jEdit.getActiveView() }
wenzelm@52971
    44
wenzelm@52980
    45
  override def current_node(view: View): Option[Document.Node.Name] =
wenzelm@52973
    46
    Swing_Thread.require { PIDE.document_model(view.getBuffer).map(_.node_name) }
wenzelm@52971
    47
wenzelm@52980
    48
  override def current_node_snapshot(view: View): Option[Document.Snapshot] =
wenzelm@52971
    49
    Swing_Thread.require { PIDE.document_model(view.getBuffer).map(_.snapshot()) }
wenzelm@52971
    50
wenzelm@52980
    51
  override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
wenzelm@52974
    52
  {
wenzelm@52974
    53
    Swing_Thread.require()
wenzelm@52974
    54
wenzelm@52974
    55
    JEdit_Lib.jedit_buffer(name.node) match {
wenzelm@52974
    56
      case Some(buffer) =>
wenzelm@52974
    57
        PIDE.document_model(buffer) match {
wenzelm@52974
    58
          case Some(model) => model.snapshot
wenzelm@52974
    59
          case None => session.snapshot(name)
wenzelm@52974
    60
        }
wenzelm@52974
    61
      case None => session.snapshot(name)
wenzelm@52974
    62
    }
wenzelm@52974
    63
  }
wenzelm@52974
    64
wenzelm@53844
    65
  override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] =
wenzelm@52971
    66
  {
wenzelm@52971
    67
    Swing_Thread.require()
wenzelm@52971
    68
wenzelm@54325
    69
    val text_area = view.getTextArea
wenzelm@54325
    70
    PIDE.document_view(text_area) match {
wenzelm@54325
    71
      case Some(doc_view) =>
wenzelm@54325
    72
        val node = snapshot.version.nodes(doc_view.model.node_name)
wenzelm@54325
    73
        val caret = snapshot.revert(text_area.getCaretPosition)
wenzelm@54325
    74
        if (caret < text_area.getBuffer.getLength) {
wenzelm@54325
    75
          val caret_commands = node.command_range(caret)
wenzelm@54325
    76
          if (caret_commands.hasNext) {
wenzelm@54325
    77
            val (cmd0, _) = caret_commands.next
wenzelm@54325
    78
            node.commands.reverse.iterator(cmd0).find(cmd => !cmd.is_ignored)
wenzelm@53844
    79
          }
wenzelm@54325
    80
          else None
wenzelm@54325
    81
        }
wenzelm@54325
    82
        else node.commands.reverse.iterator.find(cmd => !cmd.is_ignored)
wenzelm@54325
    83
      case None => None
wenzelm@52971
    84
    }
wenzelm@52971
    85
  }
wenzelm@52977
    86
wenzelm@52977
    87
wenzelm@52977
    88
  /* overlays */
wenzelm@52977
    89
wenzelm@52977
    90
  private var overlays = Document.Overlays.empty
wenzelm@52977
    91
wenzelm@52980
    92
  override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
wenzelm@52977
    93
    synchronized { overlays(name) }
wenzelm@52977
    94
wenzelm@52980
    95
  override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
wenzelm@52977
    96
    synchronized { overlays = overlays.insert(command, fn, args) }
wenzelm@52977
    97
wenzelm@52980
    98
  override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
wenzelm@52977
    99
    synchronized { overlays = overlays.remove(command, fn, args) }
wenzelm@52980
   100
wenzelm@52980
   101
wenzelm@52980
   102
  /* hyperlinks */
wenzelm@52980
   103
wenzelm@52980
   104
  def goto(view: View, file_name: String, line: Int = 0, column: Int = 0)
wenzelm@52980
   105
  {
wenzelm@52980
   106
    Swing_Thread.require()
wenzelm@52980
   107
wenzelm@52980
   108
    JEdit_Lib.jedit_buffer(file_name) match {
wenzelm@52980
   109
      case Some(buffer) =>
wenzelm@52980
   110
        view.goToBuffer(buffer)
wenzelm@52980
   111
        val text_area = view.getTextArea
wenzelm@52980
   112
wenzelm@52980
   113
        try {
wenzelm@52980
   114
          val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
wenzelm@52980
   115
          text_area.moveCaretPosition(line_start)
wenzelm@52980
   116
          if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
wenzelm@52980
   117
        }
wenzelm@52980
   118
        catch {
wenzelm@52980
   119
          case _: ArrayIndexOutOfBoundsException =>
wenzelm@52980
   120
          case _: IllegalArgumentException =>
wenzelm@52980
   121
        }
wenzelm@52980
   122
wenzelm@52980
   123
      case None =>
wenzelm@52980
   124
        val args =
wenzelm@52980
   125
          if (line <= 0) Array(file_name)
wenzelm@52980
   126
          else if (column <= 0) Array(file_name, "+line:" + line.toInt)
wenzelm@52980
   127
          else Array(file_name, "+line:" + line.toInt + "," + column.toInt)
wenzelm@52980
   128
        jEdit.openFiles(view, null, args)
wenzelm@52980
   129
    }
wenzelm@52980
   130
  }
wenzelm@52980
   131
wenzelm@52980
   132
  override def hyperlink_file(file_name: String, line: Int = 0, column: Int = 0): Hyperlink =
wenzelm@52980
   133
    new Hyperlink { def follow(view: View) = goto(view, file_name, line, column) }
wenzelm@52980
   134
wenzelm@52980
   135
  override def hyperlink_command(snapshot: Document.Snapshot, command: Command, offset: Int = 0)
wenzelm@52980
   136
    : Option[Hyperlink] =
wenzelm@52980
   137
  {
wenzelm@52980
   138
    if (snapshot.is_outdated) None
wenzelm@52980
   139
    else {
wenzelm@52980
   140
      snapshot.state.find_command(snapshot.version, command.id) match {
wenzelm@52980
   141
        case None => None
wenzelm@52980
   142
        case Some((node, _)) =>
wenzelm@52980
   143
          val file_name = command.node_name.node
wenzelm@52980
   144
          val sources =
wenzelm@52980
   145
            node.commands.iterator.takeWhile(_ != command).map(_.source) ++
wenzelm@52980
   146
              (if (offset == 0) Iterator.empty
wenzelm@52980
   147
               else Iterator.single(command.source(Text.Range(0, command.decode(offset)))))
wenzelm@52980
   148
          val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
wenzelm@52980
   149
          Some(new Hyperlink { def follow(view: View) { goto(view, file_name, line, column) } })
wenzelm@52980
   150
      }
wenzelm@52980
   151
    }
wenzelm@52980
   152
  }
wenzelm@52971
   153
}