[cfe-dev] clang-format leading whitespace

Daniel Jasper via cfe-dev cfe-dev at lists.llvm.org
Sun Dec 10 09:06:34 PST 2017


On Tue, Nov 14, 2017 at 1:42 AM, Samuel Williams via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Thanks for your feedback, and I appreciate the link and the logic behind
> it.
>
> Unfortunately, by stripping all trailing whitespace, clang-format has made
> a policy decision, but with no way to control it via the configuration. I
> understand it's not desirable for every option to be configurable, but this
> is something which significantly affects existing code. I propose that it
> would be good to have an option "TrailingWhitespace:
> Truncate|Preserve|Fix". Truncate would be existing behaviour. Preserve
> would not alter leading whitespace on blank lines or touch trailing
> whitespace. Fix would fix leading whitespace on blank lines, fix trailing
> whitespace on code, but avoid making any changes in comments except if it
> was reflowed. It could be more granular but that's where I'd start.
>
> ## Some objective problems:
>
> - Existing code which is indented this way would require a lot of changes.
>

That's true for any project starting to use clang-format. That's why we'd
suggest to use editor and version control system integrations and only
format the lines that are touched.


> - Markdown (which is useful in comments) is sensitive to trailing
> whitespace.
>

I haven't seen a project using markdown in comments and relying on the
trailing whitespace. However, even if some do, I think it'd be better for
clang-format to recognize this and not remove spaces for those comments
specifically.

It's possible to add a post process script to normalise leading whitespace
> but not trailing whitespace (e.g. in the case of markdown).
>
> ## Some subjective problems:
>
> - It's my preference when I start editing on a line, the indentation is
> correct already.
>

How often do you even start editing in a currently empty line? Don't you
normally start with some existing code and have to insert a line break?
clang-format wouldn't help there..

(It's also a really easy habit to loose once you have gotten yourself to
rely on clang-format working in your editor).


> - When moving cursor up and down it's my preference that it doesn't jump
> back and forward when crossing blank lines.
>

But the proposal wouldn't really change much here, unless you happen to
have the cursor right at the start of the line and it would all be indented
the exact same level. Isn't that a really rare case?

Some of these issues can be fixed by editor configuration/plugins, but it's
> still not 100% correct.
>
> Thanks for your time.
>
> Kind regards,
> Samuel
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20171210/ce3ed262/attachment-0001.html>


More information about the cfe-dev mailing list