-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphHelpers.cs
More file actions
87 lines (81 loc) · 3.43 KB
/
GraphHelpers.cs
File metadata and controls
87 lines (81 loc) · 3.43 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
85
86
87
namespace CodeAtlas;
internal static class GraphHelpers
{
internal static string SanitizeId(string input) => input
.Replace("<", "_").Replace(">", "_").Replace(".", "_")
.Replace(",", "_").Replace(" ", "_").Replace("?", "")
.Replace("(", "").Replace(")", "").Replace("/", "_").Replace("\\", "_");
internal static string StripInterfacePrefix(string name) =>
name.StartsWith("I") && name.Length > 1 && char.IsUpper(name[1]) ? name[1..] : name;
internal static string DetectFileType(string path) =>
Path.GetExtension(path).ToLowerInvariant() switch
{
".cs" => "csharp",
".py" => "python",
".ts" or ".tsx" => "typescript",
".js" or ".jsx" => "javascript",
".json" => "json",
".yaml" or ".yml" => "yaml",
".css" or ".scss" or ".less" => "css",
".html" or ".htm" => "html",
".sql" => "sql",
".xml" or ".csproj" or ".props" or ".targets" => "xml",
".md" => "markdown",
".toml" => "toml",
".cfg" or ".ini" or ".conf" => "config",
_ => "other"
};
internal static string DetectProjectFromPath(string filePath)
{
var parts = filePath.Replace("\\", "/").Split('/');
for (int i = 0; i < parts.Length - 1; i++)
{
var part = parts[i];
if (part is "src" or "test" or "tests" or "." or ".." or "") continue;
return part;
}
return "Other";
}
internal static void BuildSections(MrFileNode fileNode, IEnumerable<DiffHunk> hunks)
{
foreach (var hunk in hunks)
{
var section = new MrCodeSection { Header = hunk.Header };
foreach (var line in hunk.Lines)
{
section.Lines.Add(new MrCodeLine
{
LineNum = line.NewLineNum ?? line.OldLineNum ?? 0,
Text = line.Text,
DiffType = line.Type switch
{
DiffLineType.Add => "add",
DiffLineType.Remove => "remove",
_ => "context"
}
});
}
fileNode.Sections.Add(section);
}
}
internal static void ComputeImpactRadii(MrGraph graph)
{
foreach (var file in graph.Files.Where(f => f.IsChanged))
{
var visited = new HashSet<string>();
var queue = new Queue<string>();
queue.Enqueue(file.Id);
visited.Add(file.Id);
while (queue.Count > 0)
{
var current = queue.Dequeue();
foreach (var edge in graph.Edges.Where(e => e.FromFileId == current))
{
if (visited.Add(edge.ToFileId))
queue.Enqueue(edge.ToFileId);
}
}
file.ImpactRadius = visited.Count - 1;
}
}
}