Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folding mechanism for while/for/if/switch-case #1562

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jakub-suliga
Copy link

@jakub-suliga jakub-suliga commented Jul 31, 2024

Problem:
Currently, there is no folding mechanism for while/for/if/switch-case statements in Eclipse. VSCode and IntelliJ support a folding mechanism for these statements. Therefore, I have created a method that implements this folding mechanism. This is also an open issue: #1426. In addition, I deleted some commented-out code from 2007.

Before:
image

After:
image

How to test:

  1. Start an Eclipse workspace and create a new class.
  2. Add while/for/if/switch-case statements to this class.
  3. Verify that the folding mechanism works as expected.

Here is a small program you can use to test:

private int zaehler;
private final Object sperre = new Object();


public void test() {
    if (zaehler > 0) {
        System.out.println();
    } else if (zaehler == 0) {
        System.out.println();
    } else {
        System.out.println();
    }

    while (zaehler < 10) {
        zaehler++;
    }

    for (int i = 0; i < zaehler; i++) {
        System.out.println();
    }

    do {
        zaehler--;
    } while (zaehler > 0);

    int[] zahlen = {1, 2, 3, 4, 5};
    for (int zahl : zahlen) {
        System.out.println();
    }

    switch (zaehler) {
        case 0:
            System.out.println();
            break;
        case 1:
            System.out.println();
            break;
        default:
            System.out.println();
            break;
    }

    try {
        int ergebnis = 10 / zaehler;
    } catch (ArithmeticException e) {
        System.out.println();
    } finally {
        System.out.println();
    }

    synchronized (sperre) {
        zaehler++;
    }

    Runnable runnable = () -> {
        System.out.println();
    };
    runnable.run();

    {
        System.out.println();
    }

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i * j > 6) {
            }
            System.out.println();
        }
    }

    try {
        String text = null;
        text.length();
    } catch (NullPointerException e) {
        System.out.println();
    } catch (Exception e) {
        System.out.println();
    } finally {
        System.out.println();
    }

    {
        System.out.println();
    }
    if (zaehler > 0) {
        for (int i = 0; i < zaehler; i++) {
            while (i < 5) {
                System.out.println();
                i++;
            }
        }
    }

    lambdaVerwenden();
}

public void lambdaVerwenden() {
    verarbeiteLambda(() -> {
        System.out.println();
    });
}

private void verarbeiteLambda(Runnable runnable) {
    runnable.run();
}

Bugs:

@jakub-suliga jakub-suliga marked this pull request as draft July 31, 2024 07:29
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 2 times, most recently from 08cd1d9 to afd4aed Compare September 25, 2024 07:09
@jakub-suliga
Copy link
Author

jakub-suliga commented Sep 25, 2024

Bug:

  • When you press enter or delete a line and the element is closed, the folding mechanism gets buggy

@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 2 times, most recently from c2082f5 to 0390f1c Compare October 2, 2024 12:14
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 10 times, most recently from c1ac59f to 80ee66d Compare October 8, 2024 09:00
@jakub-suliga jakub-suliga marked this pull request as ready for review October 8, 2024 11:59
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 3 times, most recently from d5cf645 to a44a7e0 Compare October 8, 2024 17:24
@fedejeanne
Copy link
Contributor

The test failures seem to be unrelated: https://ci.eclipse.org/jdt/job/eclipse.jdt.ui-github/job/PR-1562/21/testReport/

The only test that fails with age == 1 (i.e. in this PR) is org.eclipse.jdt.text.tests.PluginsNotLoadedTest.pluginsNotLoaded but looking at its stack trace I don't see any hints that point to this PR either:

java.lang.AssertionError: 
Wrong bundles loaded:
- org.eclipse.jdt.junit
 expected:<0> but was:<24>
	at org.junit.Assert.fail(Assert.java:89)
	at org.junit.Assert.failNotEquals(Assert.java:835)
	at org.junit.Assert.assertEquals(Assert.java:647)
	at org.eclipse.jdt.text.tests.PluginsNotLoadedTest.pluginsNotLoaded(PluginsNotLoadedTest.java:278)
...

Does anyone know the reason for this failure?

In the meantime @jakub-suliga, try with another force-push to re-trigger the checks and see if the failure persists.

