27

When I reformat code with Eclipse, it turns method headers like this:

public void myMethod(String param) {

into method headers like this:

public void myMethod(
                     String param) {

When I was brought on here I'd never used Eclipse before, and I imported project settings provided by someone else. I have seen that on small new projects I've worked on Eclipse does not do this, so it must be in the settings I've imported. But I've gone through every panel I can find, as well as every hidden file I can find in the workspace, and I can't figure out what is causing this.

How do I turn it off? I don't want a newline before parameters in my method signatures, and I can't imagine why anyone would!

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
skiphoppy
  • 97,646
  • 72
  • 174
  • 218

5 Answers5

52

Have a look at Window>Preferences>Java>Code Style>Formatter.

There you can configure almost everything. Your case is found at

Line Wrapping>Method Declarations>Parameters.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
tangens
  • 39,095
  • 19
  • 120
  • 139
  • 1
    You may also want to check under the Project Preferences by right-clicking on the project. There could be a project specific formatter selected. – RC. Oct 21 '09 at 16:01
  • 1
    I tracked this down, and it displays what appears to be proper code style to me: void foo(int arg1 // no newline It appears to be following a "Always wrap first element, others when necessary" policy, but the actual setting is "Wrap only when necessary." – skiphoppy Oct 21 '09 at 16:10
  • 1
    Project settings display the wrong formatting I'm experiencing, but they are also set to "Wrap only when necessary." It says the project settings are an "unmanaged profile." – skiphoppy Oct 21 '09 at 16:17
  • 1
    I switched to not use project settings, bringing me back to Eclipse builtins. This looks nice for me. :) I think I may have to change the tab/indenting policy. Thanks! – skiphoppy Oct 21 '09 at 16:19
  • 1
    Someone may come back and make me use the project settings again, if so I'd like to figure out how to get that setting to actually follow "Wrap only when necessary" policy. But beats me how to do it... Hopefully I can get away with the Eclipse formatting policy for now. :) – skiphoppy Oct 21 '09 at 16:20
  • 3
    Perhaps it's really necessary because your line width is to small (same dialog, tab "line wrapping"). – tangens Oct 21 '09 at 16:24
  • how to edit this for php/html `code style`? i see there far less options to format the code. eclipse version 3.7.2 for linux – ulkas Aug 20 '14 at 08:53
  • I am using `STS v3.9.6.RELEASE` and I have a `formatter.xml` file that I want to add to it. `Window` > `Preferences` > `Java` > `Code Style` > `Formatter`, here when I click `Import...` and select file I don't see any change. How do i achieve this? – Nisarg Patil Jan 09 '19 at 07:43
5

In my version of Eclipse, I found the option under Window->Preferences->Java->Code Style->Formatter, then clicked the Edit.. button.

In the new window, go to the Line Wrapping tab and find Method Declarations. You want to change the Line wrapping policy. The Force split check box seems to do the same.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Colin Gislason
  • 5,449
  • 2
  • 27
  • 22
4

Go into preferences: Java -> Code Style -> Formatter

Restore Defaults or edit what's there.

Additionally, Code Style -> Code Templates will allow you to define generate code formatting.

Note that this is for Eclipse Workspace as a whole, the same Preferences can be accessed under the project preferences if you want to get more fine grained at a per project level.

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
MarkPowell
  • 16,482
  • 7
  • 61
  • 77
  • Picture by picture tutorial can be found here: http://www.wikihow.com/Change-the-Default-Format-Settings-in-Eclipse – Igor L. Aug 24 '13 at 09:02
3

In eclipse you can define your code formatting use Code Style ,

for MAC System :--> Eclipse --> Preference -- > java--> code Style

for Window System :--> Window->Preferences->Java->Code Style->Formatter

M. A. Kishawy
  • 5,001
  • 11
  • 47
  • 72
Imran khan
  • 819
  • 3
  • 12
  • 24
1

The best way to change your format is to go to Window-> Preferences-> Java-> Code Style-> Formatter. Inside the formatter window, eclipse has a couple of built in styles, but the best way is create a new profile of your own by pressing on New. You can change the format as you please on the edit tab

As shown in the image

You can change parentheses and curly braces positioning along with other stuff.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107