Skip to content

Do not normalize leading whitespace in code comments#57414

Merged
CyrusNajmabadi merged 10 commits intodotnet:mainfrom
pakrym:pakrym/Do-not-normalize-leading-whitespace-in-code-comments
Nov 1, 2021
Merged

Do not normalize leading whitespace in code comments#57414
CyrusNajmabadi merged 10 commits intodotnet:mainfrom
pakrym:pakrym/Do-not-normalize-leading-whitespace-in-code-comments

Conversation

@pakrym
Copy link
Copy Markdown
Contributor

@pakrym pakrym commented Oct 27, 2021

Fixes: #57262

@pakrym pakrym requested a review from a team as a code owner October 27, 2021 18:02
@ghost ghost added the Area-IDE label Oct 27, 2021

private static string TrimEachLine(string text)
=> string.Join(Environment.NewLine, text.Split(s_NewLineAsStringArray, StringSplitOptions.RemoveEmptyEntries).Select(i => i.Trim()));
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't seem to be about 'code' blocks at all. It seems to relate to all lines of code (unless i'm missing something). So wouldn't this affect our normalization of code outside of codeblocks? if that's not the case can you explain why (and also doc the code accordingly)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if we had:

<code>
    int x;
    int y;
</code>

this would trim? But if we had:

BlahBlah
<code>
    int x;
    int y;
</code>

We wouldn't? that seems odd to me.

Copy link
Copy Markdown
Contributor Author

@pakrym pakrym Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in both cases you showed it won't trim.

It will trim:

  BlahBlah
  <code>
      int x;
      int y;
  </code>

and

  <code>
      int x;
      int y;
  </code>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm confused. those are my cases. :) you said it won't trim... but htat it will trim.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yours don't have leading whitespace on all lines:

<code>
    int x;
    int y;
</code>

vs

  <code>
      int x;
      int y;
  </code>

i++;
}

maxPrefix = Math.Min(maxPrefix, i);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests that exercising the codepaths with different lengths of maxprefix on different lines (including lines whose prefixes get shorter (or longer) than prior lines).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test I've added does have different prefixes on different lines. Happy to add more though.

@jinujoseph jinujoseph added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Oct 28, 2021
while (i < line.Length && char.IsWhiteSpace(line[i]))
{
i++;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't accurate with tabs vs spaces. i think we can live with that.

Comment on lines +198 to +209
var i = 0;
while (i < line.Length && char.IsWhiteSpace(line[i]))
{
i++;
}

// Don't include all-whitespace lines in the calculation
// They'll be completely trimmed
if (i == line.Length)
{
continue;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var i = 0;
while (i < line.Length && char.IsWhiteSpace(line[i]))
{
i++;
}
// Don't include all-whitespace lines in the calculation
// They'll be completely trimmed
if (i == line.Length)
{
continue;
}
var firstNonWhitespaceOffset = line.GetFirstNonWhitespaceOffset();
// Don't include all-whitespace lines in the calculation
// They'll be completely trimmed
if (firstNonWhitespaceOffset == null)
continue;

@pakrym
Copy link
Copy Markdown
Contributor Author

pakrym commented Nov 1, 2021

@CyrusNajmabadi failures look like build flakiness. Is there anything I can do to move this PR forward?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@pakrym I'm on it :-)

@CyrusNajmabadi CyrusNajmabadi merged commit 5947cca into dotnet:main Nov 1, 2021
@ghost ghost added this to the Next milestone Nov 1, 2021
@allisonchou allisonchou modified the milestones: Next, 17.1.P2 Nov 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VS Quick Info code sample formatting not preserved

4 participants