1 | /*
|
---|
2 | * 04/24/2012
|
---|
3 | *
|
---|
4 | * LatexFoldParser.java - Fold parser for LaTeX.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea.folding;
|
---|
10 |
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Stack;
|
---|
14 | import javax.swing.text.BadLocationException;
|
---|
15 |
|
---|
16 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
---|
17 | import org.fife.ui.rsyntaxtextarea.Token;
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * A fold parser for LaTeX documents. This is likely incomplete and/or not
|
---|
22 | * quite right; feedback is appreciated.
|
---|
23 | *
|
---|
24 | * @author Robert Futrell
|
---|
25 | * @version 1.0
|
---|
26 | */
|
---|
27 | public class LatexFoldParser implements FoldParser {
|
---|
28 |
|
---|
29 | private static final char[] BEGIN = "\\begin".toCharArray();
|
---|
30 | private static final char[] END = "\\end".toCharArray();
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * {@inheritDoc}
|
---|
35 | */
|
---|
36 | public List getFolds(RSyntaxTextArea textArea) {
|
---|
37 |
|
---|
38 | List folds = new ArrayList();
|
---|
39 | Stack expectedStack = new Stack();
|
---|
40 |
|
---|
41 | Fold currentFold = null;
|
---|
42 | int lineCount = textArea.getLineCount();
|
---|
43 |
|
---|
44 | try {
|
---|
45 |
|
---|
46 | for (int line=0; line<lineCount; line++) {
|
---|
47 |
|
---|
48 | Token t = textArea.getTokenListForLine(line);
|
---|
49 | while (t!=null && t.isPaintable()) {
|
---|
50 |
|
---|
51 | if (t.is(Token.RESERVED_WORD, BEGIN)) {
|
---|
52 | Token temp = t.getNextToken();
|
---|
53 | if (temp!=null && temp.isLeftCurly()) {
|
---|
54 | temp = temp.getNextToken();
|
---|
55 | if (temp!=null && temp.type==Token.RESERVED_WORD) {
|
---|
56 | if (currentFold==null) {
|
---|
57 | currentFold = new Fold(FoldType.CODE, textArea, t.offset);
|
---|
58 | folds.add(currentFold);
|
---|
59 | }
|
---|
60 | else {
|
---|
61 | currentFold = currentFold.createChild(FoldType.CODE, t.offset);
|
---|
62 | }
|
---|
63 | expectedStack.push(temp.getLexeme());
|
---|
64 | t = temp;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | else if (t.is(Token.RESERVED_WORD, END) &&
|
---|
70 | currentFold!=null && !expectedStack.isEmpty()) {
|
---|
71 | Token temp = t.getNextToken();
|
---|
72 | if (temp!=null && temp.isLeftCurly()) {
|
---|
73 | temp = temp.getNextToken();
|
---|
74 | if (temp!=null && temp.type==Token.RESERVED_WORD) {
|
---|
75 | String value = temp.getLexeme();
|
---|
76 | if (expectedStack.peek().equals(value)) {
|
---|
77 | expectedStack.pop();
|
---|
78 | currentFold.setEndOffset(t.offset);
|
---|
79 | Fold parentFold = currentFold.getParent();
|
---|
80 | // Don't add fold markers for single-line blocks
|
---|
81 | if (currentFold.isOnSingleLine()) {
|
---|
82 | if (parentFold!=null) {
|
---|
83 | currentFold.removeFromParent();
|
---|
84 | }
|
---|
85 | else {
|
---|
86 | folds.remove(folds.size()-1);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | t = temp;
|
---|
90 | currentFold = parentFold;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | t = t.getNextToken();
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|
101 |
|
---|
102 | } catch (BadLocationException ble) {
|
---|
103 | ble.printStackTrace(); // Never happens
|
---|
104 | }
|
---|
105 |
|
---|
106 | return folds;
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | } |
---|