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

Retain single leading space when appending string in TypeLookUp #1064

Merged
merged 11 commits into from
Jan 9, 2018

Conversation

akshita31
Copy link
Contributor

@akshita31 akshita31 commented Dec 14, 2017

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

@akshita31 akshita31 changed the title Retain Single leading space when appending string in TypeLookUp Retain single leading space when appending string in TypeLookUp Dec 14, 2017
@@ -166,6 +166,16 @@ private static string GetCref(string cref)
}
return cref + " ";
}

private static string RetainSingleLeadingSpace(string input)
Copy link
Contributor

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);
Copy link
Contributor

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()}";

Copy link

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.

Copy link

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.

Copy link

@rchande rchande left a 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();
Copy link

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.

Copy link

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)));
Copy link

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

Copy link

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)

Copy link
Contributor Author

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.

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?

Copy link
Contributor Author

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.

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?

Copy link
Contributor Author

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.";

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?

Copy link
Contributor Author

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]))
Copy link

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.

@david-driscoll david-driscoll merged commit 55eadc0 into OmniSharp:master Jan 9, 2018
@akshita31 akshita31 deleted the space_after_c branch February 1, 2018 22:25
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.

5 participants