-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSyntaxTreeExtensions.cs
More file actions
25 lines (20 loc) · 876 Bytes
/
SyntaxTreeExtensions.cs
File metadata and controls
25 lines (20 loc) · 876 Bytes
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
namespace SharpTools.Tools.Extensions;
public static class SyntaxTreeExtensions
{
public static Project GetRequiredProject(this SyntaxTree tree, Solution solution)
{
var projectIds = solution.Projects
.Where(p => p.Documents.Any(d => d.FilePath == tree.FilePath))
.Select(p => p.Id)
.ToList();
if (projectIds.Count == 0)
throw new InvalidOperationException($"Could not find project containing file {tree.FilePath}");
if (projectIds.Count > 1)
throw new InvalidOperationException($"File {tree.FilePath} belongs to multiple projects");
var project = solution.GetProject(projectIds[0]);
if (project == null)
throw new InvalidOperationException($"Could not get project with ID {projectIds[0]}");
return project;
}
}