Bug 568941 - [formatter] Put ( on separate line from try
Summary: [formatter] Put ( on separate line from try
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.18   Edit
Hardware: PC Windows 10
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-18 17:19 EST by Nathan Reynolds CLA
Modified: 2023-02-24 07:08 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nathan Reynolds CLA 2020-11-18 17:19:36 EST
In Preferences > Java > Code Style > Formatter, I can edit the format.  In the Parentheses positions section, there is an option for try clause.  I set that to "Separate lines" and the formatter leaves the ( on the same line as the "try".  For example, I would like the formatter to change the code from this...

try (
   ...
)
...


... to this ...

try
(
   ...
)
...

Please let me know what settings to change to accomplish this.  If this is not possible, please add the ability.
Comment 1 Mateusz Matela CLA 2020-11-19 12:56:17 EST
There's no such setting at the moment.
I guess it could fit into the New Lines > In control statements section.

But I'm not convinced the option would be useful/popular enough to implement it.

And it would be a weird outlier - why would you put a line break before opening parenthesis in try statements, but not in catch statements, for loops, or method arguments lists for that matter?
Comment 2 Nathan Reynolds CLA 2020-11-19 16:35:59 EST
> Why would you put a line break before opening parenthesis in try statements, but not in catch statements, for loops, or method arguments lists for that matter?

Some times try-with-resources blocks have 1 declared and initialized variable.  Depending on the length of the initialization code, this might all fit nicely on 1 line.

Some times try-with-resources blocks have 3+ declared and initialized variables.  The code is much easier to read if all of these initialization statements are on each line.  As such, the code formatter does this.

try (
   InputStream file = Files.newInputStream(...);
   InputStream buffer = new BufferedInputStream(file);
   Reader reader = new InputStreamReader(buffer, ...);     
)
{
   while (true)
   {
      line = reader.readLine();

      ...
   }
}

Notice how the ( does not match the Allman style of {}.  By implementing this feature request, the ( matches the Allman style.

try
(
   InputStream file = Files.newInputStream(...);
   InputStream buffer = new BufferedInputStream(file);
   Reader reader = new InputStreamReader(buffer, ...);     
)
{
   while (true)
   {
      line = reader.readLine();

      ...
   }
}

What about catch statements?

Typically there are not very many exceptions in the catch.  If there were, then I would probably want each exception on its own line as such...  (Note: This is a contrived example.  I am not sure where these exceptions could all be caught in real code.)

catch
(
   ExecutionException |
   InterruptedException |
   InvocationException |
   IOException |
   ParseException |
   URISyntaxException e
)
{
   ...
}

As for "for" loops, the 3 parts of the for loop are rarely long enough to cause a problem for wrapping.

As for method parameters, I think the formatter does this already... (Note: This example has several other problems in it.  Please ignore that and just focus on the wrapping.)

public void myMethod
(
   String arg0,
   String arg1,
   String arg2,
   String arg3,
   String arg4,
   String arg5,
   String arg6,
   String arg7,
   String arg8
)
{
   ...
}
Comment 3 Mateusz Matela CLA 2020-12-19 14:46:31 EST
(In reply to Nathan Reynolds from comment #2)
> As for method parameters, I think the formatter does this already... 

AFAIK this is not possible.
Maybe you've found some workaround if you don't have this problem? Maybe it could also be applied to try statements? :)

I guess a new checkbox in the "Parenthesis position" section called "Insert new line before opening parenthesis when separating lines" would be general enough and not too much clutter.
But I wonder how many people would want to use it... I would be more convinced if other IDEs made it possible or there were some questions about it around the internet.
Comment 4 Eclipse Genie CLA 2023-02-24 07:08:22 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.