Syntax.Token(SyntaxKind.SemicolonToken).HasLeadingTrivia should be false.
-
Tuesday, February 21, 2012 4:27 AM
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
All Replies
-
Tuesday, February 21, 2012 8:58 PMOwner
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
- Edited by Shyam NamboodiripadMicrosoft Employee, Owner Tuesday, February 21, 2012 9:00 PM
-
Wednesday, February 22, 2012 11:44 PM
How do I make one without elastic trivia?
Thanks,
Chris -
Friday, February 24, 2012 8:22 PMOwner
Syntax.Token(SyntaxKind.SemicolonToken).WithLeadingTrivia().WithTrailingTrivia()
Bill
-
Friday, February 24, 2012 9:13 PM
Bill, when does Roslyn add elastic trivia to the tokens I create using the Syntax class? Always?
Chris
- Edited by kingces95 Friday, February 24, 2012 10:35 PM
-
Friday, February 24, 2012 11:05 PMOwner
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
- Marked As Answer by kingces95 Saturday, February 25, 2012 10:19 PM
-
Saturday, February 25, 2012 10:19 PM
Shyam, that's the answer I was looking for! By default please don't add any elastic trivia to my tokens!
Thanks,
Chris -
Tuesday, July 24, 2012 2:36 AMOwner
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

