codeformatter icon indicating copy to clipboard operation
codeformatter copied to clipboard

Trim trailing whitespace on line comments

Open rainersigwald opened this issue 10 years ago • 6 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?

rainersigwald avatar Oct 19 '15 15:10 rainersigwald

Hmm, documentation comments are going to make things a bit complicated here :-(.

SamB avatar Oct 22 '15 02:10 SamB

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?

terrajobst avatar Nov 06 '15 17:11 terrajobst

@dpoeschl will know how the IDE works here better than me.

jaredpar avatar Nov 06 '15 18:11 jaredpar

@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 avatar Nov 11 '15 05:11 SamB

@SamB @jaredpar @terrajobst There are a couple tricky cases, if you're starting the process from the syntax tree:

  1. In regular code (no comments), then we just need to look for trailing WhitespaceTrivia followed by trailing EndOfLineTrivia
  2. 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
  3. Single-line comments will contain their trailing whitespace, so we'd have to do something similar to multi-line comments
  4. 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.
  5. (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:

  1. 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...).

dpoeschl avatar Nov 11 '15 16:11 dpoeschl

@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.

dpoeschl avatar Nov 11 '15 16:11 dpoeschl