Skip to content

Commit 2c955f2

Browse files
Deal with more complicated solution structures
1 parent 5c2d869 commit 2c955f2

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

Vsix/SolutionProjectExtensions.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using EnvDTE;
4+
using EnvDTE80;
5+
6+
namespace CodeConverter.VsExtension
7+
{
8+
/// <summary>
9+
/// http://www.wwwlicious.com/2011/03/29/envdte-getting-all-projects-html/#comment-2557459433
10+
/// </summary>
11+
public static class SolutionProjectExtensions
12+
{
13+
public static IEnumerable<Project> GetAllProjects(this Solution sln)
14+
{
15+
return sln.Projects.Cast<Project>().SelectMany(GetProjects);
16+
}
17+
18+
public static IEnumerable<Project> GetProjects(this Project project)
19+
{
20+
if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder) {
21+
return project.ProjectItems.Cast<ProjectItem>()
22+
.Select(x => x.SubProject).Where(x => x != null)
23+
.SelectMany(GetProjects);
24+
}
25+
return new[] { project };
26+
}
27+
}
28+
}

Vsix/VisualStudioInteraction.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ private static IEnumerable<T> GetSelectedSolutionExplorerItems<T>() where T: cla
6868
var selectedObjects = (IEnumerable<object>) Dte.ToolWindows.SolutionExplorer.SelectedItems;
6969
var selectedItems = selectedObjects.Cast<UIHierarchyItem>().ToList();
7070

71+
return ObjectOfType<T>(selectedItems);
72+
}
73+
74+
private static IEnumerable<T> ObjectOfType<T>(IReadOnlyCollection<UIHierarchyItem> selectedItems) where T : class
75+
{
7176
var returnType = typeof(T);
7277
return selectedItems.Select(item => item.Object).Where(returnType.IsInstanceOfType).Cast<T>();
7378
}
@@ -86,13 +91,12 @@ public static void SelectAll(this Window window)
8691

8792
public static IReadOnlyCollection<Project> GetSelectedProjects(string projectExtension)
8893
{
89-
var items = GetSelectedSolutionExplorerItems<Solution>()
90-
.SelectMany(s => s.Projects.Cast<Project>())
91-
.Concat(GetSelectedSolutionExplorerItems<Project>())
92-
.Concat(GetSelectedSolutionExplorerItems<ProjectItem>().Where(p => p.SubProject != null).Select(p => p.SubProject))
94+
var projects = GetSelectedSolutionExplorerItems<Solution>().SelectMany(s => s.GetAllProjects())
95+
.Concat(GetSelectedSolutionExplorerItems<Project>().SelectMany(p => p.GetProjects()))
96+
.Concat(GetSelectedSolutionExplorerItems<ProjectItem>().Where(p => p.SubProject != null).SelectMany(p => p.SubProject.GetProjects()))
9397
.Where(project => project.FullName.EndsWith(projectExtension, StringComparison.InvariantCultureIgnoreCase))
94-
.ToList();
95-
return items;
98+
.Distinct().ToList();
99+
return projects;
96100
}
97101

98102
public static IWpfTextViewHost GetCurrentViewHost(IServiceProvider serviceProvider)

Vsix/Vsix.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
</Compile>
246246
<Compile Include="ConvertVBToCSCommand.cs" />
247247
<Compile Include="REConverterPackage.cs" />
248+
<Compile Include="SolutionProjectExtensions.cs" />
248249
<Compile Include="VisualStudioInteraction.cs" />
249250
<Compile Include="VsDocument.cs" />
250251
</ItemGroup>

0 commit comments

Comments
 (0)