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