Skip to content

Commit bd8b322

Browse files
Resolves #2: Extend SolutionHelper to find similar project items for shared projects and add RetrieveFileCodeModel to CodeModelBuilder that will conditionally resolve the FileCodeModel through a similar project item.
1 parent 1d57234 commit bd8b322

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

CodeMaid/Helpers/SolutionHelper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ internal static IEnumerable<ProjectItem> GetSelectedProjectItemsRecursively(Code
9797
return selectedProjectItems;
9898
}
9999

100+
/// <summary>
101+
/// Gets an enumerable set of similar project items compared by file name, useful for shared projects.
102+
/// </summary>
103+
/// <param name="package">The hosting package.</param>
104+
/// <param name="projectItem">The project item to match.</param>
105+
/// <returns>The enumerable set of similar project items.</returns>
106+
internal static IEnumerable<ProjectItem> GetSimilarProjectItems(CodeMaidPackage package, ProjectItem projectItem)
107+
{
108+
var allItems = GetAllItemsInSolution<ProjectItem>(package.IDE.Solution);
109+
110+
return allItems.Where(x => x.Name == projectItem.Name && x.Kind == projectItem.Kind && x.Document.FullName == projectItem.Document.FullName);
111+
}
112+
100113
#endregion Internal Methods
101114

102115
#region Private Methods

CodeMaid/Model/CodeModelBuilder.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ internal SetCodeItems RetrieveAllCodeItems(Document document)
7272
{
7373
var codeItems = new SetCodeItems();
7474

75-
if (document.ProjectItem != null)
76-
{
77-
RetrieveCodeItems(codeItems, document.ProjectItem.FileCodeModel);
78-
}
75+
var fileCodeModel = RetrieveFileCodeModel(document.ProjectItem);
76+
RetrieveCodeItems(codeItems, fileCodeModel);
7977

8078
codeItems.AddRange(_codeModelHelper.RetrieveCodeRegions(document.GetTextDocument()));
8179

@@ -86,6 +84,39 @@ internal SetCodeItems RetrieveAllCodeItems(Document document)
8684

8785
#region Private Methods
8886

87+
/// <summary>
88+
/// Attempts to return the FileCodeModel associated with the specified project item.
89+
/// </summary>
90+
/// <param name="projectItem">The project item.</param>
91+
/// <returns>The associated FileCodeModel, otherwise null.</returns>
92+
private FileCodeModel RetrieveFileCodeModel(ProjectItem projectItem)
93+
{
94+
if (projectItem == null)
95+
{
96+
return null;
97+
}
98+
99+
if (projectItem.FileCodeModel != null)
100+
{
101+
return projectItem.FileCodeModel;
102+
}
103+
104+
// If this project item is part of a shared project, retrieve the FileCodeModel via a similar platform project item.
105+
const string sharedProjectTypeGUID = "{d954291e-2a0b-460d-934e-dc6b0785db48}";
106+
var containingProject = projectItem.ContainingProject;
107+
108+
if (containingProject != null && containingProject.Kind != null &&
109+
containingProject.Kind.ToLowerInvariant() == sharedProjectTypeGUID)
110+
{
111+
var similarProjectItems = SolutionHelper.GetSimilarProjectItems(_package, projectItem);
112+
var fileCodeModel = similarProjectItems.Select(x => x.FileCodeModel).FirstOrDefault(y => y != null);
113+
114+
return fileCodeModel;
115+
}
116+
117+
return null;
118+
}
119+
89120
/// <summary>
90121
/// Walks the given FileCodeModel, turning CodeElements into code items within the specified
91122
/// code items set.

0 commit comments

Comments
 (0)