public IEnumerable <HierarchyItemPair> GetSelection() { return(jtf.Run(async() => { // The VS selection operations must be performed on the UI thread. await jtf.SwitchToMainThreadAsync(); var selHier = IntPtr.Zero; var selContainer = IntPtr.Zero; try { // Get the current project hierarchy, project item, and selection container for the current selection // If the selection spans multiple hierarchies, hierarchyPtr is Zero ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(out selHier, out var selId, out var selMulti, out selContainer)); // There may be no selection at all. if (selMulti == null && selHier == IntPtr.Zero) { return Enumerable.Empty <HierarchyItemPair>(); } // This is a single item selection. if (selMulti == null) { return new[] { new HierarchyItemPair( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(selHier, typeof(IVsHierarchy)), selId) }; } // This is a multiple item selection. ErrorHandler.ThrowOnFailure(selMulti.GetSelectionInfo(out var selCount, out var singleHier)); var selection = new VSITEMSELECTION[selCount]; ErrorHandler.ThrowOnFailure(selMulti.GetSelectedItems(0, selCount, selection)); return selection.Where(sel => sel.pHier != null) .Select(sel => new HierarchyItemPair(sel.pHier, sel.itemid)) .ToArray(); } finally { if (selHier != IntPtr.Zero) { Marshal.Release(selHier); } if (selContainer != IntPtr.Zero) { Marshal.Release(selContainer); } } })); }
public IEnumerable <IVsHierarchyItem> GetSelection() { return(jtf.Run(async() => { await jtf.SwitchToMainThreadAsync(); var selHier = IntPtr.Zero; try { ErrorHandler.ThrowOnFailure(hierarchyWindow.GetCurrentSelection(out selHier, out var selId, out var selMulti)); // There may be no selection at all. if (selMulti == null && selHier == IntPtr.Zero) { return Enumerable.Empty <IVsHierarchyItem>(); } // This is a single item selection. if (selMulti == null) { return new[] { hierarchyManager.GetHierarchyItem( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(selHier, typeof(IVsHierarchy)), selId) }; } // This is a multiple item selection. ErrorHandler.ThrowOnFailure(selMulti.GetSelectionInfo(out var selCount, out var singleHier)); var selection = new VSITEMSELECTION[selCount]; ErrorHandler.ThrowOnFailure(selMulti.GetSelectedItems(0, selCount, selection)); return selection.Where(sel => sel.pHier != null) .Select(sel => hierarchyManager.GetHierarchyItem(sel.pHier, sel.itemid)) .ToArray(); } finally { if (selHier != IntPtr.Zero) { Marshal.Release(selHier); } } })); }
public IEnumerable<IVsHierarchyItem> GetSelection () { return asyncManager.Run (async () => { await asyncManager.SwitchToMainThread (); var selHier = IntPtr.Zero; uint selId; IVsMultiItemSelect selMulti; try { ErrorHandler.ThrowOnFailure (hierarchyWindow.GetCurrentSelection (out selHier, out selId, out selMulti)); // There may be no selection at all. if (selMulti == null && selHier == IntPtr.Zero) return Enumerable.Empty<IVsHierarchyItem> (); // This is a single item selection. if (selMulti == null) { return new[] { hierarchyManager.GetHierarchyItem ( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown (selHier, typeof (IVsHierarchy)), selId) }; } // This is a multiple item selection. uint selCount; int singleHier; ErrorHandler.ThrowOnFailure (selMulti.GetSelectionInfo (out selCount, out singleHier)); var selection = new VSITEMSELECTION[selCount]; ErrorHandler.ThrowOnFailure (selMulti.GetSelectedItems (0, selCount, selection)); return selection.Where (sel => sel.pHier != null) .Select (sel => hierarchyManager.GetHierarchyItem (sel.pHier, sel.itemid)) .ToArray (); } finally { if (selHier != IntPtr.Zero) Marshal.Release (selHier); } }); }
public static IEnumerable<Tuple<IVsHierarchy, uint>> GetSelection(this IVsMonitorSelection monitorSelection, IUIThread uiThread, IVsHierarchy solution) { var hierarchyPtr = IntPtr.Zero; var selectionContainer = IntPtr.Zero; return uiThread.Invoke(() => { try { // Get the current project hierarchy, project item, and selection container for the current selection // If the selection spans multiple hierarchies, hierarchyPtr is Zero uint itemid; IVsMultiItemSelect multiItemSelect = null; ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainer)); if (itemid == VSConstants.VSITEMID_NIL) return Enumerable.Empty<Tuple<IVsHierarchy, uint>>(); if (itemid == VSConstants.VSITEMID_ROOT) { if (hierarchyPtr == IntPtr.Zero) return new[] { Tuple.Create(solution, VSConstants.VSITEMID_ROOT) }; else return new[] { Tuple.Create( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)), VSConstants.VSITEMID_ROOT) }; } if (itemid != VSConstants.VSITEMID_SELECTION) return new[] { Tuple.Create( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)), itemid) }; // This is a multiple item selection. uint numberOfSelectedItems; int isSingleHierarchyInt; ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectionInfo(out numberOfSelectedItems, out isSingleHierarchyInt)); var isSingleHierarchy = (isSingleHierarchyInt != 0); var vsItemSelections = new VSITEMSELECTION[numberOfSelectedItems]; var flags = (isSingleHierarchy) ? (uint)__VSGSIFLAGS.GSI_fOmitHierPtrs : 0; ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectedItems(flags, numberOfSelectedItems, vsItemSelections)); return vsItemSelections.Where(sel => sel.pHier != null) // NOTE: we can return lazy results here, since // the GetSelectedItems has already returned in the UI thread // the array of results. We're just delaying the creation of the tuples // in case they aren't all needed. .Select(sel => Tuple.Create(sel.pHier, sel.itemid)); } finally { if (hierarchyPtr != IntPtr.Zero) { Marshal.Release(hierarchyPtr); } if (selectionContainer != IntPtr.Zero) { Marshal.Release(selectionContainer); } } }); }
public static IEnumerable <Tuple <IVsHierarchy, uint> > GetSelection(this IVsMonitorSelection monitorSelection, IUIThread uiThread, IVsHierarchy solution) { var hierarchyPtr = IntPtr.Zero; var selectionContainer = IntPtr.Zero; return(uiThread.Invoke(() => { try { // Get the current project hierarchy, project item, and selection container for the current selection // If the selection spans multiple hierarchies, hierarchyPtr is Zero uint itemid; IVsMultiItemSelect multiItemSelect = null; ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainer)); if (itemid == VSConstants.VSITEMID_NIL) { return Enumerable.Empty <Tuple <IVsHierarchy, uint> >(); } if (itemid == VSConstants.VSITEMID_ROOT) { if (hierarchyPtr == IntPtr.Zero) { return new[] { Tuple.Create(solution, VSConstants.VSITEMID_ROOT) } } ; else { return new[] { Tuple.Create( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)), VSConstants.VSITEMID_ROOT) } }; } if (itemid != VSConstants.VSITEMID_SELECTION) { return new[] { Tuple.Create( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown(hierarchyPtr, typeof(IVsHierarchy)), itemid) } } ; // This is a multiple item selection. uint numberOfSelectedItems; int isSingleHierarchyInt; ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectionInfo(out numberOfSelectedItems, out isSingleHierarchyInt)); var isSingleHierarchy = (isSingleHierarchyInt != 0); var vsItemSelections = new VSITEMSELECTION[numberOfSelectedItems]; var flags = (isSingleHierarchy) ? (uint)__VSGSIFLAGS.GSI_fOmitHierPtrs : 0; ErrorHandler.ThrowOnFailure(multiItemSelect.GetSelectedItems(flags, numberOfSelectedItems, vsItemSelections)); return vsItemSelections.Where(sel => sel.pHier != null) // NOTE: we can return lazy results here, since // the GetSelectedItems has already returned in the UI thread // the array of results. We're just delaying the creation of the tuples // in case they aren't all needed. .Select(sel => Tuple.Create(sel.pHier, sel.itemid)); } finally { if (hierarchyPtr != IntPtr.Zero) { Marshal.Release(hierarchyPtr); } if (selectionContainer != IntPtr.Zero) { Marshal.Release(selectionContainer); } } })); }
public IEnumerable<HierarchyItemPair> GetSelection () { return asyncManager.Run (async () => { // The VS selection operations must be performed on the UI thread. await asyncManager.SwitchToMainThread (); var selHier = IntPtr.Zero; var selContainer = IntPtr.Zero; uint selId; IVsMultiItemSelect selMulti; try { // Get the current project hierarchy, project item, and selection container for the current selection // If the selection spans multiple hierarchies, hierarchyPtr is Zero ErrorHandler.ThrowOnFailure (monitorSelection.GetCurrentSelection (out selHier, out selId, out selMulti, out selContainer)); // There may be no selection at all. if (selMulti == null && selHier == IntPtr.Zero) return Enumerable.Empty<HierarchyItemPair> (); // This is a single item selection. if (selMulti == null) { return new[] { new HierarchyItemPair ( (IVsHierarchy)Marshal.GetTypedObjectForIUnknown (selHier, typeof (IVsHierarchy)), selId) }; } // This is a multiple item selection. uint selCount; int singleHier; ErrorHandler.ThrowOnFailure (selMulti.GetSelectionInfo (out selCount, out singleHier)); var selection = new VSITEMSELECTION[selCount]; ErrorHandler.ThrowOnFailure (selMulti.GetSelectedItems (0, selCount, selection)); return selection.Where (sel => sel.pHier != null) .Select (sel => new HierarchyItemPair (sel.pHier, sel.itemid)) .ToArray (); } finally { if (selHier != IntPtr.Zero) { Marshal.Release (selHier); } if (selContainer != IntPtr.Zero) { Marshal.Release (selContainer); } } }); }