Beispiel #1
0
        /// <summary>
        /// Scrolls an editor, only if it is already open
        /// </summary>
        /// <param name="fileUri"></param>
        /// <param name="scrollTo"></param>
        /// <param name="atTop"></param>
        public void ScrollEditor(Uri fileUri, int?scrollTo = null, int?deltaPixels = null, bool?atTop = false)
        {
            if (scrollTo == null || scrollTo.Value < 0)
            {
                return;
            }

            using (var metrics = Log.WithMetrics($"{nameof(ScrollEditor)} {fileUri} scrollTo={scrollTo} atTop={atTop}")) {
                try {
                    var localPath     = fileUri.ToLocalPath();
                    var textViewCache = _componentModel.GetService <IWpfTextViewCache>();
                    if (!textViewCache.TryGetValue(localPath, out var wpfTextView) || wpfTextView == null)
                    {
                        return;
                    }

                    if (deltaPixels != null)
                    {
                        wpfTextView.ViewScroller.ScrollViewportVerticallyByPixels(-deltaPixels.Value);
                        return;
                    }

                    var scrollToLine = scrollTo.Value;

                    if (atTop == true)
                    {
                        using (metrics.Measure("ScrollViewportVerticallyByPixels")) {
                            ScrollViewportVerticallyByPixels(wpfTextView, scrollToLine);
                        }
                    }
                    else
                    {
                        EnsureTargetSpanVisible(wpfTextView, scrollToLine);
                    }
                }
                catch (Exception ex) {
                    Log.Error(ex, $"{nameof(ScrollEditor)} failed for {fileUri}");
                }
            }
        }