I\'m trying to write a simple Visual Studio 2012 extension. I have generated the extension template and can bring up a dialog box from a tool menu.
I\'d like to enum
This seems to be the simple answer. In the context of a visual studio extension will return all files.
public IEnumerable<ProjectItem> Recurse(ProjectItems i)
{
if (i!=null)
{
foreach (ProjectItem j in i)
{
foreach (ProjectItem k in Recurse(j))
{
yield return k;
}
}
}
}
public IEnumerable<ProjectItem> Recurse(ProjectItem i)
{
yield return i;
foreach (ProjectItem j in Recurse(i.ProjectItems ))
{
yield return j;
}
}
public IEnumerable<ProjectItem> SolutionFiles()
{
Solution2 soln = (Solution2)_applicationObject.Solution;
foreach (Project project in soln.Projects)
{
foreach (ProjectItem item in Recurse(project.ProjectItems))
{
yield return item;
}
}
}
You can then do neat tricks with it like implement the search function at the core of my CommandT clone.
private static string Pattern(string src)
{
return ".*" + String.Join(".*", src.ToCharArray());
}
private static bool RMatch(string src, string dest)
{
try
{
return Regex.Match(dest, Pattern(src), RegexOptions.IgnoreCase).Success;
}
catch (Exception e)
{
return false;
}
}
private static List<string> RSearch(
string word,
IEnumerable<string> wordList,
double fuzzyness)
{
// Tests have prove that the !LINQ-variant is about 3 times
// faster!
List<string> foundWords =
(
from s in wordList
where RMatch(word, s) == true
orderby s.Length ascending
select s
).ToList();
return foundWords;
}
which is used like
var list = RSearch("bnd", SolutionFiles().Select(x=>x.Name))