Skip to content

Commit

Permalink
Fixed nested folding bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-suliga committed Sep 25, 2024
1 parent 56aa875 commit afd4aed
Showing 1 changed file with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;

import org.eclipse.core.runtime.Assert;

Expand Down Expand Up @@ -476,7 +477,6 @@ public IRegion[] computeProjectionRegions(IDocument document) throws BadLocation

IRegion preRegion;
if (firstLine < captionLine) {
// preRegion= new Region(offset + prefixEnd, contentStart - prefixEnd);
int preOffset= document.getLineOffset(firstLine);
IRegion preEndLineInfo= document.getLineInformation(captionLine);
int preEnd= preEndLineInfo.getOffset();
Expand Down Expand Up @@ -520,33 +520,6 @@ private int findFirstContent(final CharSequence content, int prefixEnd) {
return 0;
}

// /**
// * Finds the offset of the first identifier part within <code>content</code>.
// * Returns 0 if none is found.
// *
// * @param content the content to search
// * @return the first index of a unicode identifier part, or zero if none can
// * be found
// */
// private int findPrefixEnd(final CharSequence content) {
// // return the index after the leading '/*' or '/**'
// int len= content.length();
// int i= 0;
// while (i < len && isWhiteSpace(content.charAt(i)))
// i++;
// if (len >= i + 2 && content.charAt(i) == '/' && content.charAt(i + 1) == '*')
// if (len >= i + 3 && content.charAt(i + 2) == '*')
// return i + 3;
// else
// return i + 2;
// else
// return i;
// }
//
// private boolean isWhiteSpace(char c) {
// return c == ' ' || c == '\t';
// }

/*
* @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeCaptionOffset(org.eclipse.jface.text.IDocument)
*/
Expand Down Expand Up @@ -979,10 +952,44 @@ private void computeFoldingStructure(FoldingStructureComputationContext ctx) {

ctx.getScanner().setSource(source.toCharArray());
computeFoldingStructure(parent.getChildren(), ctx);
computeFoldingStructureOfControlStatements(source, ctx);
} catch (JavaModelException x) {
}
}

private void computeFoldingStructureOfControlStatements(String source, FoldingStructureComputationContext ctx) {
IScanner scanner = ctx.getScanner();
scanner.setSource(source.toCharArray());
Stack<Integer> blockStarts = new Stack<>();
int token;

try {
while ((token = scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
switch (token) {
case ITerminalSymbols.TokenNameLBRACE:
blockStarts.push(scanner.getCurrentTokenStartPosition());
break;
case ITerminalSymbols.TokenNameRBRACE:
if (!blockStarts.isEmpty()) {
int start = blockStarts.pop();
if (source.charAt(start) == '{') {
IRegion region = new Region(start + 1, scanner.getCurrentTokenEndPosition() - start);
IRegion normalized = alignRegion(region, ctx);
if (normalized != null) {
Position position = createCommentPosition(normalized);
if (position != null) {
ctx.addProjectionRange(new JavaProjectionAnnotation(false, null, false), position);
}
}
}
}
break;
}
}
} catch (InvalidInputException e) {
}
}

private void computeFoldingStructure(IJavaElement[] elements, FoldingStructureComputationContext ctx) throws JavaModelException {
for (IJavaElement element : elements) {
computeFoldingStructure(element, ctx);
Expand Down

0 comments on commit afd4aed

Please sign in to comment.