src/Tools/jEdit/src/text_structure.scala
author wenzelm
Fri, 08 Jul 2016 22:22:51 +0200
changeset 63428 005b490f0ce2
parent 63427 88d62f8b5f6e
child 63431 8002eec44fbb
permissions -rw-r--r--
indentation in reminiscence to Proof General (see proof-indent.el);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
     1
/*  Title:      Tools/jEdit/src/text_structure.scala
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     3
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
     4
Text structure based on Isabelle/Isar outer syntax.
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     5
*/
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     6
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     8
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
     9
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    10
import isabelle._
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    11
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    12
import org.gjt.sp.jedit.indent.{IndentRule, IndentAction}
58803
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
    13
import org.gjt.sp.jedit.textarea.{TextArea, StructureMatcher, Selection}
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    14
import org.gjt.sp.jedit.buffer.JEditBuffer
63423
ed65a6d9929b more operations;
wenzelm
parents: 63422
diff changeset
    15
import org.gjt.sp.jedit.Buffer
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    16
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    17
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    18
object Text_Structure
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
    19
{
63425
wenzelm
parents: 63424
diff changeset
    20
  /* token navigator */
wenzelm
parents: 63424
diff changeset
    21
wenzelm
parents: 63424
diff changeset
    22
  class Navigator(syntax: Outer_Syntax, buffer: Buffer)
wenzelm
parents: 63424
diff changeset
    23
  {
wenzelm
parents: 63424
diff changeset
    24
    val limit = PIDE.options.value.int("jedit_structure_limit") max 0
wenzelm
parents: 63424
diff changeset
    25
wenzelm
parents: 63424
diff changeset
    26
    def iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
wenzelm
parents: 63424
diff changeset
    27
      Token_Markup.line_token_iterator(syntax, buffer, line, line + lim).
wenzelm
parents: 63424
diff changeset
    28
        filter(_.info.is_proper)
wenzelm
parents: 63424
diff changeset
    29
63427
wenzelm
parents: 63425
diff changeset
    30
    def reverse_iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
63425
wenzelm
parents: 63424
diff changeset
    31
      Token_Markup.line_token_reverse_iterator(syntax, buffer, line, line - lim).
wenzelm
parents: 63424
diff changeset
    32
        filter(_.info.is_proper)
wenzelm
parents: 63424
diff changeset
    33
  }
wenzelm
parents: 63424
diff changeset
    34
wenzelm
parents: 63424
diff changeset
    35
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    36
  /* indentation */
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    37
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    38
  object Indent_Rule extends IndentRule
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    39
  {
63428
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    40
    private val keyword_open = Keyword.theory_goal ++ Keyword.proof_open
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    41
    private val keyword_close = Keyword.proof_close
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    42
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    43
    def apply(buffer: JEditBuffer, current_line: Int, prev_line: Int, prev_prev_line: Int,
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    44
      actions: java.util.List[IndentAction])
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    45
    {
63425
wenzelm
parents: 63424
diff changeset
    46
      Isabelle.buffer_syntax(buffer) match {
wenzelm
parents: 63424
diff changeset
    47
        case Some(syntax) if buffer.isInstanceOf[Buffer] =>
wenzelm
parents: 63424
diff changeset
    48
          val keywords = syntax.keywords
wenzelm
parents: 63424
diff changeset
    49
          val nav = new Navigator(syntax, buffer.asInstanceOf[Buffer])
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
    50
63428
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    51
          def head_token(line: Int): Option[Token] =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    52
            nav.iterator(line, 1).toStream.headOption.map(_.info)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    53
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    54
          def prev_command: Option[Token] =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    55
            nav.reverse_iterator(prev_line, 1).
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    56
              collectFirst({ case Text.Info(_, tok) if tok.is_command => tok })
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    57
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    58
          def line_indent(line: Int): Int =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    59
            if (line < 0 || line >= buffer.getLineCount) 0
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    60
            else buffer.getCurrentIndentForLine(line, null)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    61
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    62
          val indent_size = buffer.getIndentSize
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    63
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    64
          def indent_indent(tok: Token): Int =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    65
            if (keywords.is_command(tok, keyword_open)) indent_size
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    66
            else if (keywords.is_command(tok, keyword_close)) - indent_size
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    67
            else 0
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    68
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    69
          def indent_offset(tok: Token): Int =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    70
            if (keywords.is_command(tok, Keyword.proof_enclose)) indent_size
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    71
            else 0
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    72
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    73
          def indent_structure: Int =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    74
            nav.reverse_iterator(current_line - 1).scanLeft((0, false))(
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    75
              { case ((ind, _), Text.Info(range, tok)) =>
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    76
                  val ind1 = ind + indent_indent(tok)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    77
                  if (tok.is_command) {
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    78
                    val line = buffer.getLineOfOffset(range.start)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    79
                    if (head_token(line) == Some(tok))
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    80
                      (ind1 + indent_offset(tok) + line_indent(line), true)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    81
                    else (ind1, false)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    82
                  }
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    83
                  else (ind1, false)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    84
              }).collectFirst({ case (i, true) => i }).getOrElse(0)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    85
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    86
          val indent =
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    87
            head_token(current_line) match {
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    88
              case Some(tok) if tok.is_command =>
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    89
                if (keywords.is_command(tok, Keyword.theory)) 0
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    90
                else indent_structure - indent_offset(tok)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    91
              case _ =>
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    92
                if (nav.iterator(current_line, 1).isEmpty) indent_structure
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    93
                else
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    94
                  prev_command match {
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    95
                    case Some(tok) =>
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    96
                      indent_structure - indent_offset(tok) - indent_indent(tok) + indent_size
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    97
                    case None => line_indent(prev_line)
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    98
                  }
005b490f0ce2 indentation in reminiscence to Proof General (see proof-indent.el);
wenzelm
parents: 63427
diff changeset
    99
            }
63423
ed65a6d9929b more operations;
wenzelm
parents: 63422
diff changeset
   100
63425
wenzelm
parents: 63424
diff changeset
   101
          actions.clear()
wenzelm
parents: 63424
diff changeset
   102
          actions.add(new IndentAction.AlignOffset(indent))
63423
ed65a6d9929b more operations;
wenzelm
parents: 63422
diff changeset
   103
        case _ =>
ed65a6d9929b more operations;
wenzelm
parents: 63422
diff changeset
   104
      }
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   105
    }
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   106
  }
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   107
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   108
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   109
  /* structure matching */
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   110
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   111
  object Matcher extends StructureMatcher
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   112
  {
58803
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   113
    private def find_block(
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   114
      open: Token => Boolean,
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   115
      close: Token => Boolean,
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   116
      reset: Token => Boolean,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   117
      restrict: Token => Boolean,
58754
wenzelm
parents: 58752
diff changeset
   118
      it: Iterator[Text.Info[Token]]): Option[(Text.Range, Text.Range)] =
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   119
    {
58754
wenzelm
parents: 58752
diff changeset
   120
      val range1 = it.next.range
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   121
      it.takeWhile(info => !info.info.is_command || restrict(info.info)).
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   122
        scanLeft((range1, 1))(
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   123
          { case ((r, d), Text.Info(range, tok)) =>
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   124
              if (open(tok)) (range, d + 1)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   125
              else if (close(tok)) (range, d - 1)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   126
              else if (reset(tok)) (range, 0)
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   127
              else (r, d) }
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   128
        ).collectFirst({ case (range2, 0) => (range1, range2) })
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   129
    }
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   130
58803
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   131
    private def find_pair(text_area: TextArea): Option[(Text.Range, Text.Range)] =
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   132
    {
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   133
      val buffer = text_area.getBuffer
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   134
      val caret_line = text_area.getCaretLine
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   135
      val caret = text_area.getCaretPosition
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   136
59074
7836d927ffca tuned signature;
wenzelm
parents: 58901
diff changeset
   137
      Isabelle.buffer_syntax(text_area.getBuffer) match {
63425
wenzelm
parents: 63424
diff changeset
   138
        case Some(syntax) if buffer.isInstanceOf[Buffer] =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   139
          val keywords = syntax.keywords
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
   140
63425
wenzelm
parents: 63424
diff changeset
   141
          val nav = new Navigator(syntax, buffer.asInstanceOf[Buffer])
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
   142
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   143
          def caret_iterator(): Iterator[Text.Info[Token]] =
63425
wenzelm
parents: 63424
diff changeset
   144
            nav.iterator(caret_line).dropWhile(info => !info.range.touches(caret))
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   145
63427
wenzelm
parents: 63425
diff changeset
   146
          def reverse_caret_iterator(): Iterator[Text.Info[Token]] =
wenzelm
parents: 63425
diff changeset
   147
            nav.reverse_iterator(caret_line).dropWhile(info => !info.range.touches(caret))
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   148
63425
wenzelm
parents: 63424
diff changeset
   149
          nav.iterator(caret_line, 1).find(info => info.range.touches(caret))
58756
eb5d0c58564d ignore improper tokens to avoid ambiguity of Range.touches (assuming that relevant tokens are separated properly);
wenzelm
parents: 58755
diff changeset
   150
          match {
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   151
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.theory_goal) =>
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   152
              find_block(
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   153
                keywords.is_command(_, Keyword.proof_goal),
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   154
                keywords.is_command(_, Keyword.qed),
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   155
                keywords.is_command(_, Keyword.qed_global),
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   156
                t =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   157
                  keywords.is_command(t, Keyword.diag) ||
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   158
                  keywords.is_command(t, Keyword.proof),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   159
                caret_iterator())
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   160
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   161
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.proof_goal) =>
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   162
              find_block(
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   163
                keywords.is_command(_, Keyword.proof_goal),
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   164
                keywords.is_command(_, Keyword.qed),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   165
                _ => false,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   166
                t =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   167
                  keywords.is_command(t, Keyword.diag) ||
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   168
                  keywords.is_command(t, Keyword.proof),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   169
                caret_iterator())
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   170
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   171
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.qed_global) =>
63427
wenzelm
parents: 63425
diff changeset
   172
              reverse_caret_iterator().find(info => keywords.is_command(info.info, Keyword.theory))
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   173
              match {
58750
1b4b005d73c1 added option jedit_structure_limit;
wenzelm
parents: 58749
diff changeset
   174
                case Some(Text.Info(range2, tok))
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   175
                if keywords.is_command(tok, Keyword.theory_goal) => Some((range1, range2))
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   176
                case _ => None
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   177
              }
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   178
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   179
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.qed) =>
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   180
              find_block(
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   181
                keywords.is_command(_, Keyword.qed),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   182
                t =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   183
                  keywords.is_command(t, Keyword.proof_goal) ||
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   184
                  keywords.is_command(t, Keyword.theory_goal),
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   185
                _ => false,
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   186
                t =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   187
                  keywords.is_command(t, Keyword.diag) ||
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   188
                  keywords.is_command(t, Keyword.proof) ||
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   189
                  keywords.is_command(t, Keyword.theory_goal),
63427
wenzelm
parents: 63425
diff changeset
   190
                reverse_caret_iterator())
58755
fc822ca2428a support for proof structure matching;
wenzelm
parents: 58754
diff changeset
   191
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   192
            case Some(Text.Info(range1, tok)) if tok.is_begin =>
58762
4fedc5d4b2fe restricted scanning;
wenzelm
parents: 58756
diff changeset
   193
              find_block(_.is_begin, _.is_end, _ => false, _ => true, caret_iterator())
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   194
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   195
            case Some(Text.Info(range1, tok)) if tok.is_end =>
63427
wenzelm
parents: 63425
diff changeset
   196
              find_block(_.is_end, _.is_begin, _ => false, _ => true, reverse_caret_iterator())
58763
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   197
              match {
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   198
                case Some((_, range2)) =>
63427
wenzelm
parents: 63425
diff changeset
   199
                  reverse_caret_iterator().
58800
bfed1c26caed explicit keyword category for commands that may start a block;
wenzelm
parents: 58763
diff changeset
   200
                    dropWhile(info => info.range != range2).
bfed1c26caed explicit keyword category for commands that may start a block;
wenzelm
parents: 58763
diff changeset
   201
                    dropWhile(info => info.range == range2).
bfed1c26caed explicit keyword category for commands that may start a block;
wenzelm
parents: 58763
diff changeset
   202
                    find(info => info.info.is_command || info.info.is_begin)
58763
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   203
                  match {
58800
bfed1c26caed explicit keyword category for commands that may start a block;
wenzelm
parents: 58763
diff changeset
   204
                    case Some(Text.Info(range3, tok)) =>
63424
e4e15bbfb3e2 clarified signature;
wenzelm
parents: 63423
diff changeset
   205
                      if (keywords.is_command(tok, Keyword.theory_block)) Some((range1, range3))
58800
bfed1c26caed explicit keyword category for commands that may start a block;
wenzelm
parents: 58763
diff changeset
   206
                      else Some((range1, range2))
58763
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   207
                    case None => None
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   208
                  }
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   209
                case None => None
1b943a82d5ed find main command keyword of 'begin';
wenzelm
parents: 58762
diff changeset
   210
              }
58752
2077bc9558cf support for begin/end matching;
wenzelm
parents: 58750
diff changeset
   211
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   212
            case _ => None
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   213
          }
63425
wenzelm
parents: 63424
diff changeset
   214
        case _ => None
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   215
      }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   216
    }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   217
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   218
    def getMatch(text_area: TextArea): StructureMatcher.Match =
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   219
      find_pair(text_area) match {
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   220
        case Some((_, range)) =>
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   221
          val line = text_area.getBuffer.getLineOfOffset(range.start)
63422
5cf8dd98a717 clarified modules;
wenzelm
parents: 59122
diff changeset
   222
          new StructureMatcher.Match(Matcher, line, range.start, line, range.stop)
58749
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   223
        case None => null
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   224
      }
83b0f633190e some structure matching, based on line token iterators;
wenzelm
parents: 58748
diff changeset
   225
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   226
    def selectMatch(text_area: TextArea)
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   227
    {
58803
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   228
      def get_span(offset: Text.Offset): Option[Text.Range] =
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   229
        for {
59074
7836d927ffca tuned signature;
wenzelm
parents: 58901
diff changeset
   230
          syntax <- Isabelle.buffer_syntax(text_area.getBuffer)
58803
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   231
          span <- Token_Markup.command_span(syntax, text_area.getBuffer, offset)
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   232
        } yield span.range
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   233
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   234
      find_pair(text_area) match {
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   235
        case Some((r1, r2)) =>
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   236
          (get_span(r1.start), get_span(r2.start)) match {
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   237
            case (Some(range1), Some(range2)) =>
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   238
              val start = range1.start min range2.start
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   239
              val stop = range1.stop max range2.stop
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   240
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   241
              text_area.moveCaretPosition(stop, false)
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   242
              if (!text_area.isMultipleSelectionEnabled) text_area.selectNone
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   243
              text_area.addToSelection(new Selection.Range(start, stop))
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   244
            case _ =>
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   245
          }
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   246
        case None =>
7a0f675eb671 proper selectMatch, e.g. relevant for S-click on gutter;
wenzelm
parents: 58800
diff changeset
   247
      }
58748
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   248
    }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   249
  }
8f92f17d8781 support for structure matching;
wenzelm
parents:
diff changeset
   250
}