~ubuntu-branches/ubuntu/trusty/drmips/trusty-backports

« back to all changes in this revision

Viewing changes to src/pc/DrMIPS/src/org/fife/ui/rsyntaxtextarea/folding/LatexFoldParser.java

  • Committer: Package Import Robot
  • Author(s): Bruno Nova
  • Date: 2014-09-27 12:24:17 UTC
  • Revision ID: package-import@ubuntu.com-20140927122417-2gadkwt9k0u7j4zu
Tags: upstream-1.2.3
Import upstream version 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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<Fold> getFolds(RSyntaxTextArea textArea) {
 
37
 
 
38
                List<Fold> folds = new ArrayList<Fold>();
 
39
                Stack<String> expectedStack = new Stack<String>();
 
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.getType()==Token.RESERVED_WORD) {
 
56
                                                                if (currentFold==null) {
 
57
                                                                        currentFold = new Fold(FoldType.CODE, textArea, t.getOffset());
 
58
                                                                        folds.add(currentFold);
 
59
                                                                }
 
60
                                                                else {
 
61
                                                                        currentFold = currentFold.createChild(FoldType.CODE, t.getOffset());
 
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.getType()==Token.RESERVED_WORD) {
 
75
                                                                String value = temp.getLexeme();
 
76
                                                                if (expectedStack.peek().equals(value)) {
 
77
                                                                        expectedStack.pop();
 
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);
 
84
                                                                                }
 
85
                                                                        }
 
86
                                                                        t = temp;
 
87
                                                                        currentFold = parentFold;
 
88
                                                                }
 
89
                                                        }
 
90
                                                }
 
91
                                        }
 
92
 
 
93
                                        t = t.getNextToken();
 
94
 
 
95
                                }
 
96
 
 
97
                        }
 
98
 
 
99
                } catch (BadLocationException ble) {
 
100
                        ble.printStackTrace(); // Never happens
 
101
                }
 
102
 
 
103
                return folds;
 
104
 
 
105
        }
 
106
 
 
107
 
 
108
}
 
 
b'\\ No newline at end of file'