/// <summary> /// Returns a list of source controllable files associated with the specified node /// </summary> private static async Task<IList<string>> GetNodeFiles(IVsSccProject2 pscp2, uint itemid) { // NOTE: the function returns only a list of files, containing both regular files and special files // If you want to hide the special files (similar with solution explorer), you may need to return // the special files in a hastable (key=master_file, values=special_file_list) // Initialize output parameters IList<string> sccFiles = new List<string>(); if (pscp2 != null) { CALPOLESTR[] pathStr = new CALPOLESTR[1]; CADWORD[] flags = new CADWORD[1]; await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); if (pscp2.GetSccFiles(itemid, pathStr, flags) == 0) { for (int elemIndex = 0; elemIndex < pathStr[0].cElems; elemIndex++) { IntPtr pathIntPtr = Marshal.ReadIntPtr(pathStr[0].pElems, elemIndex); String path = Marshal.PtrToStringAuto(pathIntPtr); sccFiles.Add(path); // See if there are special files if (flags.Length > 0 && flags[0].cElems > 0) { int flag = Marshal.ReadInt32(flags[0].pElems, elemIndex); if (flag != 0) { // We have special files CALPOLESTR[] specialFiles = new CALPOLESTR[1]; CADWORD[] specialFlags = new CADWORD[1]; pscp2.GetSccSpecialFiles(itemid, path, specialFiles, specialFlags); for (int i = 0; i < specialFiles[0].cElems; i++) { IntPtr specialPathIntPtr = Marshal.ReadIntPtr(specialFiles[0].pElems, i * IntPtr.Size); String specialPath = Marshal.PtrToStringAuto(specialPathIntPtr); sccFiles.Add(specialPath); Marshal.FreeCoTaskMem(specialPathIntPtr); } if (specialFiles[0].cElems > 0) { Marshal.FreeCoTaskMem(specialFiles[0].pElems); } } } Marshal.FreeCoTaskMem(pathIntPtr); } if (pathStr[0].cElems > 0) { Marshal.FreeCoTaskMem(pathStr[0].pElems); } } } else if (itemid == VSConstants.VSITEMID_ROOT) { sccFiles.Add(await GetSolutionFileName()); } return sccFiles; }
/// <summary> /// This method is called to discover special (hidden files) associated with a given VSITEMID within this hierarchy. /// </summary> /// <param name="itemid">Identifier for the VSITEMID being queried.</param> /// <param name="sccFile">One of the files associated with the node</param> /// <param name="stringsOut">Pointer to an array of CALPOLESTR strings containing the file names for this item.</param> /// <param name="flagsOut">Pointer to a CADWORD array of flags stored in DWORDs indicating that some of the files have special behaviors.</param> /// <returns>If the method succeeds, it returns S_OK. If it fails, it returns an error code. </returns> /// <remarks>This method is called to discover any special or hidden files associated with an item in the project hierarchy. It is called when GetSccFiles returns with the SFF_HasSpecialFiles flag set for any of the files associated with the node.</remarks> public virtual int GetSccSpecialFiles(uint itemid, string sccFile, CALPOLESTR[] stringsOut, CADWORD[] flagsOut) { if (itemid == VSConstants.VSITEMID_SELECTION) { throw new ArgumentException(SR.GetString(SR.InvalidParameter, CultureInfo.CurrentUICulture), "itemid"); } HierarchyNode n = this.NodeFromItemId(itemid); if (n == null) { throw new ArgumentException(SR.GetString(SR.InvalidParameter, CultureInfo.CurrentUICulture), "itemid"); } List<string> files = new List<string>(); List<tagVsSccFilesFlags> flags = new List<tagVsSccFilesFlags>(); n.GetSccSpecialFiles(sccFile, files, flags); if (stringsOut != null && stringsOut.Length > 0) { stringsOut[0] = Utilities.CreateCALPOLESTR(files); } if (flagsOut != null && flagsOut.Length > 0) { flagsOut[0] = Utilities.CreateCADWORD(flags); } return VSConstants.S_OK; }
/// <summary> /// Returns a list of source controllable files associated with the specified node /// </summary> private IList<string> GetNodeFiles(IVsSccProject2 pscp2, uint itemid) { // NOTE: the function returns only a list of files, containing both regular files and special files // If you want to hide the special files (similar with solution explorer), you may need to return // the special files in a hastable (key=master_file, values=special_file_list) // Initialize output parameters IList<string> sccFiles = new List<string>(); if (pscp2 != null) { var pathStr = new CALPOLESTR[1]; var flags = new CADWORD[1]; if (pscp2.GetSccFiles(itemid, pathStr, flags) == 0) { for (int elemIndex = 0; elemIndex < pathStr[0].cElems; elemIndex++) { var pathIntPtr = Marshal.ReadIntPtr(pathStr[0].pElems, elemIndex); var path = Marshal.PtrToStringAuto(pathIntPtr); sccFiles.Add(path); // See if there are special files if (flags.Length > 0 && flags[0].cElems > 0) { int flag = Marshal.ReadInt32(flags[0].pElems, elemIndex); if (flag != 0) { // We have special files var specialFiles = new CALPOLESTR[1]; var specialFlags = new CADWORD[1]; pscp2.GetSccSpecialFiles(itemid, path, specialFiles, specialFlags); for (int i = 0; i < specialFiles[0].cElems; i++) { IntPtr specialPathIntPtr = Marshal.ReadIntPtr(specialFiles[0].pElems, i * IntPtr.Size); String specialPath = Marshal.PtrToStringAuto(specialPathIntPtr); sccFiles.Add(specialPath); Marshal.FreeCoTaskMem(specialPathIntPtr); } if (specialFiles[0].cElems > 0) { Marshal.FreeCoTaskMem(specialFiles[0].pElems); } } } Marshal.FreeCoTaskMem(pathIntPtr); } if (pathStr[0].cElems > 0) { Marshal.FreeCoTaskMem(pathStr[0].pElems); } } } return sccFiles; }
public int GetSccFiles(uint itemid, Microsoft.VisualStudio.OLE.Interop.CALPOLESTR[] pCaStringsOut, Microsoft.VisualStudio.OLE.Interop.CADWORD[] pCaFlagsOut) { if (itemid == VSConstants.VSITEMID_ROOT) { string solutionFilename = SelectionUtils.GetSolutionFileName(_context); if (!string.IsNullOrEmpty(solutionFilename)) pCaStringsOut[0] = CreateCALPOLESTR(new string[] { solutionFilename }); else pCaStringsOut[0] = new CALPOLESTR(); pCaFlagsOut[0].cElems = 0; pCaFlagsOut[0].pElems = IntPtr.Zero; return VSConstants.S_OK; } return VSConstants.E_NOTIMPL; }
public static CALPOLESTR CreateCALPOLESTR(IList<string> strings) { CALPOLESTR calpolStr = new CALPOLESTR(); if (strings != null) { // Demand unmanaged permissions in order to access unmanaged memory. new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand(); calpolStr.cElems = (uint)strings.Count; int size = Marshal.SizeOf(typeof(IntPtr)); calpolStr.pElems = Marshal.AllocCoTaskMem(strings.Count * size); IntPtr ptr = calpolStr.pElems; foreach (string aString in strings) { IntPtr tempPtr = Marshal.StringToCoTaskMemUni(aString); Marshal.WriteIntPtr(ptr, tempPtr); ptr = new IntPtr(ptr.ToInt64() + size); } } return calpolStr; }
//[CLSCompliant(false)] static bool GetSccFiles(IVsHierarchy hierarchy, IVsSccProject2 sccProject, uint id, out string[] files, bool includeSpecial, bool includeNoScc, IDictionary<string, uint> map) { int[] flags; files = null; if (!GetSccFiles(hierarchy, sccProject, id, out files, out flags, includeNoScc, map)) return false; else if (flags == null || sccProject == null || !includeSpecial) return true; int n = Math.Min(files.Length, flags.Length); List<string> allFiles = new List<string>(files); for (int i = 0; i < n; i++) { if (0 != (flags[i] & (int)tagVsSccFilesFlags.SFF_HasSpecialFiles)) { CALPOLESTR[] str = new CALPOLESTR[1]; CADWORD[] dw = new CADWORD[1]; if (ErrorHandler.Succeeded(sccProject.GetSccSpecialFiles(id, allFiles[i], str, dw))) { files = GetFileNamesFromOleBuffer(str, true); GetFlagsFromOleBuffer(dw, true); // Free the flags (No need to parse at this time) if (files != null && files.Length > 0) allFiles.AddRange(files); } } } files = allFiles.ToArray(); return true; }
//[CLSCompliant(false)] public static bool GetSccFiles(IVsHierarchy hierarchy, IVsSccProject2 sccProject, uint id, out string[] files, out int[] flags, bool includeNoScc, IDictionary<string, uint> map) { if (hierarchy == null) throw new ArgumentNullException("hierarchy"); int hr; bool ok = false; files = null; flags = null; try { if (sccProject != null) { CALPOLESTR[] str = new CALPOLESTR[1]; CADWORD[] dw = new CADWORD[1]; if (ErrorHandler.Succeeded(hr = sccProject.GetSccFiles(id, str, dw))) { files = GetFileNamesFromOleBuffer(str, true); flags = GetFlagsFromOleBuffer(dw, true); if (!includeNoScc || files.Length > 0) return ok = true; // We have a result else ok = true; // Try the GetMkDocument route to find an alternative } else if (hr != VSConstants.E_NOTIMPL) return false; // } // If sccProject2.GetSccFiles() returns E_NOTIMPL we must try GetMkDocument // We also try this if the item does not implement IVsSccProject2 IVsProject project = hierarchy as IVsProject; if (project != null) { string mkDocument; if (ErrorHandler.Succeeded(project.GetMkDocument(id, out mkDocument))) { if (!IsValidPath(mkDocument)) files = new string[0]; else files = new string[] { mkDocument }; return true; } return ok; // No need to check our interface for projects } if (hierarchy is IVsSolution) { return ok; // Will fail in GetCanonicalName in VS2008 SP1 Beta 1 } string name; try { if (ErrorHandler.Succeeded(hierarchy.GetCanonicalName(id, out name))) { if (IsValidPath(name, true)) { files = new string[] { name }; return true; } } } catch { } // Ok, this seems to error in some managed tree implementations like TFS :( return ok; } finally { if (ok && map != null && files != null) { foreach (string file in files) map[file] = id; } } }
public int GetSccSpecialFiles(uint itemid, string pszSccFile, CALPOLESTR[] pCaStringsOut, CADWORD[] pCaFlagsOut) { return VSConstants.E_NOTIMPL; }
/// <summary> /// Gets the files from an OLE String buffer and clears the buffer /// </summary> /// <param name="pathStr">The path STR.</param> /// <returns></returns> //[CLSCompliant(false)] internal static string[] GetFileNamesFromOleBuffer(CALPOLESTR[] pathStr, bool free) { int nEls = (int)pathStr[0].cElems; string[] files = new string[nEls]; for (int i = 0; i < nEls; i++) { IntPtr pathIntPtr = Marshal.ReadIntPtr(pathStr[0].pElems, i * IntPtr.Size); files[i] = Marshal.PtrToStringUni(pathIntPtr); if (free) Marshal.FreeCoTaskMem(pathIntPtr); } if (free && pathStr[0].pElems != IntPtr.Zero) Marshal.FreeCoTaskMem(pathStr[0].pElems); return files; }
public int RegisterSccProject(IVsSccProject2 pscp2Project, string pszSccProjectName, string pszSccAuxPath, string pszSccLocalPath, string pszProvider) { if (ExpectedProjectName != null) { AreEqual(ExpectedProjectName, pszSccProjectName); } if (ExpectedAuxPath != null) { AreEqual(ExpectedAuxPath, pszSccAuxPath); } if (ExpectedLocalPath != null) { AreEqual(ExpectedLocalPath, pszSccLocalPath); } if (ExpectedProvider != null) { AreEqual(ExpectedProvider, pszProvider); } var project = _loadedProjects[pscp2Project] = new ProjectInfo(pscp2Project, pszSccProjectName); CALPOLESTR[] str = new CALPOLESTR[1]; CADWORD[] cadword = new CADWORD[1]; // try it once w/ VSITEMID_ROOT, make sure we get back just the project. if (ErrorHandler.Failed(pscp2Project.GetSccFiles(VSConstants.VSITEMID_ROOT, str, cadword))) { Fail("Failed to get SccFiles"); } AreEqual(UnpackCALPOLESTR(str[0]).Length, 1); object propRes; if(ErrorHandler.Failed(((IVsHierarchy)pscp2Project).GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_FirstChild, out propRes))) { Fail("Failed to get first child of root node"); } int curSibling = (int)propRes; while(ErrorHandler.Succeeded(((IVsHierarchy)pscp2Project).GetProperty((uint)curSibling, (int)__VSHPROPID.VSHPROPID_NextSibling, out propRes))) { curSibling = (int)propRes; if(curSibling == unchecked((int)VSConstants.VSITEMID_NIL)) { break; } if (ErrorHandler.Failed(pscp2Project.GetSccFiles((uint)curSibling, str, cadword))) { Fail("Failed to get SccFiles"); } string[] files = UnpackCALPOLESTR(str[0]); foreach (var file in files) { uint pItemId; if (ErrorHandler.Succeeded(((IVsHierarchy)pscp2Project).ParseCanonicalName(file, out pItemId))) { project.Files.Add(file, new FileInfo(project, file, pItemId)); } } } // couple extra test cases to make sure we handle weird inputs... pscp2Project.GetSccFiles(VSConstants.VSITEMID_ROOT, null, null); pscp2Project.GetSccFiles(VSConstants.VSITEMID_ROOT, new CALPOLESTR[0], new CADWORD[0]); AreEqual(VSConstants.E_INVALIDARG, pscp2Project.GetSccFiles(VSConstants.VSITEMID_SELECTION, null, null)); AreEqual(VSConstants.E_INVALIDARG, pscp2Project.GetSccFiles(0xffffffff, null, null)); pscp2Project.GetSccSpecialFiles(VSConstants.VSITEMID_ROOT, "", str, cadword); pscp2Project.GetSccSpecialFiles(VSConstants.VSITEMID_ROOT, "", null, null); AreEqual(VSConstants.E_INVALIDARG, pscp2Project.GetSccSpecialFiles(VSConstants.VSITEMID_SELECTION, "", null, null)); AreEqual(VSConstants.E_INVALIDARG, pscp2Project.GetSccSpecialFiles(0xffffffff, "", null, null)); return VSConstants.S_OK; }
public int GetSccFiles(uint itemid, CALPOLESTR[] pCaStringsOut, CADWORD[] pCaFlagsOut) { if ((null == pCaStringsOut) || (0 == pCaStringsOut.Length)) throw new ArgumentNullException(); if ((null == pCaFlagsOut) || (0 == pCaFlagsOut.Length)) throw new ArgumentNullException(); pCaStringsOut[0] = new CALPOLESTR(); pCaStringsOut[0].cElems = 0; pCaStringsOut[0].pElems = IntPtr.Zero; pCaFlagsOut[0] = new CADWORD(); pCaFlagsOut[0].cElems = 0; pCaFlagsOut[0].pElems = IntPtr.Zero; string fileForNode = null; if (itemid == VSConstants.VSITEMID_ROOT) { fileForNode = _projFile; } else if (itemid >= 0 && itemid < _items.Count) { fileForNode = _items[(int)itemid]; } if (fileForNode != null) { // There is only one scc controllable file per each hierarchy node pCaStringsOut[0].cElems = 1; pCaStringsOut[0].pElems = Marshal.AllocCoTaskMem(IntPtr.Size); Marshal.WriteIntPtr(pCaStringsOut[0].pElems, Marshal.StringToCoTaskMemUni(fileForNode)); pCaFlagsOut[0].cElems = 1; pCaFlagsOut[0].pElems = Marshal.AllocCoTaskMem(sizeof(Int32)); Marshal.WriteInt32(pCaFlagsOut[0].pElems, 0); } return VSConstants.S_OK; }
public static string[] UnpackCALPOLESTR(CALPOLESTR strings) { if (strings.pElems != IntPtr.Zero) { string[] res = new string[strings.cElems]; int size = IntPtr.Size; for (int i = 0; i < res.Length; i++) { IntPtr strAddr = Marshal.ReadIntPtr(new IntPtr(strings.pElems.ToInt64() + size * i)); res[i] = Marshal.PtrToStringUni(strAddr); Marshal.FreeCoTaskMem(strAddr); } Marshal.FreeCoTaskMem(strings.pElems); return res; } return new string[0]; }
public int GetSccFiles(uint itemid, CALPOLESTR[] pCaStringsOut, CADWORD[] pCaFlagsOut) { HierarchyNode node = this.NodeFromItemId(itemid); if (node is Project || node is HierarchyItemNode){ string url = node.FullPath; pCaStringsOut[0].cElems = 1; IntPtr pElems = Marshal.AllocCoTaskMem(IntPtr.Size); IntPtr pElem = Marshal.StringToCoTaskMemAuto(url); Marshal.WriteIntPtr(pElems, pElem); pCaStringsOut[0].pElems = pElems; pCaFlagsOut[0].cElems = 0; return (int)HResult.S_OK; } return (int)HResult.E_NOTIMPL; }
private static string[] GetSpecialFiles(IVsSccProject2 project, uint itemId, string fileName) { var specialFiles = new CALPOLESTR[1]; var specialFlags = new CADWORD[1]; if (ErrorHandler.Succeeded(project.GetSccSpecialFiles(itemId, fileName, specialFiles, specialFlags))) { return GetFileNames(specialFiles[0]); } return new string[0]; }
private static string[] GetFileNames(CALPOLESTR array) { var files = new string[array.cElems]; for (int i = 0; i < files.Length; i++) { var pathPtr = Marshal.ReadIntPtr(array.pElems, i * IntPtr.Size); files[i] = Marshal.PtrToStringUni(pathPtr); Marshal.FreeCoTaskMem(pathPtr); } if (array.pElems != IntPtr.Zero) { Marshal.FreeCoTaskMem(array.pElems); } return files; }
public static string[] GetItemFiles(IVsSccProject2 project, uint itemId) { var itemFiles = new List<string>(); var files = new CALPOLESTR[1]; var flags = new CADWORD[1]; if (ErrorHandler.Succeeded(project.GetSccFiles(itemId, files, flags))) { var fileNames = GetFileNames(files[0]); for (int i = 0; i < files[0].cElems; i++) { itemFiles.Add(fileNames[i]); if (HasSpecialFiles(flags, i)) { itemFiles.AddRange(GetSpecialFiles(project, itemId, fileNames[i])); } } } return itemFiles.ToArray(); }