I discovered that AVR Studio 5.1 doesnt always work in relation to code formatting. I searched for bugs and found a number of places where Atmel has replied that they are working on fixing this. However, until this happens a nice work around using an external tool can be used.
This is how to setup the external tool AStyle which is on SourceForge and free.
- Download AStyle and unzip it somewhere you like
- Open AVR Studio 5
- Go to: Tools->External Tools…
- Add a tool and fill in the fields:
- Title = AStyle
- Command: full path to C:\\…\AStyle.exe
- Arguments: -y –indent=tab –style=ansi –errors-to-stdout –suffix=none $(ItemDir)$(ItemFileName)$(ItemExt)
- If you like the code formatted differently you can check the AStyle documentation
- And finally check the Use Output Window, so it doesnt popup a new windows every time you run it.
- Now we need to setup a key binding for it like Ctrl+Shift+F and make the document reload automatically when formatted
- Goto Tools->Options
- Locate Environment->Keyboard in the left tree
- Then fund the command: Tools.ExternalCommand1 (assuming you only have one external tool specified)
- Assign a Ctrl+Shift+F to it as global. And the key binding is complete.
- Locate Environment->Document
- Check “Auto-load changes, if saved”
- That’s it enjoy