Skip to content

Fix errors in firstInLine calculations#261

Closed
sharwell wants to merge 1 commit intoDotNetAnalyzers:masterfrom
sharwell:fix-257
Closed

Fix errors in firstInLine calculations#261
sharwell wants to merge 1 commit intoDotNetAnalyzers:masterfrom
sharwell:fix-257

Conversation

@sharwell
Copy link
Member

Fixes #257
Fixes #258

@sharwell
Copy link
Member Author

@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);
}

@sharwell
Copy link
Member Author

As described above, this pull request does not actually solve the problem it aims to solve. A new solution should be presented instead.

@heejaechang
Copy link

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.

@sharwell sharwell deleted the fix-257 branch July 27, 2015 00:44
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.

SA1018 incorrectly reported at beginning of line SA1001 incorrectly reported at beginning of line

3 participants