コード例 #1
0
        private async void WindowEvents_WindowActivated(Window gotFocus, Window lostFocus)
        {
            // Wire up reference for Caret events
            var textViewHost = GetCurrentViewHost();

            if (textViewHost != null)
            {
                _control.TextView = textViewHost.TextView;
                textViewHost.TextView.Caret.PositionChanged += Caret_PositionChanged;

                if (Settings.Default.ShowHistoryIndicators)
                {
                    textViewHost.TextView.TextBuffer.ChangedLowPriority += TextBuffer_ChangedLowPriority;
                }

                // Subscribe to Outlining events
                var outliningManagerService = OutliningHelper.GetOutliningManagerService(Package as IServiceProvider);
                var outliningManager        = OutliningHelper.GetOutliningManager(outliningManagerService, GetCurrentViewHost().TextView);

                if (outliningManager != null && outliningManagerService != null)
                {
                    _control.OutliningManagerService   = outliningManagerService;
                    outliningManager.RegionsExpanded  -= OutliningManager_RegionsExpanded;
                    outliningManager.RegionsExpanded  += OutliningManager_RegionsExpanded;
                    outliningManager.RegionsCollapsed -= OutliningManager_RegionsCollapsed;
                    outliningManager.RegionsCollapsed += OutliningManager_RegionsCollapsed;
                }
            }

            await UpdateDocumentAsync(gotFocus, gotFocus != lostFocus);
        }
コード例 #2
0
        public IEnumerable <ITagSpan <IOutliningRegionTag> > GetTags(NormalizedSnapshotSpanCollection spans)
        {
            if (this.buffer.EditInProgress || spans.Count == 0)
            {
                yield break;
            }
            List <Region> currentRegions  = this.regions;
            ITextSnapshot currentSnapshot = this.snapshot;
            SnapshotSpan  entire          = new SnapshotSpan(spans[0].Start, spans[spans.Count - 1].End)
                                            .TranslateTo(currentSnapshot, SpanTrackingMode.EdgeExclusive);
            int startLineNumber = entire.Start.GetContainingLine().LineNumber;
            int endLineNumber   = entire.End.GetContainingLine().LineNumber;

            foreach (var region in currentRegions)
            {
                if (region.StartLine <= endLineNumber &&
                    region.EndLine >= startLineNumber)
                {
                    var startLine = currentSnapshot.GetLineFromLineNumber(region.StartLine);
                    var endLine   = currentSnapshot.GetLineFromLineNumber(region.EndLine);

                    string ellipsis  = OutliningHelper.Ellipsis;
                    string hoverText = OutliningHelper.GetHoverText(currentSnapshot, region);
                    //the region starts at the beginning of the "[", and goes until the *end* of the line that contains the "]".
                    yield return(new TagSpan <IOutliningRegionTag>(
                                     new SnapshotSpan(startLine.Start + region.StartOffset,
                                                      endLine.End),
                                     new OutliningRegionTag(false, true, ellipsis, hoverText)));
                }
            }
        }
コード例 #3
0
        private void WindowEvents_WindowActivated(Window gotFocus, Window lostFocus)
        {
            // Wire up reference for Caret events
            var textViewHost = GetCurrentViewHost();

            if (textViewHost != null)
            {
                _control.TextView = textViewHost.TextView;
                textViewHost.TextView.Caret.PositionChanged += Caret_PositionChanged;

                // Subscribe to Outlining events
                var outliningManager = OutliningHelper.GetManager(Package as CodeNavToolWindowPackage, GetCurrentViewHost().TextView);
                if (outliningManager == null)
                {
                    return;
                }
                _control.OutliningManager          = outliningManager;
                outliningManager.RegionsExpanded  -= OutliningManager_RegionsExpanded;
                outliningManager.RegionsExpanded  += OutliningManager_RegionsExpanded;
                outliningManager.RegionsCollapsed -= OutliningManager_RegionsCollapsed;
                outliningManager.RegionsCollapsed += OutliningManager_RegionsCollapsed;
            }

            UpdateDocument(gotFocus, gotFocus != lostFocus);
        }
