-
Notifications
You must be signed in to change notification settings - Fork 416
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
Retain single leading space when appending string in TypeLookUp #1064
Conversation
@@ -166,6 +166,16 @@ private static string GetCref(string cref) | |||
} | |||
return cref + " "; | |||
} | |||
|
|||
private static string RetainSingleLeadingSpace(string input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would TrimStartRetainingSingleLeadingSpace
be clearer in terms of the intent?
if (!Char.IsWhiteSpace(input[0])) | ||
return input; | ||
int offset = input.TakeWhile(c => char.IsWhiteSpace(c)).Count(); | ||
return " " + input.Substring(offset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of LINQ caught my eye here. It looks like this will be a pretty hot path so we might do a little perf tuning.
Would deleting the LINQ expression and doing something like this satisfy the goal?
return $" {input.TrimStart()}";
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, we allocate one string fewer by not calling TrimStart.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, @jonsequitur is right--I don't know why I thought string.substring didn't allocate a string. I like his solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please just use a for loop :).
Optionally experiment with inputs to string.split.
return string.Empty; | ||
if (!Char.IsWhiteSpace(input[0])) | ||
return input; | ||
int offset = input.TakeWhile(c => char.IsWhiteSpace(c)).Count(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just write this as a simple for loop instead of linq.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It's faster).
@@ -147,7 +147,7 @@ public static DocumentationComment From(string xmlDocumentation, string lineEndi | |||
private static string TrimMultiLineString(string input, string lineEnding) | |||
{ | |||
var lines = input.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); | |||
return string.Join(lineEnding, lines.Select(l => l.TrimStart())); | |||
return string.Join(lineEnding, lines.Select(l => RetainSingleLeadingSpace(l))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if you put
(space space) in your list of strings to split on? I think that will give you strings that start with 0 or 1 spaces...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Just spitballing, you don't have to do this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think that will work. Suppose the user separates two works with 3 spaces "hello hello1" , then these parts will also be split and joined with a lineEnding
. That will be unnecessary splitting and incorrect joining.
@@ -499,7 +499,7 @@ public class TestClass | |||
}"; | |||
var response = await GetTypeLookUpResponse(content); | |||
var expected = | |||
@"This sample shows how to call the TestClass.GetZero method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did we eliminate these spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't matter because the spaces were not a part of the string anyways. Just left-aligned it
@@ -499,7 +499,7 @@ public class TestClass | |||
}"; | |||
var response = await GetTypeLookUpResponse(content); | |||
var expected = | |||
@"This sample shows how to call the TestClass.GetZero method. | |||
@"This sample shows how to call the TestClass.GetZero method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we adding extra whitespace here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially return cref + " ";
in GetCref was appending a space after TestClass.GetZero
and the space before method
was being trimmed so overall there was a single space. Now we are retaining the space before method
as well, hence the output consists of 2 spaces. But this shouldn't be an issue as vscode collapses the multiple spaces in the text (as Hover API uses a MarkDown string)
@@ -452,7 +452,7 @@ public class TestClass | |||
}"; | |||
var response = await GetTypeLookUpResponse(content); | |||
var expected = | |||
@"DoWork is a method in the TestClass class. System.Console.WriteLine(System.String) for information about output statements."; | |||
@"DoWork is a method in the TestClass class. System.Console.WriteLine(System.String) for information about output statements."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we adding extra whitespace here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
{ | ||
if (string.IsNullOrWhiteSpace(input)) | ||
return string.Empty; | ||
if (!Char.IsWhiteSpace(input[0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style: char
instead of Char
.
Fixes: #1046
Before concatenating the string, a TrimStart was being called that removed all the whitespaces. Added a call to a function that retains a single space if the string begins with a whitespace. Added a test for the same and modified the existing tests to match the behaviour.
Please review : @DustinCampbell @TheRealPiotrP @rchande