Skip to content

Commit

Permalink
Fixed Problems with folding mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-suliga committed Oct 8, 2024
1 parent 80e43fd commit eaacf49
Showing 1 changed file with 76 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 @@ -69,7 +68,15 @@
import org.eclipse.jdt.core.compiler.IScanner;
import org.eclipse.jdt.core.compiler.ITerminalSymbols;
import org.eclipse.jdt.core.compiler.InvalidInputException;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.WhileStatement;

import org.eclipse.jdt.ui.PreferenceConstants;

Expand Down Expand Up @@ -963,51 +970,63 @@ private void update(FoldingStructureComputationContext ctx) {
}

private void computeFoldingStructure(FoldingStructureComputationContext ctx) {
IParent parent= (IParent) fInput;
try {
if (!(fInput instanceof ISourceReference))
return;
String source= ((ISourceReference)fInput).getSource();
if (source == null)
return;
ICompilationUnit unit = (ICompilationUnit) fInput;
ASTParser parser = ASTParser.newParser(AST.JLS_Latest);
parser.setSource(unit);
parser.setResolveBindings(true);
CompilationUnit ast = (CompilationUnit) parser.createAST(null);

ctx.getScanner().setSource(source.toCharArray());
computeFoldingStructure(parent.getChildren(), ctx);
computeFoldingStructureOfControlStatements(source, ctx);
} catch (JavaModelException x) {
}
}
try {
if (!(fInput instanceof ISourceReference))
return;
String source = ((ISourceReference) fInput).getSource();
if (source == null)
return;

ctx.getScanner().setSource(source.toCharArray());

IParent parent = (IParent) fInput;
computeFoldingStructure(parent.getChildren(), ctx);

ast.accept(new ASTVisitor() {
@Override
public boolean visit(IfStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

private void computeFoldingStructureOfControlStatements(String source, FoldingStructureComputationContext ctx) {
IScanner scanner = ctx.getScanner();
scanner.setSource(source.toCharArray());
Stack<Integer> blockStarts = new Stack<>();
int token;
@Override
public boolean visit(WhileStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

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;
@Override
public boolean visit(ForStatement node) {
processStatement(node, ctx);
return super.visit(node);
}
}
} catch (InvalidInputException e) {

@Override
public boolean visit(SwitchStatement node) {
processStatement(node, ctx);
return super.visit(node);
}
});
} catch (JavaModelException e) {
}
}


private void processStatement(Statement node, FoldingStructureComputationContext ctx) {
int start = node.getStartPosition();
int length = node.getLength();

IRegion aligned = alignRegion(new Region(start, length), ctx);
if (aligned != null) {
Position position = new Position(aligned.getOffset(), aligned.getLength());
JavaProjectionAnnotation annotation = new JavaProjectionAnnotation(ctx.collapseMembers(), null, false);
ctx.addProjectionRange(annotation, position);
}
}

Expand Down Expand Up @@ -1280,33 +1299,28 @@ protected final Position createMemberPosition(IRegion aligned, IMember member) {
* only one line)
*/
protected final IRegion alignRegion(IRegion region, FoldingStructureComputationContext ctx) {
if (region == null)
return null;

IDocument document= ctx.getDocument();
if (region == null)
return null;

try {
IDocument document = ctx.getDocument();

int start= document.getLineOfOffset(region.getOffset());
int end= document.getLineOfOffset(region.getOffset() + region.getLength());
if (start >= end)
return null;
try {
int start = document.getLineOfOffset(region.getOffset());
int end = document.getLineOfOffset(region.getOffset() + region.getLength());

int offset= document.getLineOffset(start);
int endOffset;
if (document.getNumberOfLines() > end + 1)
endOffset= document.getLineOffset(end + 1);
else
endOffset= document.getLineOffset(end) + document.getLineLength(end);
if (start >= end)
return null;

return new Region(offset, endOffset - offset);
int offset = document.getLineOffset(start);
int endOffset = document.getLineOffset(end) + document.getLineLength(end);

} catch (BadLocationException x) {
// concurrent modification
return null;
}
return new Region(offset, endOffset - offset);
} catch (BadLocationException e) {
return null;
}
}


private ProjectionAnnotationModel getModel() {
return fEditor.getAdapter(ProjectionAnnotationModel.class);
}
Expand Down

0 comments on commit eaacf49

Please sign in to comment.