Last active
August 29, 2015 14:23
-
-
Save Maxwe11/2c0499933e29bf2787bb to your computer and use it in GitHub Desktop.
First token in line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal static bool IsFirstInLine_1(this SyntaxToken token) | |
{ | |
return token.HasLeadingTrivia || token.GetLocation()?.GetMappedLineSpan().StartLinePosition.Character == 0; | |
} | |
internal static bool IsFirstInLine_2(this SyntaxToken token) | |
{ | |
var prevToken = token.GetPreviousToken(); | |
var leadingTriviaList = prevToken.TrailingTrivia.AddRange(token.LeadingTrivia); | |
var isFirstOnLine = false; | |
if (prevToken.GetLocation().GetLineSpan().EndLinePosition.Line < token.GetLocation().GetLineSpan().StartLinePosition.Line) | |
{ | |
var done = false; | |
for (var i = leadingTriviaList.Count - 1; !done && (i >= 0); i--) | |
{ | |
switch (leadingTriviaList[i].Kind()) | |
{ | |
case SyntaxKind.WhitespaceTrivia: | |
break; | |
case SyntaxKind.EndOfLineTrivia: | |
isFirstOnLine = true; | |
done = true; | |
break; | |
default: | |
done = true; | |
break; | |
} | |
} | |
} | |
return isFirstOnLine; | |
} | |
internal static bool? IsFirstInLine_3(this SyntaxToken clause) | |
{ | |
var fileLinePositionSpan = clause.GetLocation().GetLineSpan(); | |
if (!fileLinePositionSpan.IsValid) | |
{ | |
return null; | |
} | |
var startLine = fileLinePositionSpan.StartLinePosition.Line; | |
var previousToken = clause.GetPreviousToken(); | |
if (previousToken.IsMissing) | |
{ | |
return true; | |
} | |
var endLineOfPreviousToken = previousToken.GetLocation().GetLineSpan().EndLinePosition.Line; | |
return startLine > endLineOfPreviousToken; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment