4
* LatexFoldParser.java - Fold parser for LaTeX.
6
* This library is distributed under a modified BSD license. See the included
7
* RSyntaxTextArea.License.txt file for details.
9
package org.fife.ui.rsyntaxtextarea.folding;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Stack;
14
import javax.swing.text.BadLocationException;
16
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
17
import org.fife.ui.rsyntaxtextarea.Token;
21
* A fold parser for LaTeX documents. This is likely incomplete and/or not
22
* quite right; feedback is appreciated.
24
* @author Robert Futrell
27
public class LatexFoldParser implements FoldParser {
29
private static final char[] BEGIN = "\\begin".toCharArray();
30
private static final char[] END = "\\end".toCharArray();
36
public List<Fold> getFolds(RSyntaxTextArea textArea) {
38
List<Fold> folds = new ArrayList<Fold>();
39
Stack<String> expectedStack = new Stack<String>();
41
Fold currentFold = null;
42
int lineCount = textArea.getLineCount();
46
for (int line=0; line<lineCount; line++) {
48
Token t = textArea.getTokenListForLine(line);
49
while (t!=null && t.isPaintable()) {
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.getType()==Token.RESERVED_WORD) {
56
if (currentFold==null) {
57
currentFold = new Fold(FoldType.CODE, textArea, t.getOffset());
58
folds.add(currentFold);
61
currentFold = currentFold.createChild(FoldType.CODE, t.getOffset());
63
expectedStack.push(temp.getLexeme());
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.getType()==Token.RESERVED_WORD) {
75
String value = temp.getLexeme();
76
if (expectedStack.peek().equals(value)) {
78
currentFold.setEndOffset(t.getOffset());
79
Fold parentFold = currentFold.getParent();
80
// Don't add fold markers for single-line blocks
81
if (currentFold.isOnSingleLine()) {
82
if (!currentFold.removeFromParent()) {
83
folds.remove(folds.size()-1);
87
currentFold = parentFold;
99
} catch (BadLocationException ble) {
100
ble.printStackTrace(); // Never happens
b'\\ No newline at end of file'