Skip to content

Commit

Permalink
Added deleted comment and deleted CommentPosition (never used locally)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-suliga committed Nov 6, 2024
1 parent 47b16ed commit 51c2222
Showing 1 changed file with 115 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
import org.eclipse.jdt.core.dom.DoStatement;
import org.eclipse.jdt.core.dom.EnhancedForStatement;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.Initializer;
Expand All @@ -90,7 +89,6 @@
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.SynchronizedStatement;
import org.eclipse.jdt.core.dom.TryStatement;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.WhileStatement;

import org.eclipse.jdt.ui.PreferenceConstants;
Expand All @@ -99,7 +97,6 @@
import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
import org.eclipse.jdt.internal.ui.text.DocumentCharacterIterator;

/**
* Updates the projection model of a class file or compilation unit.
Expand Down Expand Up @@ -331,6 +328,99 @@ private static final class Tuple {
}
}

private class FoldingVisitor extends ASTVisitor {

private FoldingStructureComputationContext ctx;

public FoldingVisitor(FoldingStructureComputationContext ctx) {
this.ctx = ctx;
}

@Override
public boolean visit(MethodDeclaration node) {
processMethodDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(IfStatement node) {
processIfStatement(node, ctx);
return false;
}

@Override
public boolean visit(WhileStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(ForStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(EnhancedForStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(DoStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

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

@Override
public boolean visit(TryStatement node) {
processTryStatement(node, ctx);
return false;
}

@Override
public boolean visit(SynchronizedStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(LambdaExpression node) {
processLambdaExpression(node, ctx);
return false;
}

@Override
public boolean visit(AnonymousClassDeclaration node) {
processAnonymousClassDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(EnumDeclaration node) {
processEnumDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(Initializer node) {
processInitializer(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(Javadoc node) {
processComment(node, ctx);
return false;
}
}

/**
* Filter for annotations.
*/
Expand Down Expand Up @@ -492,87 +582,6 @@ private IJavaElementDelta findElement(IJavaElement target, IJavaElementDelta del
}
}

/**
* Projection position that will return two foldable regions: one folding away
* the region from after the '/**' to the beginning of the content, the other
* from after the first content line until after the comment.
*/
private static final class CommentPosition extends Position implements IProjectionPosition {
CommentPosition(int offset, int length) {
super(offset, length);
}

/*
* @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeFoldingRegions(org.eclipse.jface.text.IDocument)
*/
@Override
public IRegion[] computeProjectionRegions(IDocument document) throws BadLocationException {
DocumentCharacterIterator sequence= new DocumentCharacterIterator(document, offset, offset + length);
int prefixEnd= 0;
int contentStart= findFirstContent(sequence, prefixEnd);

int firstLine= document.getLineOfOffset(offset + prefixEnd);
int captionLine= document.getLineOfOffset(offset + contentStart);
int lastLine= document.getLineOfOffset(offset + length);

Assert.isTrue(firstLine <= captionLine, "first folded line is greater than the caption line"); //$NON-NLS-1$
Assert.isTrue(captionLine <= lastLine, "caption line is greater than the last folded line"); //$NON-NLS-1$

IRegion preRegion;
if (firstLine < captionLine) {
int preOffset= document.getLineOffset(firstLine);
IRegion preEndLineInfo= document.getLineInformation(captionLine);
int preEnd= preEndLineInfo.getOffset();
preRegion= new Region(preOffset, preEnd - preOffset);
} else {
preRegion= null;
}

if (captionLine < lastLine) {
int postOffset= document.getLineOffset(captionLine + 1);
int postLength= offset + length - postOffset;
if (postLength > 0) {
IRegion postRegion= new Region(postOffset, postLength);
if (preRegion == null)
return new IRegion[] { postRegion };
return new IRegion[] { preRegion, postRegion };
}
}

if (preRegion != null)
return new IRegion[] { preRegion };

return null;
}

/**
* Finds the offset of the first identifier part within <code>content</code>.
* Returns 0 if none is found.
*
* @param content the content to search
* @param prefixEnd the end of the prefix
* @return the first index of a unicode identifier part, or zero if none can
* be found
*/
private int findFirstContent(final CharSequence content, int prefixEnd) {
int lenght= content.length();
for (int i= prefixEnd; i < lenght; i++) {
if (Character.isUnicodeIdentifierPart(content.charAt(i)))
return i;
}
return 0;
}

/*
* @see org.eclipse.jface.text.source.projection.IProjectionPosition#computeCaptionOffset(org.eclipse.jface.text.IDocument)
*/
@Override
public int computeCaptionOffset(IDocument document) throws BadLocationException {
DocumentCharacterIterator sequence= new DocumentCharacterIterator(document, offset, offset + length);
return findFirstContent(sequence, 0);
}
}

