Skip to content

Commit

Permalink
apache#4447 - Fix missing code folding blocks in java editor for cond…
Browse files Browse the repository at this point in the history
…itions, loops and try/catch

The motivation about this was to implement those missing folding blocks in java editor. This was just implemented for imports, classes, inner classes, methods but not for stuff inside methods like if/else if/else, try/catch and loops because all of them are treated like code blocks.

Also introducing code-block fold type for specific code parts and separate from method fold type for customization. Add missing implementation of new method for method folding and add method folding to deprecated Java Editor lib as requested. Map the code block to the method block for deprecated Java Editor Lib
  • Loading branch information
Chris authored and Chris2011 committed May 16, 2023
1 parent 5043b05 commit bb1584c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
Expand Down Expand Up @@ -170,8 +171,24 @@ private void handleTree(int symStart, Tree node, Tree javadocTree, boolean handl

@Override
public Object visitMethod(MethodTree node, Object p) {
super.visitMethod(node, p);
handleTree((int)sp.getStartPosition(cu, node), node.getBody(), node, false);
super.visitMethod(node, Boolean.TRUE);

try {
if (p == Boolean.TRUE) {
int start = Utilities.findBodyStart(info, node, cu, sp, doc);
int end = (int)sp.getEndPosition(cu, node);

if (start != (-1) && start < end) {
addFold(creator.createMethodFold(start, end), (int)sp.getStartPosition(cu, node));
}
}

handleJavadoc(node);
} catch (BadLocationException | ConcurrentModificationException e) {
//the document probably changed, stop
stopped = true;
}

return null;
}

Expand Down Expand Up @@ -213,7 +230,8 @@ public Object visitBlock(BlockTree node, Object p) {
//check static/dynamic initializer:
TreePath path = getCurrentPath();

if (TreeUtilities.CLASS_TREE_KINDS.contains(path.getParentPath().getLeaf().getKind())) {
if (TreeUtilities.CLASS_TREE_KINDS.contains(path.getParentPath().getLeaf().getKind()) ||
!Kind.METHOD.equals(path.getParentPath().getLeaf().getKind())) {
handleTree(node, null, false);
}

Expand Down Expand Up @@ -308,6 +326,7 @@ public static interface FoldCreator<T> {

T createImportsFold(int start, int end);
T createInnerClassFold(int start, int end);
T createMethodFold(int start, int end);
T createCodeBlockFold(int start, int end);
T createJavadocFold(int start, int end);
T createInitialCommentFold(int start, int end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public abstract class JavaFoldManager implements FoldManager {

public static final FoldType JAVADOC_FOLD_TYPE = JavaElementFoldManager.JAVADOC_FOLD_TYPE;

public static final FoldType CODE_BLOCK_FOLD_TYPE = JavaElementFoldManager.CODE_BLOCK_FOLD_TYPE;
public static final FoldType CODE_BLOCK_FOLD_TYPE = JavaElementFoldManager.METHOD_BLOCK_FOLD_TYPE;

public static final FoldType METHOD_BLOCK_FOLD_TYPE = JavaElementFoldManager.METHOD_BLOCK_FOLD_TYPE;

public static final FoldType INNERCLASS_TYPE = JavaElementFoldManager.INNERCLASS_TYPE;

Expand All @@ -68,7 +70,11 @@ public abstract class JavaFoldManager implements FoldManager {

@Deprecated
public static final FoldTemplate CODE_BLOCK_FOLD_TEMPLATE
= new FoldTemplate(CODE_BLOCK_FOLD_TYPE, CODE_BLOCK_FOLD_DESCRIPTION, 1, 1);
= new FoldTemplate(CODE_BLOCK_FOLD_TYPE, CODE_BLOCK_FOLD_DESCRIPTION, 1, 1);

@Deprecated
public static final FoldTemplate METHOD_BLOCK_FOLD_TEMPLATE
= new FoldTemplate(METHOD_BLOCK_FOLD_TYPE, CODE_BLOCK_FOLD_DESCRIPTION, 1, 1);

@Deprecated
public static final FoldTemplate INNER_CLASS_FOLD_TEMPLATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public class JavaElementFoldManager implements FoldManager {
new org.netbeans.api.editor.fold.FoldTemplate(3, 2, "/**...*/")); // NOI18N

@NbBundle.Messages("FoldType_Methods=Methods")
public static final FoldType CODE_BLOCK_FOLD_TYPE = FoldType.MEMBER.derive("method", Bundle.FoldType_Methods(),
public static final FoldType METHOD_BLOCK_FOLD_TYPE = FoldType.MEMBER.derive("method", Bundle.FoldType_Methods(),
new org.netbeans.api.editor.fold.FoldTemplate(1, 1, "{...}")); // NOI18N

@NbBundle.Messages("FoldType_CodeBlocks=Code Blocks")
public static final FoldType CODE_BLOCK_FOLD_TYPE = FoldType.CODE_BLOCK.derive("code-block", Bundle.FoldType_CodeBlocks(),
new org.netbeans.api.editor.fold.FoldTemplate(1, 1, "{...}")); // NOI18N

@NbBundle.Messages("FoldType_InnerClasses=Inner Classes")
Expand Down Expand Up @@ -273,6 +277,11 @@ public FoldInfo createInnerClassFold(int start, int end) {
return FoldInfo.range(start, end, INNERCLASS_TYPE);
}

@Override
public FoldInfo createMethodFold(int start, int end) {
return FoldInfo.range(start, end, METHOD_BLOCK_FOLD_TYPE);
}

@Override
public FoldInfo createCodeBlockFold(int start, int end) {
return FoldInfo.range(start, end, CODE_BLOCK_FOLD_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class JavaFoldTypeProvider implements FoldTypeProvider {
public JavaFoldTypeProvider() {
types.add(JavaElementFoldManager.CODE_BLOCK_FOLD_TYPE);
types.add(JavaElementFoldManager.INNERCLASS_TYPE);
types.add(JavaElementFoldManager.METHOD_BLOCK_FOLD_TYPE);
types.add(JavaElementFoldManager.IMPORTS_FOLD_TYPE);
types.add(JavaElementFoldManager.JAVADOC_FOLD_TYPE);
types.add(JavaElementFoldManager.INITIAL_COMMENT_FOLD_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,11 +1496,16 @@ public FoldingRange createImportsFold(int start, int end) {
public FoldingRange createInnerClassFold(int start, int end) {
return createFold(start, end, FoldingRangeKind.Region);
}

@Override
public FoldingRange createCodeBlockFold(int start, int end) {
return createFold(start, end, FoldingRangeKind.Region);
}

@Override
public FoldingRange createMethodFold(int start, int end) {
return createFold(start, end, FoldingRangeKind.Region);
}

@Override
public FoldingRange createJavadocFold(int start, int end) {
Expand Down

0 comments on commit bb1584c

Please sign in to comment.