Fix errors in firstInLine calculations#261
Fix errors in firstInLine calculations#261sharwell wants to merge 1 commit intoDotNetAnalyzers:masterfrom
Conversation
|
@srivatsn @Pilchie Which of the following is faster? Are there bugs I haven't identified? I'm trying to determine if a token is the first token on a line, not counting trivia. Option 1: bool firstInLine = token.HasLeadingTrivia
|| token.GetLocation()?.GetMappedLineSpan().StartLinePosition.Character;Option 2: bool firstInLine = token.HasLeadingTrivia;
if (!firstInLine)
{
SyntaxToken previousToken = token.GetPreviousToken();
firstInLine = previousToken.IsMissing
|| previousToken.TrailingTrivia.Any(SyntaxKind.WhitespaceTrivia);
} |
|
I'm not sure either of those work reliably. We use this helper in formatting: http://source.roslyn.codeplex.com/#Microsoft.CodeAnalysis.Workspaces/Shared/Utilities/CommonFormattingHelpers.cs,fcf0d57b28a05a1e |
|
As described above, this pull request does not actually solve the problem it aims to solve. A new solution should be presented instead. |
|
Finding out whether a token is a first token on a line only using Token is not simple since leading and trailing trivia could contain structured trivia, and end of line (or other kinds of trivia that could contain new line) trivia might belong to them rather than regular token. if you have SourceText, just use previous Token's end position and current token's start position to get line number and compare them to see whether two are on different line. also, when you say, "token", you need to distinguish whether it only means a normal token or it includes token that belong to structured trivia such as xml document, skipped tokens and etc. Simplest way is probably using SourceText, otherwise, you probably need to traverse down to every trivia (including trivia inside of structured trivia) between two tokens and see any one of them (since there can be multiple different kinds of trivia that can have EOL) contains new line. other easiest way (without SourceText) will be go through all trivia between two tokens and then just use ToString to get "string representation" of the trivia and do text search. |
Fixes #257
Fixes #258