author | wenzelm |
Tue, 21 Oct 2014 20:19:14 +0200 | |
changeset 58752 | 2077bc9558cf |
parent 58750 | 1b4b005d73c1 |
child 58754 | 0232d43422d6 |
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 |
{ |
|
58752 | 19 |
def scan_block( |
20 |
open: Token => Boolean, |
|
21 |
close: Token => Boolean, |
|
22 |
reset: Token => Boolean, |
|
23 |
init_range: Text.Range, |
|
24 |
init_depth: Int, |
|
25 |
it: Iterator[Text.Info[Token]]): Option[Text.Range] = |
|
26 |
{ |
|
27 |
it.scanLeft((init_range, init_depth))( |
|
28 |
{ case ((r, d), Text.Info(range, tok)) => |
|
29 |
if (open(tok)) (range, d + 1) |
|
30 |
else if (close(tok)) (range, d - 1) |
|
31 |
else if (reset(tok)) (range, 0) |
|
32 |
else (r, d) } |
|
33 |
).collectFirst({ case (r, 0) => r }) |
|
34 |
} |
|
35 |
||
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
36 |
def find_pair(text_area: TextArea): Option[(Text.Range, Text.Range)] = |
58748 | 37 |
{ |
38 |
val buffer = text_area.getBuffer |
|
39 |
val caret_line = text_area.getCaretLine |
|
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
40 |
val caret = text_area.getCaretPosition |
58748 | 41 |
|
42 |
PIDE.session.recent_syntax match { |
|
58750 | 43 |
case syntax: Outer_Syntax |
44 |
if syntax != Outer_Syntax.empty => |
|
45 |
||
46 |
val limit = PIDE.options.value.int("jedit_structure_limit") max 0 |
|
47 |
||
48 |
def iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] = |
|
49 |
Token_Markup.line_token_iterator(syntax, buffer, line, line + lim) |
|
50 |
||
51 |
def rev_iterator(line: Int, lim: Int = limit): Iterator[Text.Info[Token]] = |
|
52 |
Token_Markup.line_token_reverse_iterator(syntax, buffer, line, line - lim) |
|
53 |
||
58752 | 54 |
def caret_iterator(): Iterator[Text.Info[Token]] = |
55 |
iterator(caret_line).dropWhile(info => !info.range.touches(caret)) |
|
56 |
||
57 |
def rev_caret_iterator(): Iterator[Text.Info[Token]] = |
|
58 |
rev_iterator(caret_line).dropWhile(info => !info.range.touches(caret)) |
|
59 |
||
60 |
iterator(caret_line, 1).find(info => info.range.touches(caret)) match |
|
61 |
{ |
|
58750 | 62 |
case Some(Text.Info(range1, tok)) if syntax.command_kind(tok, Keyword.qed_global) => |
58752 | 63 |
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
|
64 |
match { |
58750 | 65 |
case Some(Text.Info(range2, tok)) |
66 |
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
|
67 |
case _ => None |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
68 |
} |
58752 | 69 |
|
70 |
case Some(Text.Info(range1, tok)) if tok.is_begin => |
|
71 |
val it = caret_iterator() |
|
72 |
it.next |
|
73 |
scan_block(_.is_begin, _.is_end, _ => false, range1, 1, it). |
|
74 |
map(range2 => (range1, range2)) |
|
75 |
||
76 |
case Some(Text.Info(range1, tok)) if tok.is_end => |
|
77 |
val it = rev_caret_iterator() |
|
78 |
it.next |
|
79 |
scan_block(_.is_end, _.is_begin, _ => false, range1, 1, it). |
|
80 |
map(range2 => (range1, range2)) |
|
81 |
||
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
82 |
case _ => None |
58748 | 83 |
} |
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
84 |
case _ => None |
58748 | 85 |
} |
86 |
} |
|
87 |
||
58749
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
88 |
def getMatch(text_area: TextArea): StructureMatcher.Match = |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
89 |
find_pair(text_area) match { |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
90 |
case Some((_, range)) => |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
91 |
val line = text_area.getBuffer.getLineOfOffset(range.start) |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
92 |
new StructureMatcher.Match(Structure_Matching.Isabelle_Matcher, |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
93 |
line, range.start, line, range.stop) |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
94 |
case None => null |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
95 |
} |
83b0f633190e
some structure matching, based on line token iterators;
wenzelm
parents:
58748
diff
changeset
|
96 |
|
58748 | 97 |
def selectMatch(text_area: TextArea) |
98 |
{ |
|
99 |
// FIXME |
|
100 |
} |
|
101 |
} |
|
102 |
} |
|
103 |