IDocument doc = getDocumentProvider().getDocument(getEditorInput());
String source = doc.get();
ArrayList<FoldingInfo> list = new ArrayList<FoldingInfo>();
Stack<FoldingInfo> stack = new Stack<FoldingInfo>();
FoldingInfo prev = null;
char quote = 0;
boolean escape = false;
for(int i=0;i<source.length();i++){
char c = source.charAt(i);
// skip string
if(quote!=0 && escape==true){
escape = false;
} else if((prev==null || !prev.getType().equals("comment")) && (c=='"' || c=='\'')){
if(quote==0){
quote = c;
} else if(quote == c){
quote = 0;
}
} else if(quote!=0 && (c=='\\')){
escape = true;
} else if(quote!=0 && (c=='\n' || c=='\r')){
quote = 0;
// start comment
} else if(c=='/' && source.length() > i+1 && quote==0){
if(source.charAt(i+1)=='*'){
prev = new FoldingInfo(i,-1,"comment");
stack.push(prev);
i++;
}
// end comment
} else if(c=='*' && source.length() > i+1 && !stack.isEmpty() && quote==0){
if(source.charAt(i+1)=='/' && prev.getType().equals("comment")){
FoldingInfo info = stack.pop();
if(doc.getLineOfOffset(info.getStart())!=doc.getLineOfOffset(i)){
list.add(new FoldingInfo(info.getStart(), i+2 + FoldingInfo.countUpLineDelimiter(source, i+2), "comment"));
}
prev = stack.isEmpty() ? null : (FoldingInfo)stack.get(stack.size()-1);
i++;
}
// open blace
} else if(c=='{' && quote==0){
if(prev==null || !prev.getType().equals("comment")){
if(findFunction(source, i)){
prev = new FoldingInfo(i, -1, "function");
} else {
prev = new FoldingInfo(i, -1, "blace");
}
stack.push(prev);
}
// close blace
} else if(c=='}' && prev!=null && !prev.getType().equals("comment") && quote==0){
FoldingInfo info = stack.pop();
if(info.getType().equals("function") && doc.getLineOfOffset(info.getStart())!=doc.getLineOfOffset(i)){
list.add(new FoldingInfo(info.getStart(), i+2 + FoldingInfo.countUpLineDelimiter(source, i+2), "function"));
}
prev = stack.isEmpty() ? null : (FoldingInfo)stack.get(stack.size()-1);
}
}