src/Tools/jEdit/src/text_structure.scala
author wenzelm
Mon Jan 09 20:26:59 2017 +0100 (2017-01-09)
changeset 64854 f5aa712e6250
parent 64621 7116f2634e32
child 64882 c3b42ac0cf81
permissions -rw-r--r--
tuned signature;
wenzelm@63422
     1
/*  Title:      Tools/jEdit/src/text_structure.scala
wenzelm@58748
     2
    Author:     Makarius
wenzelm@58748
     3
wenzelm@63422
     4
Text structure based on 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@63422
    12
import org.gjt.sp.jedit.indent.{IndentRule, IndentAction}
wenzelm@58803
    13
import org.gjt.sp.jedit.textarea.{TextArea, StructureMatcher, Selection}
wenzelm@63422
    14
import org.gjt.sp.jedit.buffer.JEditBuffer
wenzelm@63423
    15
import org.gjt.sp.jedit.Buffer
wenzelm@58748
    16
wenzelm@58748
    17
wenzelm@63422
    18
object Text_Structure
wenzelm@58748
    19
{
wenzelm@63425
    20
  /* token navigator */
wenzelm@63425
    21
wenzelm@63447
    22
  class Navigator(syntax: Outer_Syntax, buffer: Buffer, comments: Boolean)
wenzelm@63425
    23
  {
wenzelm@63425
    24
    val limit = PIDE.options.value.int("jedit_structure_limit") max 0
wenzelm@63425
    25
wenzelm@63425
    26
    def iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
wenzelm@63445
    27
    {
wenzelm@63445
    28
      val it = Token_Markup.line_token_iterator(syntax, buffer, line, line + lim)
wenzelm@63480
    29
      if (comments) it.filterNot(_.info.is_space) else it.filterNot(_.info.is_improper)
wenzelm@63445
    30
    }
wenzelm@63425
    31
wenzelm@63427
    32
    def reverse_iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] =
wenzelm@63445
    33
    {
wenzelm@63445
    34
      val it = Token_Markup.line_token_reverse_iterator(syntax, buffer, line, line - lim)
wenzelm@63480
    35
      if (comments) it.filterNot(_.info.is_space) else it.filterNot(_.info.is_improper)
wenzelm@63445
    36
    }
wenzelm@63425
    37
  }
wenzelm@63425
    38
wenzelm@63425
    39
wenzelm@63422
    40
  /* indentation */
wenzelm@63422
    41
wenzelm@63422
    42
  object Indent_Rule extends IndentRule
