dfmt
dfmt copied to clipboard
Commandline options for partial reformatting
It'd be very nice if dfmt could only selectively reformat a file, without //dfmt off
comments. This is needed for more user friendly editor integration: only reformat stuff related to the current cursor/selection.
clang-format has the options -cursor, -lines, -offset/-length
for this purpose. See http://clang.llvm.org/docs/ClangFormat.html
I have been hacking on dfmt to try and implement -offset/-length functionality, and I got something to work, but... , it needs a more thought-out approach. The problem is that //dfmt on/off
is too crude at the moment, and so I cannot just mimic that functionality. When formatting is disabled, the whole machinery is bypassed that keeps track of indentation etc.
For example (think of the //dfmt on .. //dfmt off
range as the range specified on the commandline):
//dfmt off
void foo() {
int i;
//dfmt on
if (i) {
}
//dfmt off
}
will be reformatted without indenting if()
. However, I think it will make //dfmt on
much more useful, as it will give more fine-grained control. Right now, I guess you can only enable/disable formatting on blocks that do not need indentation at their start.
I have not spent enough time thinking about how to go about this. I.e. what indentation to start with after //dfmt on
? Assume the user was doing "the right thing" before //dfmt on
, i.e. just parse the whole file and start with the indentation that the if()
thing would get when the part before //dfmt on
would be correctly formatted?
A related problem is that running dfmt on a snippet (for example, using vim's formatprg
) gives some annoying messages: (this is from running it on a unittest block)
stdin(9:42)[error]: Declaration expected
stdin(10:40)[error]: Declaration expected
stdin(10:41)[warn]: Empty declaration