/**
* Projection position that will return two foldable regions: one folding away
* the lines before the one containing the simple name of the java element, one
Expand Down Expand Up @@ -1006,104 +1015,7 @@ private void processCompilationUnit(ICompilationUnit unit, FoldingStructureCompu
parser.setResolveBindings(true);
CompilationUnit ast = (CompilationUnit) parser.createAST(null);

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

@Override
public boolean visit(FieldDeclaration node) {
processFieldDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(IfStatement node) {
processIfStatement(node, ctx);
return false; // Prevents visiting children since we handle them explicitly
}

@Override
public boolean visit(WhileStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(ForStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(EnhancedForStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(DoStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

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

@Override
public boolean visit(TryStatement node) {
processTryStatement(node, ctx);
return false; // We handle the catch and finally blocks explicitly
}

@Override
public boolean visit(SynchronizedStatement node) {
processStatement(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(LambdaExpression node) {
processLambdaExpression(node, ctx);
return false; // Prevents visiting children since we handle them explicitly
}

@Override
public boolean visit(TypeDeclaration node) {
processTypeDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(AnonymousClassDeclaration node) {
processAnonymousClassDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(EnumDeclaration node) {
processEnumDeclaration(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(Initializer node) {
processInitializer(node, ctx);
return super.visit(node);
}

@Override
public boolean visit(Javadoc node) {
processComment(node, ctx);
return false; // No need to visit children of Javadoc
}

});
ast.accept(new FoldingVisitor(ctx));
} catch (JavaModelException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -1136,19 +1048,7 @@ private void processMethodDeclaration(MethodDeclaration node, FoldingStructureCo
}
}

private void processFieldDeclaration(FieldDeclaration node, FoldingStructureComputationContext ctx) {
createFoldingRegion(node, ctx, ctx.collapseMembers());
if (node.getJavadoc() != null) {
processComment(node.getJavadoc(), ctx);
}
}

private void processTypeDeclaration(TypeDeclaration node, FoldingStructureComputationContext ctx) {
createFoldingRegion(node, ctx, ctx.collapseMembers());
if (node.getJavadoc() != null) {
processComment(node.getJavadoc(), ctx);
}
}

private void processAnonymousClassDeclaration(AnonymousClassDeclaration node, FoldingStructureComputationContext ctx) {
createFoldingRegion(node, ctx, ctx.collapseMembers());
Expand Down Expand Up @@ -1248,6 +1148,19 @@ private int calculateEndPosition(ASTNode node, FoldingStructureComputationContex
return end;
}

/**
* Aligns <code>region</code> to start and end at a line offset. The region's start is
* decreased to the next line offset, and the end offset increased to the next line start or the
* end of the document. <code>null</code> is returned if <code>region</code> is
* <code>null</code> itself or does not comprise at least one line delimiter, as a single line
* cannot be folded.
*
* @param region the region to align, may be <code>null</code>
* @param ctx the folding context
* @return a region equal or greater than <code>region</code> that is aligned with line
* offsets, <code>null</code> if the region is too small to be foldable (e.g. covers
* only one line)
*/
protected IRegion alignRegion(IRegion region, FoldingStructureComputationContext ctx) {
if (region == null) {
return null;
Expand Down Expand Up @@ -1289,6 +1202,14 @@ private void processComment(Javadoc node, FoldingStructureComputationContext ctx
}
}

/**
* Creates a comment folding position from an
* {@link #alignRegion(IRegion, DefaultJavaFoldingStructureProvider.FoldingStructureComputationContext) aligned}
* region.
*
* @param aligned an aligned region
* @return a folding position corresponding to <code>aligned</code>
*/
protected Position createCommentPosition(IRegion aligned) {
return new Position(aligned.getOffset(), aligned.getLength());
}
Expand Down

0 comments on commit 51c2222

Please sign in to comment.