wenzelm@63422
    43
  {
wenzelm@63428
    44
    private val keyword_open = Keyword.theory_goal ++ Keyword.proof_open
wenzelm@63428
    45
    private val keyword_close = Keyword.proof_close
wenzelm@63428
    46
wenzelm@64518
    47
    def apply(buffer: JEditBuffer, current_line: Int, prev_line0: Int, prev_prev_line0: Int,
wenzelm@63422
    48
      actions: java.util.List[IndentAction])
wenzelm@63422
    49
    {
wenzelm@63425
    50
      Isabelle.buffer_syntax(buffer) match {
wenzelm@63425
    51
        case Some(syntax) if buffer.isInstanceOf[Buffer] =>
wenzelm@63425
    52
          val keywords = syntax.keywords
wenzelm@63445
    53
          val nav = new Navigator(syntax, buffer.asInstanceOf[Buffer], true)
wenzelm@63422
    54
wenzelm@63474
    55
          val indent_size = buffer.getIndentSize
wenzelm@63474
    56
wenzelm@63474
    57
wenzelm@63474
    58
          def line_indent(line: Int): Int =
wenzelm@63474
    59
            if (line < 0 || line >= buffer.getLineCount) 0
wenzelm@63474
    60
            else buffer.getCurrentIndentForLine(line, null)
wenzelm@63474
    61
wenzelm@63474
    62
          def line_head(line: Int): Option[Text.Info[Token]] =
wenzelm@63474
    63
            nav.iterator(line, 1).toStream.headOption
wenzelm@63428
    64
wenzelm@63434
    65
          def head_is_quasi_command(line: Int): Boolean =
wenzelm@63474
    66
            line_head(line) match {
wenzelm@63434
    67
              case None => false
wenzelm@63474
    68
              case Some(Text.Info(_, tok)) => keywords.is_quasi_command(tok)
wenzelm@63434
    69
            }
wenzelm@63434
    70
wenzelm@64518
    71
          val prev_line: Int =
wenzelm@64518
    72
            Range.inclusive(current_line - 1, 0, -1).find(line =>
wenzelm@64518
    73
              Token_Markup.Line_Context.prev(buffer, line).get_context == Scan.Finished &&
wenzelm@64518
    74
              !Token_Markup.Line_Context.next(buffer, line).structure.improper) getOrElse -1
wenzelm@64518
    75
wenzelm@63477
    76
          def prev_line_command: Option[Token] =
wenzelm@63428
    77
            nav.reverse_iterator(prev_line, 1).
wenzelm@63477
    78
              collectFirst({ case Text.Info(_, tok) if tok.is_begin_or_command => tok })
wenzelm@63477
    79
wenzelm@63477
    80
          def prev_line_span: Iterator[Token] =
wenzelm@63477
    81
            nav.reverse_iterator(prev_line, 1).map(_.info).takeWhile(tok => !tok.is_begin_or_command)
wenzelm@63428
    82
wenzelm@63434
    83
          def prev_span: Iterator[Token] =
wenzelm@63477
    84
            nav.reverse_iterator(prev_line).map(_.info).takeWhile(tok => !tok.is_begin_or_command)
wenzelm@63450
    85
wenzelm@63434
    86
wenzelm@63481
    87
          val script_indent: Text.Info[Token] => Int =
wenzelm@63474
    88
          {
wenzelm@64621
    89
            val opt_rendering: Option[JEdit_Rendering] =
wenzelm@63474
    90
              if (PIDE.options.value.bool("jedit_indent_script"))
wenzelm@63474
    91
                GUI_Thread.now {
wenzelm@63474
    92
                  (for {
wenzelm@63474
    93
                    text_area <- JEdit_Lib.jedit_text_areas(buffer)
wenzelm@63474
    94
                    doc_view <- PIDE.document_view(text_area)
wenzelm@63474
    95
                  } yield doc_view.get_rendering).toStream.headOption
wenzelm@63474
    96
                }
wenzelm@63474
    97
              else None
wenzelm@63474
    98
            val limit = PIDE.options.value.int("jedit_indent_script_limit")
wenzelm@63481
    99
            (info: Text.Info[Token]) =>
wenzelm@63474
   100
              opt_rendering match {
wenzelm@63481
   101
                case Some(rendering) if keywords.is_command(info.info, Keyword.prf_script) =>
wenzelm@63481
   102
                  (rendering.indentation(info.range) min limit) max 0
wenzelm@63481
   103
                case _ => 0
wenzelm@63474
   104
              }
wenzelm@63474
   105
          }
wenzelm@63428
   106
wenzelm@63428
   107
          def indent_indent(tok: Token): Int =
wenzelm@63428
   108
            if (keywords.is_command(tok, keyword_open)) indent_size
wenzelm@63428
   109
            else if (keywords.is_command(tok, keyword_close)) - indent_size
wenzelm@63428
   110
            else 0
wenzelm@63428
   111
wenzelm@63428
   112
          def indent_offset(tok: Token): Int =
wenzelm@63477
   113
            if (keywords.is_command(tok, Keyword.proof_enclose)) indent_size
wenzelm@63428
   114
            else 0
wenzelm@63428
   115
wenzelm@63428
   116
          def indent_structure: Int =
wenzelm@63428
   117
            nav.reverse_iterator(current_line - 1).scanLeft((0, false))(
wenzelm@63428
   118
              { case ((ind, _), Text.Info(range, tok)) =>
wenzelm@63428
   119
                  val ind1 = ind + indent_indent(tok)
wenzelm@63479
   120
                  if (tok.is_begin_or_command && !keywords.is_command(tok, Keyword.prf_script)) {
wenzelm@63428
   121
                    val line = buffer.getLineOfOffset(range.start)
wenzelm@63474
   122
                    line_head(line) match {
wenzelm@63474
   123
                      case Some(info) if info.info == tok =>
wenzelm@63474
   124
                        (ind1 + indent_offset(tok) + line_indent(line), true)
wenzelm@63474
   125
                      case _ => (ind1, false)
wenzelm@63474
   126
                    }
wenzelm@63428
   127
                  }
wenzelm@63428
   128
                  else (ind1, false)
wenzelm@63428
   129
              }).collectFirst({ case (i, true) => i }).getOrElse(0)
wenzelm@63428
   130
wenzelm@63480
   131
          def indent_brackets: Int =
wenzelm@63480
   132
            (0 /: prev_line_span)(
wenzelm@63480
   133
              { case (i, tok) =>
wenzelm@63480
   134
                  if (tok.is_open_bracket) i + indent_size
wenzelm@63480
   135
                  else if (tok.is_close_bracket) i - indent_size
wenzelm@63480
   136
                  else i })
wenzelm@63480
   137
wenzelm@63480
   138
          def indent_extra: Int =
wenzelm@63480
   139
            if (prev_span.exists(keywords.is_quasi_command(_))) indent_size
wenzelm@63480
   140
            else 0
wenzelm@63480
   141
wenzelm@63428
   142
          val indent =
wenzelm@64518
   143
            if (Token_Markup.Line_Context.prev(buffer, current_line).get_context == Scan.Finished) {
wenzelm@64518
   144
              line_head(current_line) match {
wenzelm@64518
   145
                case Some(info @ Text.Info(range, tok)) =>
wenzelm@64518
   146
                  if (tok.is_begin ||
wenzelm@64518
   147
                      keywords.is_before_command(tok) ||
wenzelm@64518
   148
                      keywords.is_command(tok, Keyword.theory)) 0
wenzelm@64518
   149
                  else if (keywords.is_command(tok, Keyword.proof_enclose))
wenzelm@64518
   150
                    indent_structure + script_indent(info) - indent_offset(tok)
wenzelm@64518
   151
                  else if (keywords.is_command(tok, Keyword.proof))
wenzelm@64518
   152
                    (indent_structure + script_indent(info) - indent_offset(tok)) max indent_size
wenzelm@64518
   153
                  else if (tok.is_command) indent_structure - indent_offset(tok)
wenzelm@64518
   154
                  else {
wenzelm@64518
   155
                    prev_line_command match {
wenzelm@64518
   156
                      case None =>
wenzelm@64518
   157
                        val extra =
wenzelm@64518
   158
                          (keywords.is_quasi_command(tok), head_is_quasi_command(prev_line)) match {
wenzelm@64518
   159
                            case (true, true) | (false, false) => 0
wenzelm@64518
   160
                            case (true, false) => - indent_extra
wenzelm@64518
   161
                            case (false, true) => indent_extra
wenzelm@64518
   162
                          }
wenzelm@64518
   163
                        line_indent(prev_line) + indent_brackets + extra - indent_offset(tok)
wenzelm@64518
   164
                      case Some(prev_tok) =>
wenzelm@64518
   165
                        indent_structure + indent_brackets + indent_size - indent_offset(tok) -
wenzelm@64518
   166
                        indent_offset(prev_tok) - indent_indent(prev_tok)
wenzelm@64518
   167
                    }
wenzelm@64536
   168
                  }
wenzelm@64536
   169
                case None =>
wenzelm@64536
   170
                  prev_line_command match {
wenzelm@64536
   171
                    case None =>
wenzelm@64536
   172
                      val extra = if (head_is_quasi_command(prev_line)) indent_extra else 0
wenzelm@64536
   173
                      line_indent(prev_line) + indent_brackets + extra
wenzelm@64536
   174
                    case Some(prev_tok) =>
wenzelm@64536
   175
                      indent_structure + indent_brackets + indent_size -
wenzelm@64536
   176
                      indent_offset(prev_tok) - indent_indent(prev_tok)
wenzelm@64536
   177
                  }
wenzelm@64518
   178
              }
wenzelm@63428
   179
            }
wenzelm@64518
   180
            else line_indent(current_line)
wenzelm@63423
   181
wenzelm@63425
   182
          actions.clear()
wenzelm@63439
   183
          actions.add(new IndentAction.AlignOffset(indent max 0))
wenzelm@63423
   184
        case _ =>
wenzelm@63423
   185
      }
wenzelm@63422
   186
    }
wenzelm@63422
   187
  }