コード例 #4
0
        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {
                var result = e.Result as BackgroundWorkerResult;

                if (result?.CodeItems == null)
                {
                    LogHelper.Log($"CodeNav for '{DocumentHelper.GetName(_window)}' updated, no results");
                    return;
                }

                // Filter all null items from the code document
                SyntaxMapper.FilterNullItems(result.CodeItems);

                // Do we need to update the DataContext?
                var areEqual = AreDocumentsEqual(CodeDocumentViewModel.CodeDocument, result.CodeItems);
                if (result.ForceUpdate == false && areEqual)
                {
                    LogHelper.Log($"CodeNav for '{DocumentHelper.GetName(_window)}' updated, document did not change");

                    // Should the margin be shown and are there any items to show, if not hide the margin
                    VisibilityHelper.SetMarginWidth(_column, CodeDocumentViewModel.CodeDocument);

                    return;
                }

                // Set the new list of codeitems as DataContext
                CodeDocumentViewModel.CodeDocument = result.CodeItems;
                _cache = result.CodeItems;

                // Set currently active codeitem
                HighlightHelper.SetForeground(CodeDocumentViewModel.CodeDocument);

                // Should the margin be shown and are there any items to show, if not hide the margin
                VisibilityHelper.SetMarginWidth(_column, CodeDocumentViewModel.CodeDocument);

                // Apply current visibility settings to the document
                VisibilityHelper.SetCodeItemVisibility(CodeDocumentViewModel.CodeDocument);

                // Sync all regions
                OutliningHelper.SyncAllRegions(OutliningManager, TextView, CodeDocumentViewModel.CodeDocument);

                // Sort items
                CodeDocumentViewModel.SortOrder = Settings.Default.SortOrder;
                SortHelper.Sort(CodeDocumentViewModel);

                // Apply bookmarks
                LoadBookmarksFromStorage();
                BookmarkHelper.ApplyBookmarks(CodeDocumentViewModel, Dte?.Solution?.FileName);

                LogHelper.Log($"CodeNav for '{DocumentHelper.GetName(_window)}' updated");
            }
            catch (ObjectDisposedException ex)
            {
                LogHelper.Log($"CodeNav: RunWorkerCompleted exception: {ex.Message}");
                LogHelper.Log("RunWorkerCompleted exception", ex);
            }
        }
コード例 #5
0
        /// <summary>
        /// Creates an <see cref="IWpfTextViewMargin"/> for the given <see cref="IWpfTextViewHost"/>.
        /// </summary>
        /// <param name="wpfTextViewHost">The <see cref="IWpfTextViewHost"/> for which to create the <see cref="IWpfTextViewMargin"/>.</param>
        /// <param name="marginContainer">The margin that will contain the newly-created margin.</param>
        /// <returns>The <see cref="IWpfTextViewMargin"/>.
        /// The value may be null if this <see cref="IWpfTextViewMarginProvider"/> does not participate for this context.
        /// </returns>
        public IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost, IWpfTextViewMargin marginContainer)
        {
            if (Settings.Default.MarginSide != MarginSideEnum.Right)
            {
                return(null);
            }

            var dte = (DTE)_serviceProvider.GetService(typeof(DTE));
            var outliningManager = OutliningHelper.GetManager(_serviceProvider, wpfTextViewHost.TextView);

            Logger.Initialize(_serviceProvider, "CodeNav");
            var codeNav = new CodeNavMargin(wpfTextViewHost, dte, outliningManager, Workspace, MarginSideEnum.Right);

            return(codeNav);
        }
コード例 #6
0
        public static IWpfTextViewMargin CreateMargin(IWpfTextViewHost wpfTextViewHost,
                                                      VisualStudioWorkspace visualStudioWorkspace, IServiceProvider serviceProvider, MarginSideEnum side)
        {
            if (Settings.Default.MarginSide != side)
            {
                return(null);
            }

            System.Windows.Threading.Dispatcher.CurrentDispatcher.VerifyAccess();

            var dte = (DTE)serviceProvider.GetService(typeof(DTE));
            var outliningManagerService = OutliningHelper.GetOutliningManagerService(serviceProvider);

            var codeNav = new CodeNavMargin(wpfTextViewHost, dte, outliningManagerService, visualStudioWorkspace, side);

            return(codeNav);
        }
コード例 #7
0
        private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var result = e.Result as BackgroundWorkerResult;

            if (result?.CodeItems == null)
            {
                LogHelper.Log($"CodeNav for '{_window.Document.Name}' updated, no results");
                return;
            }

            // Filter all null items from the code document
            SyntaxMapper.FilterNullItems(result.CodeItems);

            // Do we need to update the DataContext?
            var areEqual = AreDocumentsEqual(CodeDocumentViewModel.CodeDocument, result.CodeItems);

            if (result.ForceUpdate == false && areEqual)
            {
                LogHelper.Log($"CodeNav for '{_window.Document.Name}' updated, document did not change");
                return;
            }

            // Set the new list of codeitems as DataContext
            CodeDocumentViewModel.CodeDocument = result.CodeItems;
            _cache = result.CodeItems;

            // Set currently active codeitem
            HighlightHelper.SetForeground(CodeDocumentViewModel.CodeDocument);

            // Are there any items to show, if not hide the margin
            VisibilityHelper.SetMarginWidth(_column, CodeDocumentViewModel.CodeDocument);

            // Apply current visibility settings to the document
            VisibilityHelper.SetCodeItemVisibility(CodeDocumentViewModel.CodeDocument);

            // Sync all regions
            OutliningHelper.SyncAllRegions(_outliningManager, _textView, CodeDocumentViewModel.CodeDocument);

            LogHelper.Log($"CodeNav for '{_window.Document.Name}' updated");
        }
