# Improving the source-code indentation in Wolfram Workbench

One thing that I like about the more recent versions of the Mathematica front-end is that I think it does a good job indenting code. The language's functional nature, in my experience, leads to a lot of nesting, and the small indentation works well for this pupose. However, for some reason, the rules used for Wolfram Workbench (at least as of version 2.0) are evidently completely different, and the results look much worse. This can be helped somewhat by adjusting editor settings for Eclipse, but it still needs a lot of work. For example, here's a really silly approach for determining whether an integer is even or not, which I picked because it has a lot of indentation, as it's formatted in a Mathematica notebook:

Module[{even, odd, withoutLimit},
even[0] = True;
odd[0] = False;

even[n_Integer] := odd[n - 1];
odd[n_Integer] := even[n - 1];

Attributes[withoutLimit] = {HoldFirst};
withoutLimit[expr_] :=
Block[{$IterationLimit = Infinity}, expr]; With[{test = Function[n, n >= 0]}, PillsyEvenQ[n_Integer] := withoutLimit@even[n] /; test[n]; PillsyEvenQ[n_Integer] := even[-n]; PillsyOddQ[n_Integer] := withoutLimit@odd[n] /; test[n]; PillsyOddQ[n_Integer] := odd[-n]]];  I find this quite readable. Here's the same code, copied into Workbench and formatted using its automatic source formatting (I added some whitespace between logically related definitions, which I'm used to doing manually): Module[ {even, odd, withoutLimit}, even[0] = True; odd[0] = False; even[n_Integer] := odd[n - 1]; odd[n_Integer] := even[n - 1]; Attributes[withoutLimit] = {HoldFirst}; withoutLimit[expr_] := Block[ {$IterationLimit = Infinity},
expr
];

With[ {test =
Function[n,
n >= 0]},

PillsyEvenQ[n_Integer] :=
withoutLimit@even[n] /; test[n];
PillsyEvenQ[n_Integer] :=
even[-n];

PillsyOddQ[n_Integer] :=
withoutLimit@odd[n] /; test[n];
PillsyOddQ[n_Integer] :=
odd[-n]
]
];


I'd like to make the second example look a lot more like the first example. In particular, the insistance that trailing square brackets from With, Module and Block should get lines all to themselves, like they're curly braces from C, makes me crazy.

-
Heh, I dislike the front end's formatting and always work with auto indent off (I do keep auto spaces on though)... To each his own :) – R. M. Sep 4 '12 at 18:44
Related question. Actually, improving my code formatter presented there is on the top of my to-do list. It will be possible to customize it to format in very different styles. It is so far FE-based only, but it should be possible to port it to say Java and make an Eclipse plugin to be used in WB. I may consider doing this if there is enough interest in the community. – Leonid Shifrin Sep 4 '12 at 18:53
@LeonidShifrin Speaking of which, what do you think of converting that post into a blog post? It would be good to focus on the design aspects and challenges and throw in a link to your post here and to github for the bulk of the code. It would be a great post to show how one can extend Mathematica using Mathematica in ways that might seem unimaginable (at least, it seemed that way to me). Since it is mostly written up, it should only require some minor reorganization and polishing. Of course, only when you're back from your vacation :) – R. M. Sep 4 '12 at 19:07
@R.M Superb idea! I had to be brief (by my standards :-) ) in my SE post, and I can include some more stuff in the blog post. The only thing is, I want to improve the formatter first - currently there are too many unfixed bugs and cases it does not handle correctly. Also, I want to do a serious refactoring and introduce a tiny DSL which would allow the user to write small scripts to customize the formatter. But this won't take that long, and meanwhile I will make the post on Bessel functions, which is really mostly ready. I can make the formatter my second blog post, after that one. – Leonid Shifrin Sep 4 '12 at 19:13
@LeonidShifrin Fantastic! Please ping me when you're ready. I believe you already have an account set up, which should let you start writing, but I'll be glad to help with formatting and such (it is slightly different on the Wordpress platform... specifically, stick to the HTML editing mode and not visual mode). I have admin access on the blog, so I'll publish it when it's all set. I'll remove my comments here soon. We can continue this further, if necessary, in the blog chatroom :) – R. M. Sep 4 '12 at 19:16