-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathLocationExtensions.cs
More file actions
84 lines (73 loc) · 3.36 KB
/
Copy pathLocationExtensions.cs
File metadata and controls
84 lines (73 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
namespace Semmle.Extraction.CSharp
{
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;
/// <summary>
/// Whether one Location outer completely contains another Location inner.
/// </summary>
/// <param name="outer">The outer location.</param>
/// <param name="inner">The inner location</param>
/// <returns>Whether inner is completely container in outer.</returns>
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;
}
/// <summary>
/// Whether one Location ends before another starts.
/// </summary>
/// <param name="before">The Location coming before</param>
/// <param name="after">The Location coming after</param>
/// <returns>Whether 'before' comes before 'after'.</returns>
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;
}
private static int GetLocationKindPriority(Location location) =>
location.IsInSource
? 2
: location.IsInMetadata
? 1
: 0;
/// <summary>
/// Returns true if l1 is better than l2.
/// Source locations are considered better than non source locations.
/// </summary>
private static bool BetterThan(Location l1, Location l2)
{
if (GetLocationKindPriority(l1) > GetLocationKindPriority(l2))
{
return true;
}
// For source locations we compare the filepath and span.
if (l1.IsInSource && l2.IsInSource)
{
var l1s = l1.SourceTree.FilePath + l1.SourceSpan;
var l2s = l2.SourceTree.FilePath + l2.SourceSpan;
return l1s.CompareTo(l2s) < 0;
}
return false;
}
/// <summary>
/// Returns the best location from the given list of locations.
/// Source locations are considered better than non-source locations.
/// In case of a (source location) tie, the location with the
/// lexicographically smaller filepath and span is considered better.
/// </summary>
public static Location? BestOrDefault(this IEnumerable<Location> locations) =>
locations.Any() ? locations.Aggregate((best, loc) => BetterThan(loc, best) ? loc : best) : null;
public static Location Best(this IEnumerable<Location> locations) =>
locations.BestOrDefault() ?? throw new ArgumentException("No location found.");
}
}