Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 440778

Summary: [formatter] Adding line comment trips up code indentation
Product: [Eclipse Project] JDT Reporter: Luke Hutchison <luke.hutch>
Component: CoreAssignee: Mateusz Matela <mateusz.matela>
Status: VERIFIED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: daniel_megert, dusisarath, jarthana, jelle, manoj.palat, mateusz.matela
Version: 4.4   
Target Milestone: 4.6 M7   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
The same formatting examples, but without the line wrapping
none
Formatter configuration which exposes the indentation issue none

Description Luke Hutchison CLA 2014-07-30 09:36:55 EDT
Version: Luna Release (4.4.0)
Build id: 20140612-0600


For the following method:


    @SuppressWarnings("unchecked")
    public <T> ClasspathScanner addSubclassMatcher(final Class<T> superclass, final ClassMatchProcessor<T> classMatchProcessor) {
        classMatchers.add(() -> {
            ClassInfo superclassInfo = classNameToClassInfo.get(superclass.getName());
            boolean foundMatches = false;
            if (superclassInfo != null) {
                for (ClassInfo subclassInfo : superclassInfo.allSubclasses) {
                    try {
                        Class<? extends T> klass = (Class<? extends T>) Class.forName(subclassInfo.name);
                        classMatchProcessor.processMatch(klass);
                        foundMatches = true;
                    } catch (ClassNotFoundException | NoClassDefFoundError e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            if (!foundMatches) {
                // TODO: Log.i("No classes found with superclass " + superclass.getName());
            }
        });
        return this;
    }


If I insert a line comment inside the try block, the indentation gets screwed up from that point on:


    @SuppressWarnings("unchecked")
    public <T> ClasspathScanner addSubclassMatcher(final Class<T> superclass, final ClassMatchProcessor<T> classMatchProcessor) {
        classMatchers.add(() -> {
            ClassInfo superclassInfo = classNameToClassInfo.get(superclass.getName());
            boolean foundMatches = false;
            if (superclassInfo != null) {
                for (ClassInfo subclassInfo : superclassInfo.allSubclasses) {
                    try {
                        // Only now do we call the classloader (Class.forName()), it is slow
                Class<? extends T> klass = (Class<? extends T>) Class.forName(subclassInfo.name);
                classMatchProcessor.processMatch(klass);
                foundMatches = true;
            } catch (ClassNotFoundException | NoClassDefFoundError e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (!foundMatches) {
        // TODO: Log.i("No classes found with superclass " + superclass.getName());
    }
})      ;
        return this;
    }
Comment 1 Luke Hutchison CLA 2014-07-30 09:39:18 EDT
Created attachment 245529 [details]
The same formatting examples, but without the line wrapping
Comment 2 Luke Hutchison CLA 2014-07-30 09:43:33 EDT
Turns out it is the lambda that is causing the formatting issue with line comments. If you insert a line comment below the "() -> {" line, everything indents. if you insert it after the "if (superclassInfo != null) {" line, nothing happens. If you insert it after the "for" or "try", everything outdents.
Comment 3 Jay Arthanareeswaran CLA 2014-08-04 00:02:42 EDT
Manoj, please investigate and see how critical it is.
Comment 4 Luke Hutchison CLA 2014-09-04 07:04:02 EDT
Here are two standalone examples of this problem. Both are exactly as formatted by eclipse with default settings. The only difference between the first case and the second is that the second case has an additional comment after the "try {" line. Indentation is wrong in both cases, but the first case has a lot more wrong with it.


public class Test {
    @FunctionalInterface
    public static interface Z {
        public void z(String a, String b, String c);
    }

    private static void y(String s, Z object) {
    }

    public static void main(String[] args) {
        // comment
        y("hello", (a, b, c) -> {
            // comment
                try {
                    if (a == null) {
                        // comment
                throw new IllegalArgumentException();

            } else if (a.equals("test")) {
                System.out.println("a equals \"test\"");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // comment
    }   );
        // comment
    }
}


public class Test {
    @FunctionalInterface
    public static interface Z {
        public void z(String a, String b, String c);
    }

    private static void y(String s, Z object) {
    }

    public static void main(String[] args) {
        // comment
        y("hello", (a, b, c) -> {
            // comment
                try {
                    // comment
                if (a == null) {
                    // comment
                    throw new IllegalArgumentException();

                } else if (a.equals("test")) {
                    System.out.println("a equals \"test\"");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            // comment
        });
        // comment
    }
}
Comment 5 Jelle Raaijmakers CLA 2015-05-05 11:48:25 EDT
Created attachment 253185 [details]
Formatter configuration which exposes the indentation issue

Lambda methods with line comments expose the bug. Setting the default indentation to 1 functions as a workaround. Attached is the formatter profile I use that exposes this bug.
Comment 6 Mateusz Matela CLA 2016-04-13 16:54:20 EDT
This problem no longer occurs after the formatter redesign.

*** This bug has been marked as a duplicate of bug 303519 ***
Comment 7 Dusi Sarath Chandra CLA 2016-04-27 05:34:09 EDT
Verified using Eclipse Version: Neon (4.6)
Build id: I20160425-1300