Guest User

VS Solution Helper

a guest
Jul 9th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using Microsoft.VisualStudio;
  2. using Microsoft.VisualStudio.Shell.Interop;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. namespace TSTestAdapter.Helpers
  7. {
  8.     public static class VsSolutionHelper
  9.     {
  10.         public static IEnumerable<IVsHierarchy> EnumerateLoadedProjects(this IVsSolution solution, __VSENUMPROJFLAGS enumFlags)
  11.         {
  12.             var prjType = Guid.Empty;
  13.             IEnumHierarchies ppHier;
  14.  
  15.             var hr = solution.GetProjectEnum((uint)enumFlags, ref prjType, out ppHier);
  16.             if (ErrorHandler.Succeeded(hr) && ppHier != null)
  17.             {
  18.                 uint fetched = 0;
  19.                 var hierarchies = new IVsHierarchy[1];
  20.                 while (ppHier.Next(1, hierarchies, out fetched) == VSConstants.S_OK)
  21.                 {
  22.                     yield return hierarchies[0];
  23.                 }
  24.             }
  25.         }
  26.         public static IEnumerable<string> GetProjectItems(IVsProject project)
  27.         {
  28.             // Each item in VS OM is IVSHierarchy.
  29.             var hierarchy = (IVsHierarchy)project;
  30.             return GetProjectItems(hierarchy, VSConstants.VSITEMID_ROOT);
  31.         }
  32.  
  33.         public static IEnumerable<string> GetProjectItems(IVsHierarchy project, uint itemId)
  34.         {
  35.             object pVar = GetPropertyValue((int)__VSHPROPID.VSHPROPID_FirstChild, itemId, project);
  36.  
  37.             uint childId = GetItemId(pVar);
  38.             while (childId != VSConstants.VSITEMID_NIL)
  39.             {
  40.                 string childPath = GetCanonicalName(childId, project);
  41.                 yield return childPath;
  42.  
  43.                 foreach (var childNodePath in GetProjectItems(project, childId)) yield return childNodePath;
  44.  
  45.                 pVar = GetPropertyValue((int)__VSHPROPID.VSHPROPID_NextSibling, childId, project);
  46.                 childId = GetItemId(pVar);
  47.             }
  48.         }
  49.  
  50.         public static uint GetItemId(object pvar)
  51.         {
  52.             if (pvar == null) return VSConstants.VSITEMID_NIL;
  53.             if (pvar is int) return (uint)(int)pvar;
  54.             if (pvar is uint) return (uint)pvar;
  55.             if (pvar is short) return (uint)(short)pvar;
  56.             if (pvar is ushort) return (uint)(ushort)pvar;
  57.             if (pvar is long) return (uint)(long)pvar;
  58.             return VSConstants.VSITEMID_NIL;
  59.         }
  60.  
  61.         public static object GetPropertyValue(int propid, uint itemId, IVsHierarchy vsHierarchy)
  62.         {
  63.             if (itemId == VSConstants.VSITEMID_NIL)
  64.             {
  65.                 return null;
  66.             }
  67.  
  68.             try
  69.             {
  70.                 object o;
  71.                 ErrorHandler.ThrowOnFailure(vsHierarchy.GetProperty(itemId, propid, out o));
  72.  
  73.                 return o;
  74.             }
  75.             catch (System.NotImplementedException)
  76.             {
  77.                 return null;
  78.             }
  79.             catch (System.Runtime.InteropServices.COMException)
  80.             {
  81.                 return null;
  82.             }
  83.             catch (System.ArgumentException)
  84.             {
  85.                 return null;
  86.             }
  87.         }
  88.  
  89.         public static string GetCanonicalName(uint itemId, IVsHierarchy hierarchy)
  90.         {
  91.             string strRet = string.Empty;
  92.             int hr = hierarchy.GetCanonicalName(itemId, out strRet);
  93.  
  94.             if (hr == VSConstants.E_NOTIMPL)
  95.             {
  96.                 // Special case E_NOTIMLP to avoid perf hit to throw an exception.
  97.                 return string.Empty;
  98.             }
  99.             else
  100.             {
  101.                 try
  102.                 {
  103.                     ErrorHandler.ThrowOnFailure(hr);
  104.                 }
  105.                 catch (System.Runtime.InteropServices.COMException)
  106.                 {
  107.                     strRet = string.Empty;
  108.                 }
  109.  
  110.                 // This could be in the case of S_OK, S_FALSE, etc.
  111.                 return strRet;
  112.             }
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment