src/Tools/jEdit/src/structure_matching.scala
author wenzelm
Tue Oct 21 20:45:05 2014 +0200 (2014-10-21)
changeset 58754 0232d43422d6
parent 58752 2077bc9558cf
child 58755 fc822ca2428a
permissions -rw-r--r--
tuned;
wenzelm@58748
     1
/*  Title:      Tools/jEdit/src/structure_matching.scala
wenzelm@58748
     2
    Author:     Makarius
wenzelm@58748
     3
wenzelm@58748
     4
Structure matcher for Isabelle/Isar outer syntax.
wenzelm@58748
     5
*/
wenzelm@58748
     6
wenzelm@58748
     7
package isabelle.jedit
wenzelm@58748
     8
wenzelm@58748
     9
wenzelm@58748
    10
import isabelle._
wenzelm@58748
    11
wenzelm@58748
    12
import org.gjt.sp.jedit.textarea.{TextArea, StructureMatcher}
wenzelm@58748
    13
wenzelm@58748
    14
wenzelm@58748
    15
object Structure_Matching
wenzelm@58748
    16
{
wenzelm@58748
    17
  object Isabelle_Matcher extends StructureMatcher
wenzelm@58748
    18
  {
wenzelm@58754
    19
    def find_block(
wenzelm@58752
    20
      open: Token => Boolean,
wenzelm@58752
    21
      close: Token => Boolean,
wenzelm@58752
    22
      reset: Token => Boolean,
wenzelm@58754
    23
      it: Iterator[Text.Info[Token]]): Option[(Text.Range, Text.Range)] =
wenzelm@58752
    24
    {
wenzelm@58754
    25
      val range1 = it.next.range
wenzelm@58754
    26
      it.scanLeft((range1, 1))(
wenzelm@58752
    27
        { case ((r, d), Text.Info(range, tok)) =>
wenzelm@58752
    28
            if (open(tok)) (range, d + 1)
wenzelm@58752
    29
            else if (close(tok)) (range, d - 1)
wenzelm@58752
    30
            else if (reset(tok)) (range, 0)
wenzelm@58752
    31
            else (r, d) }
wenzelm@58754
    32
      ).collectFirst({ case (range2, 0) => (range1, range2) })
wenzelm@58752
    33
    }
wenzelm@58752
    34
wenzelm@58749
    35
    def find_pair(text_area: TextArea): Option[(Text.Range, Text.Range)] =
wenzelm@58748
    36
    {
wenzelm@58748
    37
      val buffer = text_area.getBuffer
wenzelm@58748
    38
      val caret_line = text_area.getCaretLine
wenzelm@58749
    39
      val caret = text_area.getCaretPosition
wenzelm@58748
    40
wenzelm@58748
    41
      PIDE.session.recent_syntax match {
wenzelm@58750
    42
        case syntax: Outer_Syntax
wenzelm@58750
    43
        if syntax != Outer_Syntax.empty =>
wenzelm@58750
    44
wenzelm@58750
    45
          val limit = PIDE.options.value.int("jedit_structure_limit") max 0
wenzelm@58750
    46
wenzelm@58750
    47
          def iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
wenzelm@58750
    48
            Token_Markup.line_token_iterator(syntax, buffer, line, line + lim)
wenzelm@58750
    49
wenzelm@58750
    50
          def rev_iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
wenzelm@58750
    51
            Token_Markup.line_token_reverse_iterator(syntax, buffer, line, line - lim)
wenzelm@58750
    52
wenzelm@58752
    53
          def caret_iterator(): Iterator[Text.Info[Token]] =
wenzelm@58752
    54
            iterator(caret_line).dropWhile(info => !info.range.touches(caret))
wenzelm@58752
    55
wenzelm@58752
    56
          def rev_caret_iterator(): Iterator[Text.Info[Token]] =
wenzelm@58752
    57
            rev_iterator(caret_line).dropWhile(info => !info.range.touches(caret))
wenzelm@58752
    58
wenzelm@58752
    59
          iterator(caret_line, 1).find(info => info.range.touches(caret)) match
wenzelm@58752
    60
          {
wenzelm@58750
    61
            case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.qed_global) =>
wenzelm@58752
    62
              rev_caret_iterator().find(info => syntax.command_kind(info.info, Keyword.theory))
wenzelm@58749
    63
              match {
wenzelm@58750
    64
                case Some(Text.Info(range2, tok))
wenzelm@58750
    65
                if syntax.command_kind(tok, Keyword.theory_goal) => Some((range1, range2))
wenzelm@58749
    66
                case _ => None
wenzelm@58749
    67
              }
wenzelm@58752
    68
wenzelm@58752
    69
            case Some(Text.Info(range1, tok)) if tok.is_begin =>
wenzelm@58754
    70
              find_block(_.is_begin, _.is_end, _ => false, caret_iterator())
wenzelm@58752
    71
wenzelm@58752
    72
            case Some(Text.Info(range1, tok)) if tok.is_end =>
wenzelm@58754
    73
              find_block(_.is_end, _.is_begin, _ => false, rev_caret_iterator())
wenzelm@58752
    74
wenzelm@58749
    75
            case _ => None
wenzelm@58748
    76
          }
wenzelm@58749
    77
        case _ => None
wenzelm@58748
    78
      }
wenzelm@58748
    79
    }
wenzelm@58748
    80
wenzelm@58749
    81
    def getMatch(text_area: TextArea): StructureMatcher.Match =
wenzelm@58749
    82
      find_pair(text_area) match {
wenzelm@58749
    83
        case Some((_, range)) =>
wenzelm@58749
    84
          val line = text_area.getBuffer.getLineOfOffset(range.start)
wenzelm@58749
    85
          new StructureMatcher.Match(Structure_Matching.Isabelle_Matcher,
wenzelm@58749
    86
            line, range.start, line, range.stop)
wenzelm@58749
    87
        case None => null
wenzelm@58749
    88
      }
wenzelm@58749
    89
wenzelm@58748
    90
    def selectMatch(text_area: TextArea)
wenzelm@58748
    91
    {
wenzelm@58748
    92
      // FIXME
wenzelm@58748
    93
    }
wenzelm@58748
    94
  }
wenzelm@58748
    95
}
wenzelm@58748
    96