From the course: Eclipse Essential Training

Applying and customizing formatting - Eclipse Tutorial

From the course: Eclipse Essential Training

Applying and customizing formatting

- Formatted code is always easier to read, and sometimes when you're working in a project, you'll open up a code file like this, unused imports and formatting all over the map. So how do you fix it quickly and easily leveraging Eclipses power? One way to do that is by customizing your formatting in eclipse settings. So open it up under Eclipse settings, which is the Mac location, and we talked about the Windows location in a previous movie. So once you open that up, filter for formatting. Once you open that up, filter for format. You'll notice that the different languages have different format settings, and that's really important to note. So if you fix it for one language, you'll have to do the same thing for another language in the future, if you want formatting for that language. So under code style and Java, I'm going to hit formatter. And you'll see that there's an active profile. It says Eclipse built in. If I hit the drop down, I can choose Java conventions or something else. And I can get a preview of what the formatting is going to look like. If I want to import an existing coding standard, like maybe there's a standard from another company like Google who puts out their own coding standards. You could import that through this import button here and just find that XML file and bring it in. If you want to edit the settings of a profile, just hit the edit button and you'll see all of the controls that you have here, including editing, indentation, line breaks, et cetera. You'll need to expand them first of course, by clicking the triangles on the left, I'm going to leave these as is and cancel out. And then I'm going to head over to save actions under editor. Again, this is specific on a per language basis, so changing this only changes it for the Java language. So with save action selected, I can check this box to perform the selected actions on save. This can format my code when I save the file if I choose to do it. So I'll have to click that box and notice there's two options. Format, all lines and format edited lines. All lines is my preference. But if you're working in a giant file on a large team you might want to format edited lines only. I also have organized imports selected, which will fix unused imports and organize them as well. If you want to modify the settings for that, you can click on organize imports here. I'll go back to save actions for Java, and if you want to, you can add additional actions by checking this box and hitting the configure button. I'll just cancel out of that and then uncheck the box. So with all these set, my save actions for Java are set, and I'll hit apply and close. Now, I'll make some kind of change to the file, like adding space, and then press the keyboard shortcut to save the file, and then you'll see that it's all fixed. So by using your formatting settings and save actions, you can more easily format your code so that it's consistent across all your files.

Contents