Beispiel #1
0
        private void UpdateCaret(VirtualSnapshotPoint caretPosition)
        {
            IEnumerable <LineSpan> analysisLines = Analysis.GetAllLines();
            CaretHandlerBase       caret         = CaretHandlerBase.FromName(Theme.CaretHandler, caretPosition, Analysis.TabSize);

            foreach (LineSpan line in analysisLines)
            {
                int linePos = line.Indent;
                if (!Analysis.Behavior.VisibleUnaligned && linePos % Analysis.IndentSize != 0)
                {
                    continue;
                }

                int formatIndex = line.Indent / Analysis.IndentSize;

                if (line.Indent % Analysis.IndentSize != 0)
                {
                    formatIndex = LineFormat.UnalignedFormatIndex;
                }

                caret.AddLine(line, false);
            }

            foreach (LineSpan line in caret.GetModified())
            {
                Line adornment;
                if (Lines.TryGetValue(line, out adornment))
                {
                    UpdateGuide(line, adornment);
                }
            }
        }
Beispiel #2
0
        void UpdateAdornments_Performance(
            ITextSnapshot snapshot,
            ITextViewModel viewModel,
            ITextViewLine firstVisibleLine,
            IEnumerable <LineSpan> analysisLines
            )
        {
#endif
            double spaceWidth = firstVisibleLine.VirtualSpaceWidth;
            if (spaceWidth <= 0.0)
            {
                return;
            }
            double horizontalOffset = firstVisibleLine.TextLeft;

            HashSet <LineSpan> unusedLines = new HashSet <LineSpan>(Lines.Keys);

            CaretHandlerBase caret = CaretHandlerBase.FromName(Theme.CaretHandler, View.Caret.Position.VirtualBufferPosition,
                                                               Analysis.TabSize);

            object perfCookie = null;
            PerformanceLogger.Start(ref perfCookie);
#if DEBUG
            int initialCount = Lines.Count;
#endif
            foreach (LineSpan line in analysisLines.Concat(GetPageWidthLines()))
            {
                double top    = View.ViewportTop;
                double bottom = View.ViewportBottom;
                double left   = line.Indent * spaceWidth + horizontalOffset;

                Line adornment;
                unusedLines.Remove(line);

                if (line.Type == LineSpanType.PageWidthMarker)
                {
                    line.Highlight = Analysis.LongestLine > line.Indent;
                    if (!Lines.TryGetValue(line, out adornment))
                    {
                        Lines[line] = adornment = CreateGuide(Canvas);
                    }
                    UpdateGuide(line, adornment, left, top, bottom);
                    continue;
                }

                if (Lines.TryGetValue(line, out adornment))
                {
                    adornment.Visibility = Visibility.Hidden;
                }

                caret.AddLine(line, true);

                if (line.FirstLine >= 0 && line.LastLine < int.MaxValue)
                {
                    int firstLineNumber = line.FirstLine;
                    int lastLineNumber = line.LastLine;
                    ITextSnapshotLine firstLine, lastLine;
                    try
                    {
                        firstLine = snapshot.GetLineFromLineNumber(firstLineNumber);
                        lastLine  = snapshot.GetLineFromLineNumber(lastLineNumber);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("In GetLineFromLineNumber:\n{0}", ex);
                        continue;
                    }

                    if (firstLine.Start > View.TextViewLines.LastVisibleLine.Start ||
                        lastLine.Start < View.TextViewLines.FirstVisibleLine.Start)
                    {
                        continue;
                    }

                    while (
                        !viewModel.IsPointInVisualBuffer(firstLine.Start, PositionAffinity.Successor) &&
                        ++firstLineNumber < lastLineNumber
                        )
                    {
                        try
                        {
                            firstLine = snapshot.GetLineFromLineNumber(firstLineNumber);
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("In GetLineFromLineNumber:\n{0}", ex);
                            firstLine = null;
                            break;
                        }
                    }

                    while (
                        !viewModel.IsPointInVisualBuffer(lastLine.Start, PositionAffinity.Predecessor) &&
                        --lastLineNumber > firstLineNumber
                        )
                    {
                        try
                        {
                            lastLine = snapshot.GetLineFromLineNumber(lastLineNumber);
                        }
                        catch (Exception ex)
                        {
                            Trace.TraceError("In GetLineFromLineNumber:\n{0}", ex);
                            lastLine = null;
                            break;
                        }
                    }

                    if (firstLine == null || lastLine == null || firstLineNumber > lastLineNumber)
                    {
                        continue;
                    }


                    IWpfTextViewLine firstView, lastView;
                    try
                    {
                        firstView = View.GetTextViewLineContainingBufferPosition(firstLine.Start);
                        lastView  = View.GetTextViewLineContainingBufferPosition(lastLine.End);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("UpdateAdornments GetTextViewLineContainingBufferPosition failed\n{0}", ex);
                        continue;
                    }

                    string extentText;
                    if (!string.IsNullOrWhiteSpace(extentText = firstView.Extent.GetText()) &&
                        line.Indent > extentText.LeadingWhitespace(Analysis.TabSize)
                        )
                    {
                        continue;
                    }

                    if (firstView.VisibilityState != VisibilityState.Unattached)
                    {
                        top = firstView.Top;
                    }
                    if (lastView.VisibilityState != VisibilityState.Unattached)
                    {
                        bottom = lastView.Bottom;
                    }
                }

                if (!Lines.TryGetValue(line, out adornment))
                {
                    Lines[line] = adornment = CreateGuide(Canvas);
                }
                UpdateGuide(line, adornment, left, top, bottom);
            }

            PerformanceLogger.End(perfCookie);
#if DEBUG
            Debug.WriteLine("Added {0} guides", Lines.Count - initialCount);
            Debug.WriteLine("Removed {0} guides", unusedLines.Count);
            Debug.WriteLine("{0} guides active", Lines.Count - unusedLines.Count);
            Debug.WriteLine("");
#endif

            foreach (LineSpan line in unusedLines)
            {
                Line adornment;
                if (Lines.TryGetValue(line, out adornment))
                {
                    Canvas.Children.Remove(adornment);
                    Lines.Remove(line);
                }
            }

            foreach (LineSpan line in caret.GetModified())
            {
                Line adornment;
                if (Lines.TryGetValue(line, out adornment))
                {
                    UpdateGuide(line, adornment);
                }
            }
        }