Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extra newline in GetComment feature #1080

Merged

Conversation

TylerLeonhardt
Copy link
Member

The ## comment generation feature has started putting a new line between the comment and the function... That doesn't seem right.

MicrosoftTeams-image

This trims that extra newline in PSES.

@TylerLeonhardt
Copy link
Member Author

also removed LINQ :)

@@ -80,21 +79,27 @@ public async Task<CommentHelpRequestResult> Handle(CommentHelpRequestParams requ
vscodeSnippetCorrection: true,
placement: helpLocation));

string helpText = analysisResults?.FirstOrDefault()?.Correction?.Edits[0].Text;
string helpText = analysisResults?[0]?.Correction?.Edits[0].Text;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is technically less safe; if there are no results we'll crash. But if we know we're going to get a correction that's ok (looks like we make the assumption that the result we get will have an edit, but that is conditioned on Correction being non-null).

Don't know enough about the context above to know whether the assumption is safe.

But the alternative is cheap I suppose:

if (analysisResults == null || analysisResults.Count == 0)
{
    return result;
}

string helpText = analysisResults[0].Correction?.Edits[0].Text;

if (helpText == null)
{
    return result;
}

// Trim trailing newline from help text.
if (string.IsNullOrEmpty(helpLines[helpLines.Count - 1]))
{
result.Content = helpLines.GetRange(0, helpLines.Count - 1).ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be where result.Content is assigned?

If there's no empty line at the end, it seems we don't assign result.Content.

So maybe instead the way to go is:

if (string.IsNullOrEmpty(helpLines[helpLines.Count - 1]))
{
    helpLines.RemoveAt(helpLines.Count - 1);
}

result.Content = helpLines.ToArray();
return result;


if (helpLocation != null &&
!helpLocation.Equals("before", StringComparison.OrdinalIgnoreCase))
{
// we need to trim the leading `{` and newline when helpLocation=="begin"
// we also need to trim the leading newline when helpLocation=="end"
result.Content = result.Content.Skip(1).ToArray();
helpLines = helpLines.GetRange(1, helpLines.Count - 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

GetRange will duplicate the entire list into a new value, leaving two lists on the heap to garbage collect.

It might be better to use RemoveAt:

helpLines.RemoveAt(0);
helpLines.RemoveAt(helpLines.Count - 1);

@PowerShell PowerShell deleted a comment Oct 31, 2019
@rjmholt rjmholt merged commit 6340108 into PowerShell:master Oct 31, 2019
@TylerLeonhardt
Copy link
Member Author

LGTM

@TylerLeonhardt TylerLeonhardt deleted the fix-extra-newline-in-comment branch April 11, 2020 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants