Trim trailing whitespace on line comments
It would be nice if CodeFormatter automatically trimmed trailing spaces in line-comment trivia as well as in code. I naively assumed it already did until I looked at https://github.com/Microsoft/msbuild/pull/289.
I can't think of a strong reason to oppose this idea, especially since many repos including this one have a .editorconfig with trim_trailing_whitespace = true. But maybe I'm missing something?
Hmm, documentation comments are going to make things a bit complicated here :-(.
Why would that be complicated for doc comments? Trailling spaces -- expect for multiline strings -- should always be trimmed. Should be trivial by simply using the tokens.
@jaredpar?
@dpoeschl will know how the IDE works here better than me.
@terrajobst: Trailing whitespace in XML documentation comments seems not to appear in its own token/trivia. At least, that's what I saw in the syntax visualizer in VS 2015 RTM with version 1.0.0.50618 of the Roslyn SDK extension. (I didn't even look at regular multiline comments.)
There's also the small matter of whitespace sometimes being significant in XML.
@SamB @jaredpar @terrajobst There are a couple tricky cases, if you're starting the process from the syntax tree:
- In regular code (no comments), then we just need to look for trailing WhitespaceTrivia followed by trailing EndOfLineTrivia
- Multi-line comments are contained in a single MultiLineCommentTrivia, which would need to be further parsed to manually remove whitespace characters appearing before newline characters
- Single-line comments will contain their trailing whitespace, so we'd have to do something similar to multi-line comments
- XML doc comments combine words and whitespace into XmlTextLiteralTokens, so you'd have to look for XmlTextLiteralTokens before XmlTextLiteralNewLineTokens, and then remove whitespace characters from the end of the XmlTextLiteralToken as appropriate.
- (Need to handle VB xml literals, etc.)
If you instead think of this as a line-by-line text processing problem (with a convenient syntax tree that we can query), then I think we'd just:
- Check if the last character on each line is a whitespace character. If it is, then we can remove all trailing whitespace characters from the line if the syntax tree says we're not in a multi-line string (or, possibly, a CDATA region in VB, or just in VB XML literals, or...).
@SamB Is there a reasonable use case for significant trailing whitespace in documentation comments? I can definitely see a use case for significant trailing whitespace in VB XML literals.