wenzelm@63422
   188
wenzelm@63422
   189
wenzelm@63422
   190
  /* structure matching */
wenzelm@63422
   191
wenzelm@63422
   192
  object Matcher extends StructureMatcher
wenzelm@58748
   193
  {
wenzelm@58803
   194
    private def find_block(
wenzelm@58752
   195
      open: Token => Boolean,
wenzelm@58752
   196
      close: Token => Boolean,
wenzelm@58752
   197
      reset: Token => Boolean,
wenzelm@58762
   198
      restrict: Token => Boolean,
wenzelm@58754
   199
      it: Iterator[Text.Info[Token]]): Option[(Text.Range, Text.Range)] =
wenzelm@58752
   200
    {
wenzelm@58754
   201
      val range1 = it.next.range
wenzelm@58762
   202
      it.takeWhile(info => !info.info.is_command || restrict(info.info)).
wenzelm@58762
   203
        scanLeft((range1, 1))(
wenzelm@58762
   204
          { case ((r, d), Text.Info(range, tok)) =>
wenzelm@58762
   205
              if (open(tok)) (range, d + 1)
wenzelm@58762
   206
              else if (close(tok)) (range, d - 1)
wenzelm@58762
   207
              else if (reset(tok)) (range, 0)
wenzelm@58762
   208
              else (r, d) }
wenzelm@58762
   209
        ).collectFirst({ case (range2, 0) => (range1, range2) })
wenzelm@58752
   210
    }
wenzelm@58752
   211
wenzelm@58803
   212
    private def find_pair(text_area: TextArea): Option[(Text.Range, Text.Range)] =
wenzelm@58748
   213
    {
wenzelm@58748
   214
      val buffer = text_area.getBuffer
wenzelm@58748
   215
      val caret_line = text_area.getCaretLine
wenzelm@58749
   216
      val caret = text_area.getCaretPosition
wenzelm@58748
   217
wenzelm@59074
   218
      Isabelle.buffer_syntax(text_area.getBuffer) match {
wenzelm@63425
   219
        case Some(syntax) if buffer.isInstanceOf[Buffer] =>
wenzelm@63424
   220
          val keywords = syntax.keywords
wenzelm@58750
   221
wenzelm@63445
   222
          val nav = new Navigator(syntax, buffer.asInstanceOf[Buffer], false)
wenzelm@58750
   223
wenzelm@58752
   224
          def caret_iterator(): Iterator[Text.Info[Token]] =
wenzelm@63425
   225
            nav.iterator(caret_line).dropWhile(info => !info.range.touches(caret))
wenzelm@58752
   226
wenzelm@63427
   227
          def reverse_caret_iterator(): Iterator[Text.Info[Token]] =
wenzelm@63427
   228
            nav.reverse_iterator(caret_line).dropWhile(info => !info.range.touches(caret))
wenzelm@58752
   229
wenzelm@63425
   230
          nav.iterator(caret_line, 1).find(info => info.range.touches(caret))
wenzelm@58756
   231
          match {
wenzelm@63424
   232
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.theory_goal) =>
wenzelm@58755
   233
              find_block(
wenzelm@63424
   234
                keywords.is_command(_, Keyword.proof_goal),
wenzelm@63424
   235
                keywords.is_command(_, Keyword.qed),
wenzelm@63424
   236
                keywords.is_command(_, Keyword.qed_global),
wenzelm@58762
   237
                t =>
wenzelm@63424
   238
                  keywords.is_command(t, Keyword.diag) ||
wenzelm@63424
   239
                  keywords.is_command(t, Keyword.proof),
wenzelm@58755
   240
                caret_iterator())
wenzelm@58755
   241
wenzelm@63424
   242
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.proof_goal) =>
wenzelm@58755
   243
              find_block(
wenzelm@63424
   244
                keywords.is_command(_, Keyword.proof_goal),
wenzelm@63424
   245
                keywords.is_command(_, Keyword.qed),
wenzelm@58755
   246
                _ => false,
wenzelm@58762
   247
                t =>
wenzelm@63424
   248
                  keywords.is_command(t, Keyword.diag) ||
wenzelm@63424
   249
                  keywords.is_command(t, Keyword.proof),
wenzelm@58755
   250
                caret_iterator())
wenzelm@58755
   251
wenzelm@63424
   252
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.qed_global) =>
wenzelm@63427
   253
              reverse_caret_iterator().find(info => keywords.is_command(info.info, Keyword.theory))
wenzelm@58749
   254
              match {
wenzelm@58750
   255
                case Some(Text.Info(range2, tok))
wenzelm@63424
   256
                if keywords.is_command(tok, Keyword.theory_goal) => Some((range1, range2))
wenzelm@58749
   257
                case _ => None
wenzelm@58749
   258
              }
wenzelm@58752
   259
wenzelm@63424
   260
            case Some(Text.Info(range1, tok)) if keywords.is_command(tok, Keyword.qed) =>
wenzelm@58755
   261
              find_block(
wenzelm@63424
   262
                keywords.is_command(_, Keyword.qed),
wenzelm@58755
   263
                t =>
wenzelm@63424
   264
                  keywords.is_command(t, Keyword.proof_goal) ||
wenzelm@63424
   265
                  keywords.is_command(t, Keyword.theory_goal),
wenzelm@58755
   266
                _ => false,
wenzelm@58762
   267
                t =>
wenzelm@63424
   268
                  keywords.is_command(t, Keyword.diag) ||
wenzelm@63424
   269
                  keywords.is_command(t, Keyword.proof) ||
wenzelm@63424
   270
                  keywords.is_command(t, Keyword.theory_goal),
wenzelm@63427
   271
                reverse_caret_iterator())
wenzelm@58755
   272
wenzelm@58752
   273
            case Some(Text.Info(range1, tok)) if tok.is_begin =>
wenzelm@58762
   274
              find_block(_.is_begin, _.is_end, _ => false, _ => true, caret_iterator())
wenzelm@58752
   275
wenzelm@58752
   276
            case Some(Text.Info(range1, tok)) if tok.is_end =>
wenzelm@63427
   277
              find_block(_.is_end, _.is_begin, _ => false, _ => true, reverse_caret_iterator())
wenzelm@58763
   278
              match {
wenzelm@58763
   279
                case Some((_, range2)) =>
wenzelm@63427
   280
                  reverse_caret_iterator().
wenzelm@58800
   281
                    dropWhile(info => info.range != range2).
wenzelm@58800
   282
                    dropWhile(info => info.range == range2).
wenzelm@58800
   283
                    find(info => info.info.is_command || info.info.is_begin)
wenzelm@58763
   284
                  match {
wenzelm@58800
   285
                    case Some(Text.Info(range3, tok)) =>
wenzelm@63424
   286
                      if (keywords.is_command(tok, Keyword.theory_block)) Some((range1, range3))
wenzelm@58800
   287
                      else Some((range1, range2))
wenzelm@58763
   288
                    case None => None
wenzelm@58763
   289
                  }
wenzelm@58763
   290
                case None => None
wenzelm@58763
   291
              }
wenzelm@58752
   292
wenzelm@58749
   293
            case _ => None
wenzelm@58748
   294
          }
