Document document = hierarchy.getComponent().getDocument();
TokenHierarchy<Document> hi = TokenHierarchy.get(document);
TokenSequence<CompilerTokenId> ts = (TokenSequence<CompilerTokenId>) hi.tokenSequence();
int actionStack = 0;
CompilerTokenId actionStart = null;
int actionOffset = -1;
int useOffset = -1;
int endUseOffset = -1;
FoldType type = null;
int start = 0;
int offset = 0;
while (ts.moveNext()) {
offset = ts.offset();
Token<CompilerTokenId> token = ts.token();
CompilerTokenId id = token.id();
if (id.name().equals(CompilerTokenId.COMMENTS)) {
start = offset;
try {
operation.addToHierarchy(
COMMENT_FOLD_TYPE,
COMMENT_FOLD_TYPE.toString(),
false,
start,
offset + token.length(),
0,
0,
hierarchy,
transaction);
} catch (BadLocationException ex) {
logger.log(Level.INFO, ex.getMessage());
}
} else if (id.name().equals(CompilerTokenId.USE)) {
useOffset = ts.offset();
ts.moveNext();
skipWhitespaceComments(ts);
//skip any number of periods
boolean done = false;
while (!done && ts.moveNext()) {
if ( !ts.token().id().name().equals(CompilerTokenId.PERIOD)
&& !ts.token().id().name().equals(CompilerTokenId.ID)) {
//mark the location and check if there's another use statement
endUseOffset = ts.offset();
skipWhitespaceNewLinesComments(ts);
if (!ts.token().id().name().equals(CompilerTokenId.USE)) {
done = true;
} else {
ts.moveNext();
skipWhitespaceNewLinesComments(ts);
}
}
}
try {
operation.addToHierarchy(
USE_FOLD_TYPE,
USE_FOLD_TYPE.toString(),
false,
useOffset,
endUseOffset,
0,
0,
hierarchy,
transaction);
} catch (BadLocationException ex) {
logger.log(Level.INFO, ex.getMessage());
}
ts.movePrevious();
unskipWhitespaceNewLinesComments(ts);
}//if it's an action, find its end position
else if (id.name().equals(CompilerTokenId.ACTION)) {
actionStart = id;
actionStack = 0;
ts.moveNext();
Token<CompilerTokenId> search = null;
int returnedOffset = skipWhitespaceComments(ts);
actionOffset = ts.offset() + ts.token().length();
ts.moveNext();
returnedOffset = skipWhitespaceComments(ts);
search = ts.token();
if (search.id().name().equals(CompilerTokenId.LEFT_PAREN)) {
boolean done = false;
while (!done && ts.moveNext()) {
Token<CompilerTokenId> rightSide = ts.token();
if (rightSide.id().name().equals(CompilerTokenId.RIGHT_PAREN)) {
actionOffset = ts.offset() + ts.token().length();
ts.moveNext();
returnedOffset = skipWhitespaceComments(ts);
rightSide = ts.token();
if (rightSide.id().name().equals(CompilerTokenId.RETURNS)) {
ts.moveNext();
returnedOffset = skipWhitespaceComments(ts);
actionOffset = ts.offset() + ts.token().length();
} else if(rightSide.id().name().equals(CompilerTokenId.END)) {
ts.movePrevious();
}
done = true;
}
else if (rightSide.id().name().equals(CompilerTokenId.RETURNS)) {
done = true;
actionOffset = ts.offset() + ts.token().length();
ts.moveNext();
returnedOffset = skipWhitespaceComments(ts);
if(returnedOffset != -1) {
actionOffset = returnedOffset;
}
ts.moveNext();
actionOffset = ts.offset() + ts.token().length();
}
}
} else if(search.id().name().equals(CompilerTokenId.RETURNS)) {
actionOffset = ts.offset() + ts.token().length();
ts.moveNext();
returnedOffset = skipWhitespaceComments(ts);
actionOffset = ts.offset() + ts.token().length();
}
else if(search.id().name().equals(CompilerTokenId.END)) {
ts.movePrevious();
}
} else if (id.name().equals(CompilerTokenId.IF)) {
actionStack = actionStack + 1;
} else if (id.name().equals(CompilerTokenId.REPEAT)) {
actionStack = actionStack + 1;
} else if (id.name().equals(CompilerTokenId.CHECK)) {
actionStack = actionStack + 1;
} else if (id.name().equals(CompilerTokenId.END)) {
if (actionStack == 0 && actionOffset != -1) { //matching action statement
start = actionOffset;
try {
operation.addToHierarchy(
ACTION_FOLD_TYPE,