using Microsoft.CodeAnalysis; namespace Semmle.Extraction { public static class LocationExtensions { public static int StartLine(this Location loc) => loc.GetLineSpan().Span.Start.Line; public static int StartColumn(this Location loc) => loc.GetLineSpan().Span.Start.Character; public static int EndLine(this Location loc) => loc.GetLineSpan().Span.End.Line; /// /// Whether one Location outer completely contains another Location inner. /// /// The outer location. /// The inner location /// Whether inner is completely container in outer. public static bool Contains(this Location outer, Location inner) { var sameFile = outer.SourceTree == inner.SourceTree; var startsBefore = outer.SourceSpan.Start <= inner.SourceSpan.Start; var endsAfter = outer.SourceSpan.End >= inner.SourceSpan.End; return sameFile && startsBefore && endsAfter; } /// /// Whether one Location ends before another starts. /// /// The Location coming before /// The Location coming after /// Whether 'before' comes before 'after'. public static bool Before(this Location before, Location after) { var sameFile = before.SourceTree == after.SourceTree; var endsBefore = before.SourceSpan.End <= after.SourceSpan.Start; return sameFile && endsBefore; } } }