wenzelm@63425
   295
        case _ => None
wenzelm@58748
   296
      }
wenzelm@58748
   297
    }
wenzelm@58748
   298
wenzelm@58749
   299
    def getMatch(text_area: TextArea): StructureMatcher.Match =
wenzelm@58749
   300
      find_pair(text_area) match {
wenzelm@58749
   301
        case Some((_, range)) =>
wenzelm@58749
   302
          val line = text_area.getBuffer.getLineOfOffset(range.start)
wenzelm@63422
   303
          new StructureMatcher.Match(Matcher, line, range.start, line, range.stop)
wenzelm@58749
   304
        case None => null
wenzelm@58749
   305
      }
wenzelm@58749
   306
wenzelm@58748
   307
    def selectMatch(text_area: TextArea)
wenzelm@58748
   308
    {
wenzelm@58803
   309
      def get_span(offset: Text.Offset): Option[Text.Range] =
wenzelm@58803
   310
        for {
wenzelm@59074
   311
          syntax <- Isabelle.buffer_syntax(text_area.getBuffer)
wenzelm@58803
   312
          span <- Token_Markup.command_span(syntax, text_area.getBuffer, offset)
wenzelm@58803
   313
        } yield span.range
wenzelm@58803
   314
wenzelm@58803
   315
      find_pair(text_area) match {
wenzelm@58803
   316
        case Some((r1, r2)) =>
wenzelm@58803
   317
          (get_span(r1.start), get_span(r2.start)) match {
wenzelm@58803
   318
            case (Some(range1), Some(range2)) =>
wenzelm@58803
   319
              val start = range1.start min range2.start
wenzelm@58803
   320
              val stop = range1.stop max range2.stop
wenzelm@58803
   321
wenzelm@58803
   322
              text_area.moveCaretPosition(stop, false)
wenzelm@58803
   323
              if (!text_area.isMultipleSelectionEnabled) text_area.selectNone
wenzelm@58803
   324
              text_area.addToSelection(new Selection.Range(start, stop))
wenzelm@58803
   325
            case _ =>
wenzelm@58803
   326
          }
wenzelm@58803
   327
        case None =>
wenzelm@58803
   328
      }
wenzelm@58748
   329
    }
wenzelm@58748
   330
  }
wenzelm@58748
   331
}