internal IEnumerable <ICollapsible> GetCollapsiblesForCurrentSelection(IOutliningManager outliningManager, ITextView textView) { if (outliningManager == null) { return(null); } // Try for all collapsibles which are contained within the selection span SnapshotSpan selectionSnapshotSpan = textView.Selection.StreamSelectionSpan.SnapshotSpan; ITextSnapshot selectionSnapshot = selectionSnapshotSpan.Snapshot; var intersectingCollapsibles = outliningManager.GetAllRegions(selectionSnapshotSpan); var filteredCollapsibles = intersectingCollapsibles.Where(collapsible => selectionSnapshotSpan.Contains(collapsible.Extent.GetSpan(selectionSnapshot))); var tornoffCollapsibles = GetTornoffCollapsibles(filteredCollapsibles); if (tornoffCollapsibles != null) { return(tornoffCollapsibles); } // Try for innermost collapsibles which intersect and start within the selection span filteredCollapsibles = intersectingCollapsibles.Where(collapsible => selectionSnapshotSpan.Contains(collapsible.Extent.GetSpan(selectionSnapshot).Start)); tornoffCollapsibles = GetInnermostCollapsibles(selectionSnapshot, filteredCollapsibles); if (tornoffCollapsibles != null) { return(tornoffCollapsibles); } // Try for all collapsibles which are contained within the selection's first line ITextSnapshotLine selectionStartLine = selectionSnapshot.GetLineFromPosition(selectionSnapshotSpan.Start); SnapshotSpan selectionFirstLine = new SnapshotSpan(selectionSnapshot, selectionStartLine.Start, selectionStartLine.Length); intersectingCollapsibles = outliningManager.GetAllRegions(selectionFirstLine); filteredCollapsibles = intersectingCollapsibles.Where(collapsible => selectionFirstLine.Contains(collapsible.Extent.GetSpan(selectionSnapshot))); tornoffCollapsibles = GetTornoffCollapsibles(filteredCollapsibles); if (tornoffCollapsibles != null) { return(tornoffCollapsibles); } // Try for innermost collapsibles which intersect and start within the selection's first line filteredCollapsibles = intersectingCollapsibles.Where(collapsible => selectionFirstLine.Contains(collapsible.Extent.GetSpan(selectionSnapshot).Start)); tornoffCollapsibles = GetInnermostCollapsibles(selectionSnapshot, filteredCollapsibles); if (tornoffCollapsibles != null) { return(tornoffCollapsibles); } // Try for innermost collapsibles which simply intersect the selection's first line tornoffCollapsibles = GetInnermostCollapsibles(selectionSnapshot, intersectingCollapsibles); if (tornoffCollapsibles != null) { return(tornoffCollapsibles); } return(null); }
/// <summary> /// Attempts to find a <see cref="ICollapsible" /> associated with the specified <see /// cref="IMembers" />. /// </summary> /// <param name="item">The IMembers CodeItem.</param> /// <param name="manager">The outlining manager to get all regions</param> /// <param name="textView">The textview to find collapsibles in</param> /// <returns>The <see cref="ICollapsible" /> on the same starting line, otherwise null.</returns> private static ICollapsible FindCollapsibleFromCodeItem(CodeItem item, IOutliningManager manager, IWpfTextView textView) { if (item.Kind == CodeItemKindEnum.ImplementedInterface) { return(null); } if (item.StartLine > textView.TextBuffer.CurrentSnapshot.LineCount) { return(null); } try { var snapshotLine = textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(item.StartLine); var collapsibles = manager.GetAllRegions(snapshotLine.Extent); return((from collapsible in collapsibles let startLine = GetStartLineForCollapsible(collapsible) where startLine == item.StartLine select collapsible).FirstOrDefault()); } catch (ArgumentOutOfRangeException e) { LogHelper.Log($"FindCollapsibleFromCodeItem failed for item {item.Name}, exception: {e.Message}"); return(null); } catch (ObjectDisposedException e) { LogHelper.Log($"FindCollapsibleFromCodeItem failed because of disposed {e.ObjectName}"); return(null); } }
public void VsTextViewCreated(IVsTextView textViewAdapter) { IComponentModel componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)); if (componentModel != null) { IOutliningManagerService outliningManagerService = componentModel.GetService <IOutliningManagerService>(); _editorAdaptersFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>(); if (outliningManagerService != null) { if (textViewAdapter != null && _editorAdaptersFactoryService != null) { var textView = _editorAdaptersFactoryService.GetWpfTextView(textViewAdapter); var snapshot = textView.TextSnapshot; var snapshotSpan = new Microsoft.VisualStudio.Text.SnapshotSpan(snapshot, new Microsoft.VisualStudio.Text.Span(0, snapshot.Length)); _outliningManager = outliningManagerService.GetOutliningManager(textView); var regions = _outliningManager.GetAllRegions(snapshotSpan); foreach (var reg in regions) { _outliningManager.TryCollapse(reg); } } } } }
/// <summary> /// Attempts to find a <see cref="ICollapsible" /> associated with the specified <see /// cref="ICodeItemParent" />. /// </summary> /// <param name="parent">The code item parent.</param> /// <returns>The <see cref="ICollapsible" /> on the same starting line, otherwise null.</returns> private ICollapsible FindCollapsibleFromCodeItemParent(ICodeItemParent parent) { if (_outliningManager == null || _wpfTextView == null) { return(null); } var snapshotLine = _wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(parent.StartLine); var collapsibles = _outliningManager.GetAllRegions(snapshotLine.Extent); return((from collapsible in collapsibles let startLine = GetStartLineForCollapsible(collapsible) where startLine == parent.StartLine select collapsible).FirstOrDefault()); }
/// <summary> /// Attempts to find a <see cref="ICollapsible" /> associated with the specified <see /// cref="IMembers" />. /// </summary> /// <param name="item">The IMembers CodeItem.</param> /// <param name="manager">The outlining manager to get all regions</param> /// <param name="textView">The textview to find collapsibles in</param> /// <returns>The <see cref="ICollapsible" /> on the same starting line, otherwise null.</returns> private static ICollapsible FindCollapsibleFromCodeItem(CodeItem item, IOutliningManager manager, IWpfTextView textView) { if (item.Kind == CodeItemKindEnum.ImplementedInterface) { return(null); } if (item.StartLine > textView.TextBuffer.CurrentSnapshot.LineCount) { return(null); } var snapshotLine = textView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(item.StartLine); var collapsibles = manager.GetAllRegions(snapshotLine.Extent); return((from collapsible in collapsibles let startLine = GetStartLineForCollapsible(collapsible) where startLine == item.StartLine select collapsible).FirstOrDefault()); }
private void ToggleCurrent() { var caretPosition = _textView.Caret.Position.BufferPosition; var snapshot = _textView.TextBuffer.CurrentSnapshot; var span = new SnapshotSpan(snapshot, new Span(caretPosition, 0)); var regions = OutliningManager.GetAllRegions(span); // Find innermost one ICollapsible region = null; int regionStart = 0; int regionEnd = snapshot.Length; foreach (ICollapsible c in regions) { int start = c.Extent.GetStartPoint(snapshot); int end = c.Extent.GetEndPoint(snapshot); if (start >= regionStart && end < regionEnd) { regionStart = start; regionEnd = end; region = c; } } if (region != null) { if (region.IsCollapsed) { OutliningManager.Expand(region as ICollapsed); } else { OutliningManager.TryCollapse(region); } } }
/// <summary> /// Attempts to find a <see cref="ICollapsible" /> associated with the specified <see /// cref="ICodeItemParent" />. /// </summary> /// <param name="parent">The code item parent.</param> /// <returns>The <see cref="ICollapsible" /> on the same starting line, otherwise null.</returns> private ICollapsible FindCollapsibleFromCodeItemParent(ICodeItemParent parent) { if (_outliningManager == null || _wpfTextView == null) { return(null); } try { var snapshotLine = _wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(parent.StartLine); var collapsibles = _outliningManager.GetAllRegions(snapshotLine.Extent); return((from collapsible in collapsibles let startLine = GetStartLineForCollapsible(collapsible) where startLine == parent.StartLine select collapsible).FirstOrDefault()); } catch (Exception ex) { OutputWindowHelper.ExceptionWriteLine("Unable to find collapsible from ICodeItemParent", ex); return(null); } }