Syntax.Token(SyntaxKind.SemicolonToken).HasLeadingTrivia should be false.
-
21 กุมภาพันธ์ 2555 4:27
I expected Syntax.Token(SyntaxKind.SemicolonToken).HasLeadingTrivia to be false but it is true. I expect if HasLeadingTrivia is true then the sum of the widths of the trivia is greater than zero.
Thanks,
Chris
ตอบทั้งหมด
-
21 กุมภาพันธ์ 2555 20:58เจ้าของ
Looks like "Syntax.Token(SyntaxKind.SemicolonToken)" generates a token with kind SemicolonToken with one leading and one trailing elastic trivia. Elastic trivia are used as a convenient way to tell formatting APIs that “there is some whitespace here but I don’t care how ‘wide’ this whitespace is – please feel free to ‘stretch’ this whitespace as appropriate when formatting the code”. By default, elastic trivia have 0 width and if you call .Format() on the constructed token above, you will see that the elastic trivia will disappear (see below code example).
It was my impression that one had to 'opt in' to get elastic trivia (e.g. by constructing and passing trivia generated using isElastic=true or using special factory methods like Syntax.ElasticSpace()). But looks like the Syntax.Token factory API will insert elastic trivia by default for convinience (so that callers don't have to attach whitespace trivia / take care of formatting themselves).
var t = Syntax.Token(SyntaxKind.AddKeyword); // t = t.Format(); // Uncomment to see the elastic trivia disappear Console.WriteLine("HasLeadingTrivia: " + t.HasLeadingTrivia); Console.WriteLine("HasTrailingTrivia: " + t.HasTrailingTrivia); foreach(var tr in t.LeadingTrivia) { Console.Write(" Leading - "); Console.WriteLine("IsElastic: " + tr.IsElastic + ", Width: " + tr.Width); } foreach (var tr in t.TrailingTrivia) { Console.Write(" Trailing - "); Console.WriteLine("IsElastic: " + tr.IsElastic + ", Width: " + tr.Width); }
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- แก้ไขโดย Shyam NamboodiripadMicrosoft, Owner 21 กุมภาพันธ์ 2555 21:00
-
22 กุมภาพันธ์ 2555 23:44
How do I make one without elastic trivia?
Thanks,
Chris -
24 กุมภาพันธ์ 2555 20:22เจ้าของ
Syntax.Token(SyntaxKind.SemicolonToken).WithLeadingTrivia().WithTrailingTrivia()
Bill
-
24 กุมภาพันธ์ 2555 21:13
Bill, when does Roslyn add elastic trivia to the tokens I create using the Syntax class? Always?
Chris
- แก้ไขโดย kingces95 24 กุมภาพันธ์ 2555 22:35
-
24 กุมภาพันธ์ 2555 23:05เจ้าของ
You could also use Syntax.Token(SyntaxTriviaList.Empty, SyntaxKind.SemicolonToken, SyntaxTriviaList.Empty) OR call Syntax.Token(SyntaxKind.SemiColonToken).Format().
The parser (i.e. SyntaxTree.ParseCompilationUnit and other APIs like Syntax.Parse*) should never generate tokens with elastic trivia. The Syntax.Token() factory method however currently always attaches elastic trivia. I believe this behavior is not final yet and we have an internal bug tracking this issue (i.e. that because the API always attaches elastic trivia this makes it more difficult to generate tokens that don't want elastic trivia).
Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
- ทำเครื่องหมายเป็นคำตอบโดย kingces95 25 กุมภาพันธ์ 2555 22:19
-
25 กุมภาพันธ์ 2555 22:19
Shyam, that's the answer I was looking for! By default please don't add any elastic trivia to my tokens!
Thanks,
Chris -
24 กรกฎาคม 2555 2:36เจ้าของ
Chris,
What's the scenario that requires you to not have elastic trivia on tokens other than math and a strong sense of privacy?
-ADG
Anthony D. Green | Program Manager | Visual Basic & C# Languages Team