Search in sources :

Example 1 with TokenSequence

use of org.netbeans.api.lexer.TokenSequence in project enclojure by EricThorsen.

the class ClojureFoldManager method getTopLevelFolds.

public static List<FoldInfo> getTopLevelFolds(BaseDocument doc) {
    ArrayList<FoldInfo> foldList = new ArrayList<FoldInfo>();
    Stack stack = new Stack();
    int start = 0;
    int end = 0;
    String foldDesc = EMPTY_STRING;
    String foldTypeDesc = EMPTY_STRING;
    boolean readName = false;
    StringBuilder foldText = new StringBuilder();
    TokenSequence tokens = TokenHierarchy.get(doc).tokenSequence();
    tokens.moveStart();
    while (tokens.moveNext()) {
        String tk = tokens.token().id().name();
        String tkn = tokens.token().toString();
        foldText.append(tkn);
        if (tk.equals(LIST_START)) {
            stack.push(tk);
            if (stack.size() == 1)
                start = tokens.offset();
        } else if (tk.equals(LIST_END)) {
            if (!stack.isEmpty())
                stack.pop();
            if (stack.empty()) {
                end = tokens.offset() + 1;
                foldList.add(getFoldInfo(start, end, getFoldType(foldTypeDesc), getFoldString(foldText, foldDesc, MAX_FOLD_DESC_LEN)));
                start = 0;
                end = 0;
                foldTypeDesc = EMPTY_STRING;
                foldDesc = EMPTY_STRING;
                foldText = foldText.replace(0, foldText.length() - 1, EMPTY_STRING);
            }
        } else if (tkn.equals(DEF) || tkn.equals(DEFN) || tkn.equals(DEFMACRO)) {
            foldTypeDesc = tkn;
            readName = true;
        } else if (readName) {
            foldDesc = getFoldDescription(foldTypeDesc, tkn);
            readName = false;
        }
    }
    return foldList;
}
Also used : ArrayList(java.util.ArrayList) TokenSequence(org.netbeans.api.lexer.TokenSequence) Stack(java.util.Stack)

Aggregations

ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 TokenSequence (org.netbeans.api.lexer.TokenSequence)1