Next, be sure to add this configuration setting. Then the Format Document command and keys will work with Salesforce Apex. I believe you can also accomplish the same thing with the Java language support, but this is working for me.
Simply, it's needed to install uncrustify plugin on visual studio code that give you the possibility to indent apex code using the shortcut CTRL + ALT + F .The linked guide explains how to proceed step by step
I didn't want to use Crustify, which most of the other answers suggested. However, I came across the following for sfdx and VS Code. It is prettier, with a (possibly community contributed) Apex plugin:
d3342ee215