Automatic source code formatting with ABAP in Eclipse
With ABAP in Eclipse it is very easy to format the source code of ABAP objects. Pressing Shift + F1 before saving or activating a development object gets your written source code in a pretty format.
But even though it is not a lot of work pressing two buttons before each saving or activation step I think to a certain degree this repetitive task should be automated. I do not worry about the time needed for this manual tasks but more about the situations where the unsteady source code formatting obfuscates the build-in version database or the abapGit history.
Pretty printer is cool, so why not perform it automatically?
Therefore I added a functionality to ABAP in Eclipse to enable automatic source code formatting of ABAP objects (currently classes, function modules and CDS views are supported). The feature is part of the free abapCI Plugin.
One important characteristic of this feature is that it does not format all objects by default, but only that ones that start with a configurable starting source code comment. This comment is configurable and if not changed the default values to be added as first line of your development object are:
- “#autoformat … for classes, testclasses
- *#autoformat … for function modules
- //#autoformat … for CDS views
In the Eclipse preferences there is the possibility to configure and even remove this mandatory comment. But I think this could lead to unwanted formatted development objects (eg. generated data provider classes, legacy source code items with a non SAP conform style,…). Thus I decided to take the defensive path by introducing the mandatory comment … at least for the first release.
To get the automatic ABAP source code formatting feature up and running three steps are necessary.
1. Installing the abapCi Plugin from the Eclipse Marketplace, if not already done.
2. Enabling the corresponding checkbox in the Eclipse Preferences (Window->Preferences)
3. Adding the above mentioned comment as first line of the development objects which should be autoformatted when saved
Starting from this point it is ensured that on each save or activation of the development object the source code formatting is automatically performed.
Hope that helps to save at least some keystrokes or even better to clear up your abapGit or transport history.