Is anyone working on a D source code formatting tool?

Ahmed Fasih via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 16 10:58:42 PST 2015


Hi there, I'm a C++/Python refugee, new to D.

> clang-format seems to do a pretty good job with both of these. 
> Comments seem to be intact unless they're too long, then 
> they're wrapped. It seems to wrap at a space or other 
> non-identifier character. Same thing with expressions that are 
> too long.

I would love such a tool for D, especially based on the ideas of 
clang-format. I first heard about clang-format from a talk by 
Google's Chandler Carruth: the way he said it, the LLVM guys 
looked at what their C++ programmers wasted the most time on, and 
it turned out to be whitespace, surprisingly enough. So they 
implemented was is essentially LaTeX for source code (optimal 
placement of spaces and line breaks using Djikstra's algorithm, 
the works). And he said it changed the way he codes, and at that 
point I had to go get it, and I 100% agree with him: it's awesome 
to have.

It works with Vim, Emacs, Visual Studio, Sublime, etc., because 
it provides a simple Python wrapper to the executable that 
anything can call any time to format any code. It works with 
C/C++, Objective-C, and Javascript; when I discovered it could do 
JavaScript, my use of that language rose substantially (could be 
coincidence :).

Reading some of the discussion here about whether to integrate it 
into the compiler, etc., makes me realize that one of the nice 
things about clang-format is real-time interactivity, i.e., I can 
type-type-type code quickly and without bothering with 
whitespace, just getting the idea out of my head and into the 
editor, then when I reach a breathing space I can hit a keycombo, 
and my editor reformats the block I just typed. I find that the 
hits to my flow are much smaller than having to do this manually, 
and I think that's what Carruth meant when he said it changed his 
coding.

Another nice thing about it is that it's fully parameterized (how 
many spaces, whether to indent this, what penalty to assign that, 
etc., example at [1]). It can search the current and parent 
directories for a .clang-format file with those parameters. It 
can also generate stock .clang-format files conforming to various 
coding styles, viz., LLVM, Google, Mozilla, WebKit, & Chromium. I 
throw this into my Git repos; happiness ensues.

Hope this helps outline the paths others have taken!

[1] Example Javascript .clang-format file: 
https://github.com/fasiha/kanjiwild/blob/gh-pages/.clang-format


More information about the Digitalmars-d mailing list