-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSourceLineMatching.cs
108 lines (94 loc) · 4.54 KB
/
SourceLineMatching.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Meadow.CoverageReport.Models;
using SolcNet.DataDescription.Output;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Meadow.CoverageReport
{
static class SourceLineMatching
{
public static SourceFileLine GetSourceFileLineFromAstNode(AstNode node, IReadOnlyDictionary<int, SourceFileMap> sourceIndexes)
{
var sourceFileMap = sourceIndexes[node.SourceIndex];
var sourceFileLine = sourceFileMap.SourceFileLines
.First(s => node.SourceRange.Offset >= s.Offset && node.SourceRange.Offset < s.OffsetEnd);
return sourceFileLine;
}
public static IEnumerable<SourceFileLine> GetSourceFileLinesContainingAstNode(AstNode node, IReadOnlyDictionary<int, SourceFileMap> sourceIndexes)
{
var sourceFileMap = sourceIndexes[node.SourceIndex];
var sourceFileLines = sourceFileMap.SourceFileLines
.Where(sourceLine => node.SourceRange.Offset >= sourceLine.Offset && node.SourceRange.Offset < sourceLine.OffsetEnd);
foreach (var l in sourceFileLines)
{
if (l.SourceFileMapParent.SourceFileIndex != node.SourceIndex)
{
throw new Exception($"Source file index mismatch between {l.SourceFileMapParent.SourceFileIndex} and {node.SourceIndex}");
}
yield return l;
}
}
public static IEnumerable<SourceFileLine> GetSourceFileLinesContainedWithinAstNode(AstNode node, IReadOnlyDictionary<int, SourceFileMap> sourceIndexes)
{
var sourceFileMap = sourceIndexes[node.SourceIndex];
var sourceFileLines = sourceFileMap.SourceFileLines
.Where(sourceLine => sourceLine.Offset >= node.SourceRange.Offset && sourceLine.Offset <= node.SourceRange.OffsetEnd);
foreach (var l in sourceFileLines)
{
if (l.SourceFileMapParent.SourceFileIndex != node.SourceIndex)
{
throw new Exception($"Source file index mismatch between {l.SourceFileMapParent.SourceFileIndex} and {node.SourceIndex}");
}
yield return l;
}
}
public static SourceFileLine GetSourceFileLineFromSourceMapEntry(SourceMapEntry entry, IReadOnlyDictionary<int, SourceFileMap> sourceIndexes)
{
// This should only ever be missing when the source file is explicitly ignored
if (!sourceIndexes.TryGetValue(entry.Index, out var sourceFileMap))
{
return null;
}
var sourceFileLine = sourceFileMap.SourceFileLines
.First(s => entry.Offset >= s.Offset && entry.Offset < s.OffsetEnd);
if (sourceFileLine.SourceFileMapParent.SourceFileIndex != entry.Index)
{
throw new Exception($"Source file index mismatch between {sourceFileLine.SourceFileMapParent.SourceFileIndex} and {entry.Index}");
}
return sourceFileLine;
}
public static IEnumerable<SourceFileLine> GetSourceFileLinesFromSourceMapEntry(SourceMapEntry entry, IReadOnlyDictionary<int, SourceFileMap> sourceIndexes)
{
// This should only ever be missing when the source file is explicitly ignored
if (!sourceIndexes.TryGetValue(entry.Index, out var sourceFileMap))
{
return null;
}
var sourceFileLines = sourceFileMap.SourceFileLines
.Where(s => entry.Offset >= s.Offset && entry.Offset < s.OffsetEnd);
foreach (var l in sourceFileLines)
{
if (l.SourceFileMapParent.SourceFileIndex != entry.Index)
{
throw new Exception($"Source file index mismatch between {l.SourceFileMapParent.SourceFileIndex} and {entry.Index}");
}
}
return sourceFileLines;
}
public static IEnumerable<AstNode> MatchAstNodesToSourceFileLine(AnalysisResults analysis, SourceFileLine sourceFileLine)
{
foreach (var node in analysis.FullNodeList)
{
if (node.SourceIndex != sourceFileLine.SourceFileMapParent.SourceFileIndex)
{
continue;
}
if (node.SourceRange.Offset >= sourceFileLine.Offset && node.SourceRange.Offset < sourceFileLine.OffsetEnd)
{
yield return node;
}
}
}
}
}