@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch from a44a7e0 to b49f6c9 Compare October 9, 2024 07:46
@jakub-suliga jakub-suliga marked this pull request as draft October 9, 2024 08:15
@jakub-suliga jakub-suliga marked this pull request as ready for review October 9, 2024 08:16
@fedejeanne
Copy link
Contributor

fedejeanne commented Oct 9, 2024

In the meantime @jakub-suliga, try with another force-push to re-trigger the checks and see if the failure persists.

On a closer look, the test failures are because of the changes introduced in this PR. Run them locally and you will see it.

Here's (part of) the console output when running org.eclipse.jdt.ui.tests.quickfix.AnnotateAssistTest1d5.testAnnotateReturn2():

!STACK 0
java.lang.ClassCastException: class org.eclipse.jdt.internal.core.ClassFile cannot be cast to class org.eclipse.jdt.core.ICompilationUnit (org.eclipse.jdt.internal.core.ClassFile and org.eclipse.jdt.core.ICompilationUnit are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @21452f5e)
	at org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider.computeFoldingStructure(DefaultJavaFoldingStructureProvider.java:981)
	at org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider.update(DefaultJavaFoldingStructureProvider.java:907)
	at org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider.initialize(DefaultJavaFoldingStructureProvider.java:852)
	at org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider.handleProjectionEnabled(DefaultJavaFoldingStructureProvider.java:822)
	at org.eclipse.jdt.ui.text.folding.DefaultJavaFoldingStructureProvider$ProjectionListener.projectionEnabled(DefaultJavaFoldingStructureProvider.java:700)
...
	at org.eclipse.jdt.ui.tests.quickfix.AnnotateAssistTest1d5.testAnnotateReturn2(AnnotateAssistTest1d5.java:178)
...

It seems the fInput isn't always an ICompilationUnit as the new code assumes. You'll have to "fork" the execution (with an if (fPart instanceof ICompilationUnit)) and preserve the old code too (in the else block).

It would be interesting to know when exactly is fInput not and ICompilationUnit and if it's possible to turn them into ICompilationUnits (in a separate PR). By the looks of it, it might be when one opens a .class file, probably one that displays source code, like java.lang.String:

image

@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 3 times, most recently from d68c88b to baf2632 Compare October 30, 2024 06:52
@fedejeanne
Copy link
Contributor

@iloveeclipse : Jakub and I were looking into the failed tests and we found the culprit: it was a null. After some debugging and evaluating we decided that a null-check would make sense since now (in this PR) it is possible to add folding structures for things that are not IJavaElements. This led to commit ade7b52.

Do you see any possible problem with this change? We couldn't come up with any but maybe you can?

@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch from 51c2222 to b74173b Compare November 6, 2024 20:17
@jakub-suliga
Copy link
Author

jakub-suliga commented Nov 6, 2024

Open regressions:

  • Inner classes do not have a folding button.
  • if statements without braces do not fold properly.
  • Javadoc comments do not create a folding region, when under the javadoc is another javadoc
    image
  • In a switch-case statement, when one case section immediately follows another, the editor creates a folding button that encompasses both sections. However, clicking the first folding button doesn't have any effect.
  • Visual bug:
    image
  • Missing folding Button for import

@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 7 times, most recently from 85e9e56 to 36a0edb Compare November 8, 2024 11:53
@jakub-suliga jakub-suliga marked this pull request as draft November 13, 2024 07:28
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 4 times, most recently from 6695167 to dde4098 Compare November 20, 2024 09:44
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 2 times, most recently from eb70ef7 to 9ed1a12 Compare November 29, 2024 07:43
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 4 times, most recently from b6d51c5 to 35b3e87 Compare December 4, 2024 16:58
@jakub-suliga jakub-suliga marked this pull request as ready for review December 4, 2024 17:49
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 2 times, most recently from ee3706a to 4602a47 Compare December 6, 2024 07:44
@jakub-suliga jakub-suliga marked this pull request as draft December 6, 2024 11:52
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch 2 times, most recently from 78b3977 to d7b0360 Compare December 6, 2024 12:34
@jakub-suliga jakub-suliga force-pushed the feature/foldingStrucktur branch from d7b0360 to bc2e727 Compare December 13, 2024 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants