# 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

This isn't a complete answer to your question, but it is worth noting that there are several preference panes in Wolfram Workbench that allow you to tweak the automatic formatting you get when you right-click (control-click) on some selected code and choose Source > Format. Both are accessed (under Windows) from the Window menu under the Preferences dialog.

The first one controls "Smart" formatting of Mathematica code. You can try tweaking some of these settings, but I haven't found one that stops the closing bracket being on a new line. I think this is pretty standard code formatting in other languages, so I'm not surprised it's enforced. This is not the default setting - I turned off the checkmarks for the curly braces. Turning them all off gave even worse results as far as I could tell. You could try turning off Smart Indenting altogether.

There is also a more general code formatting preference pane, which Mathematica mode also supports. I've reduced the indenting to three spaces instead of the usual four.

-
This is what I tried fiddling with when I saw the question... it had no effect on the indentation on my machine (Mac). I stopped trying further because I don't have much experience with the WB – R. M. Sep 4 '12 at 22:32
Changing these settings does change things, but not in the way Pillsy wants. I think the only answer that could be accepted is "this isn't possible unless Leonid comes up with a clever plugin". – Verbeia Sep 4 '12 at 22:37
Ok, so must be something messed up on my system. I wouldn't be surprised... :) – R. M. Sep 4 '12 at 22:48
@rm-rf Try getting the computer out of the pond – belisarius has settled Oct 4 '12 at 23:10