コード例 #8
0
ファイル: CodeNavMargin.cs プロジェクト: zhihuiweilai/CodeNav
        public CodeNavMargin(IWpfTextViewHost textViewHost, DTE dte, IOutliningManagerService outliningManagerService,
                             VisualStudioWorkspace workspace, MarginSideEnum side)
        {
            // Wire up references for the event handlers in RegisterEvents
            _dte      = dte;
            _textView = textViewHost.TextView;
            _window   = GetWindow(textViewHost, dte);
            _outliningManagerService = outliningManagerService;
            _outliningManager        = OutliningHelper.GetOutliningManager(outliningManagerService, _textView);
            _workspace = workspace;
            MarginSide = side;

            // If we can not find the window we belong to we can not do anything
            if (_window == null)
            {
                return;
            }

            // Add the view/content to the margin area
            if (side == MarginSideEnum.Top)
            {
                _codeNavGrid = CreateGridTop(textViewHost);
            }
            else
            {
                _codeNavGrid   = CreateGrid(textViewHost);
                _codeNavColumn = _codeNavGrid.ColumnDefinitions[Settings.Default.MarginSide == MarginSideEnum.Left ? 0 : 2];
            }

            Children.Add(_codeNavGrid);

            System.Windows.Threading.Dispatcher.CurrentDispatcher.VerifyAccess();

            RegisterEvents();

            UpdateSettings();
        }
コード例 #9
0
        public async Task UpdateDocumentAsync(bool forceUpdate = false)
        {
            await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var activeDocument = DocumentHelper.GetActiveDocument(Dte);

            if (activeDocument == null)
            {
                return;
            }

            CodeDocumentViewModel.FilePath = activeDocument.FullName;

            // Do we need to change the side where the margin is displayed
            if (_margin?.MarginSide != null &&
                _margin?.MarginSide != Settings.Default.MarginSide &&
                Dte != null)
            {
                var filename = activeDocument.FullName;
                Dte.ExecuteCommand("File.Close");
                Dte.ExecuteCommand("File.OpenFile", filename);
            }

            try
            {
                if (forceUpdate)
                {
                    _cache = null;
                    CodeDocumentViewModel.CodeDocument.Clear();
                }

                // Do we have a cached version of this document
                if (_cache != null)
                {
                    CodeDocumentViewModel.CodeDocument = _cache;
                }

                // If not show a loading item
                if (!CodeDocumentViewModel.CodeDocument.Any())
                {
                    CodeDocumentViewModel.CodeDocument = CreateLoadingItem();
                }

                var codeItems = await SyntaxMapper.MapDocumentAsync(activeDocument, this, _workspace);

                if (codeItems == null)
                {
                    // CodeNav for document updated, no results
                    return;
                }

                // Filter all null items from the code document
                SyntaxMapper.FilterNullItems(codeItems);

                // Sort items
                CodeDocumentViewModel.SortOrder = Settings.Default.SortOrder;
                SortHelper.Sort(codeItems, Settings.Default.SortOrder);

                // Set currently active codeitem
                HighlightHelper.SetForeground(codeItems);

                // Set the new list of codeitems as DataContext
                CodeDocumentViewModel.CodeDocument = codeItems;
                _cache = codeItems;

                // Apply current visibility settings to the document
                VisibilityHelper.SetCodeItemVisibility(CodeDocumentViewModel);

                // Apply bookmarks
                LoadBookmarksFromStorage();
                BookmarkHelper.ApplyBookmarks(CodeDocumentViewModel, Dte?.Solution?.FileName);

                // Apply history items
                LoadHistoryItemsFromStorage();
                HistoryHelper.ApplyHistoryIndicator(CodeDocumentViewModel);
            }
            catch (Exception e)
            {
                LogHelper.Log("Error running UpdateDocument", e);
            }

            try
            {
                // Sync all regions
                OutliningHelper.SyncAllRegions(OutliningManagerService, TextView, CodeDocumentViewModel.CodeDocument);

                // Should the margin be shown and are there any items to show, if not hide the margin
                VisibilityHelper.SetMarginHeight(_row, CodeDocumentViewModel.CodeDocument);
            }
            catch (Exception e)
            {
                LogHelper.Log("Error finishing UpdateDocument", e);
            }
        }
コード例 #10
0
 public void ToggleAll(bool isExpanded, List <CodeItem> root = null)
 {
     OutliningHelper.ToggleAll(root ?? CodeDocumentViewModel.CodeDocument, isExpanded);
 }
コード例 #11
0
 public void RegionsExpanded(RegionsExpandedEventArgs e) =>
 OutliningHelper.RegionsExpanded(e, CodeDocumentViewModel.CodeDocument);
コード例 #12
0
 public void ToggleAllRegions(bool isExpanded) =>
 OutliningHelper.SetAllRegions(CodeDocumentViewModel.CodeDocument, isExpanded);