Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Code-Formatting in Eclipse by HotKey

I knew one can prevent the code formatting in eclipse by surrounding the code with

// @formatter:off
// my awesome formatted code...
public Set<Person> transformCustomersToPersons(List<Customer> customers){

    Set<Person> persons = new HashSet<Person>();
    for (Customer customer : customers) {
        persons.add(new Person(
                customer.getFirstname(),
                customer.getLastname(),
                customer.getTitle(),
                customer.getBirthday(),
                customer.getAddress(0).getCity(),
                customer.getAddress(0).getStreet(),
                customer.getAddress(0).getHousenumber(),
                customer.isMan() ? "m" : "w",
                customer.getCardNumber())
        );
    }
    return persons;
}
// @formatter:on

But I don't want to write it manually all the time. It would be perfect just mark the code, press a hotkey and the code gets prevented from formatting.

Anybody knows how? Thx!

like image 319
michael Avatar asked Jan 09 '14 12:01

michael


People also ask

What is the shortcut to format code in Eclipse?

To format your whole script: Open the required file. Go to Source | Format Document or press Ctrl+Shift+F.

How do I turn off auto indent in eclipse?

Preferences-> C/C++-> Editor-> Typing-> Automatically indent-> UNCHECK "new lines and braces" 4. Click mouse beyond end of any indented source-code line.

What is auto format in Eclipse?

Importing preferences for automatic formatting. Eclipse has the capability to do so some automatic formatting whenever a file that is being edited is saved. This automatic formatting can make programs more readable and understandable.


1 Answers

Yes, that is possible:

Create new template: Windows->Preferences->Java->Editor->Templates->New (screenshot)

enter image description here

Give a name to the template something like non-formater and put the Pattern:

   // @formatter:off
   ${line_selection}${cursor}
   // @formatter:on

Now, you can select the text and type Alt+Shift+Z and a number of the template - here 4

enter image description here

like image 192
Mark Avatar answered Sep 28 '22 09:09

Mark