the blog
Back to -Blog

ActionScript Smart Code Formatting

More options coming in beta 4
by Huw Collingbourne
Tuesday 10 March 2009.

Code formatting is a very personal thing. Some people like to have opening curly brackets on new lines, others insist that curly brackets must go on the same line as the code preceding them. Maybe your obsession is spacing - some people like spaces between the function name and arguments lists, others like spaces between the opening parenthesis and the first argument - and so on...

The current beta of Amethyst has powerful auto-code-formatting. You can set a large range of options to format code with consistent spacing, newlines and indentation. For example, you might set all the following options:

- indent case contents (true)
- indent case labels (false)
- open brace on newline for methods (false)
- open brace on newline for control blocks (false)
- space between method name and parenthesis in definition (false)
- space before and after assignment operators (false)
- space between ‘(‘ ‘)’ and arguments (false)

In which case, when you reformat your code, you will see something like this:

Now if you reverse all the above options, this is what you get:

There are many more options too. So if your own code starts to get untidy or if you are working on someone else’s code which has been formatted in a way that is not to your taste, you can easily reformat an entire document or a selected block of code by setting the options and pressing the relevant hotkeys (the defaults are CTRL+E,D to reformat a document and CTRL+E,F to reformat a selected block - or use the items on the Edit, Advanced menu).

- For a comprehensive list of the code formatting options in Amethyst beta 3 see: ActionScript Automatic Code Formatting

Beta 4 of Amethyst, which will be released in a couple of weeks, will have even more code formatting options. Its code formatting will also be considerably faster and slicker (for example, in beta 3 the cursor moves when you reformat a document; in beta 4 it stays put!)

One of the things we are also working on is making the code formatting more intelligent. For instance, it is quite commonplace when creating complex structures such as arrays or object literals, to align items in columns for the sake of clarity. Beta 3 does not respect such alignment. It will apply consistent spacing which may sound good in principle but, unfortunately, this has the side-effect of unaligning your carefully aligned columns. In beta 4, however, your columns will be preserved.

In beta 4, item alignment of object literals in columns (as here) is preserved

Then again, there may be specific bits of code that you absolutely do not want to be reformatted - ever. Beta 4 will introduce a new directive to turn off formatting for marked areas. These directives are //#f- to turn off formatting and //#f+ to turn it back on again. For example, if (for some reason) I decided that I wanted the first two ‘case’ items in a ‘switch’ block to be formatted differently from all other items, I could format those lines by hand and then mark them with directives to prevent them from being auto-reformatted, like this:

As I said, this is just one of the code formatting additions which will be added to beta 4. We’ll go through the other options when that beta is released. In the meantime, be sure to read the guide to the current code formatting options. And if you have any requests for specific formatting features, please let us know by emailing the support address on our Contacts Page.

Bookmark and Share   Keywords:  Amethyst  development
© SapphireSteel Software 2014