Community
Participate
Working Groups
BETA_JAVA7 For the following case void foo() throws Throwable { try (FileReader reader1 = new FileReader("file1"); FileReader reader2 = new FileReader("file2"); FileReader reader3 = new FileReader("file3"); FileReader reader4 = new FileReader("file4")) { int ch; while ((ch = reader1.read()) != -1) { System.out.println(ch); } } } We should give an option to the user under "Line wrapping" to decide how he wants the resources to be wrapped and indented. This would be on the lines of line wrapping for method declaration's parameters.
Created attachment 198240 [details] proposed fix v1.0 + regression tests The patch introduces support for adding formatter options under the "line wrapping" and "white space" tabs in the formatter preferences. The formatting of the try with resources statement is done via a new method org.eclipse.jdt.internal.formatter.CodeFormatterVisitor.formatTryResources(TryStatement, boolean, boolean, boolean, boolean, boolean, int) The default formatting preference (and also the Java conventions prefs) for a try with resources statement is "Wrap all elements, every element on a new line" and default indentation is "Default indentation". Added tests in FormatterRegressionTests#test750()... test765().
Satyam, can you please review? Thanks!
Olivier, Can you review this? I think it is better if you review.
Looks good.
Released in BETA_JAVA7 branch.
The default for DefaultCodeFormatterConstants# FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_TRY is wrong (should be INSERT). The initializations in DefaultCodeFormatterOptions# insert_space_before_opening_paren_in_try also need correction. These workarounds for this bug in FormatterRegressionTests should also be removed: this.formatterOptions.put(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_TRY, JavaCore.INSERT);
(In reply to comment #6) > The default for DefaultCodeFormatterConstants# > FORMATTER_INSERT_SPACE_BEFORE_OPENING_PAREN_IN_TRY is wrong (should be INSERT). Any particular reason why you want a space before the opening paren? Currently, in both java conventions and Eclipse built in profiles, we do not insert a space before opening paren in method/construction declarations, method invocations, etc, although we do insert space for expressions such as for, if, etc.
> Any particular reason why you want a space before the opening paren? Yes, we just follow the Java code conventions: http://www.oracle.com/technetwork/java/codeconventions-141388.html#682 8.2 Blank Spaces: A keyword followed by a parenthesis should be separated by a space. [..] Note that a blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.
(In reply to comment #9) Wow, does Google Chrome duplicate every comment now?
Created attachment 198656 [details] patch to correct spacing The patch also fixes a latent bug. I now pass Alignment.R_OUTERMOST as tie breaking strategy while creating the alignment to make sure formatting of the try with resource clause doesn;t get broken by the local declaration statements constituting it.
Released
Verified with v20110714-1300.