-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVsCSharpSourceExtensions.cs
More file actions
70 lines (53 loc) · 2.63 KB
/
VsCSharpSourceExtensions.cs
File metadata and controls
70 lines (53 loc) · 2.63 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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2020 CodeFactory, LLC
//*****************************************************************************
using System.Threading.Tasks;
namespace CodeFactory.VisualStudio
{
/// <summary>
/// Extensions class that provides common automation tasks rolled up under standard extension methods that support the <see cref="VsCSharpSource"/> model.
/// </summary>
public static class VsCSharpSourceExtensions
{
/// <summary>
/// Extension method that loads the hosting project for the <see cref="VsCSharpSource"/> document.
/// </summary>
/// <param name="source">Target document to load the parent from.</param>
/// <returns>The project model or null if the project could not be loaded.</returns>
public static async Task<VsProject> GetHostingProjectAsync(this VsCSharpSource source)
{
//Bounds check if no instance of the model is provided returning null.
if (source == null) return null;
//Loading the project system version of the document.
var document = await source.LoadDocumentModelAsync();
//If the project system version of the document could not be loaded then return null.
if (document == null) return null;
//Models to store information about lookup results.
VsProject result = null;
VsModel currentModel = document;
while (result == null)
{
//Confirming a model was returned otherwise there is no parent project to return, so break out of the while loop.
if (currentModel == null) break;
switch (currentModel.ModelType)
{
//switching between each standard model types. loading model data.
case VisualStudioModelType.Project:
result = currentModel as VsProject;
break;
case VisualStudioModelType.ProjectFolder:
currentModel = currentModel is VsProjectFolder projectFolder ? await projectFolder.GetParentAsync() : null;
break;
case VisualStudioModelType.Document:
currentModel = currentModel is VsDocument documentModel ? await documentModel.GetParentAsync() : null;
break;
default:
currentModel = null;
break;
}
}
return result;
}
}
}