Esempio n. 1
0
        void UpdateAdornmentsWorker()
        {
            //var analysisLines = Analysis.Lines;
            //if (Analysis.Snapshot != View.TextSnapshot) {
            //    var task = Analysis.Update();
            //    if (task != null) {
            //        UpdateAdornments(task);
            //    }
            //    return;
            //} else if (analysisLines == null) {
            //    UpdateAdornments(Analysis.Reset());
            //    return;
            //}

            if (!GlobalVisible)
            {
                Canvas.Visibility = Visibility.Collapsed;
                return;
            }
            Canvas.Visibility = Visibility.Visible;

            var snapshot  = View.TextSnapshot;
            var viewModel = View.TextViewModel;

            if (snapshot == null || viewModel == null)
            {
                return;
            }

            var firstVisibleLine = View.TextViewLines.FirstOrDefault(line => line.IsFirstTextViewLineForSnapshotLine);

            if (firstVisibleLine == null)
            {
                return;
            }
            var lastVisibleLine = View.TextViewLines.LastOrDefault(line => line.IsLastTextViewLineForSnapshotLine);

            if (lastVisibleLine == null)
            {
                return;
            }

            var analysisLines = Analysis.GetLines(
                firstVisibleLine.Start.GetContainingLine().LineNumber,
                lastVisibleLine.Start.GetContainingLine().LineNumber
                );

            if (!analysisLines.Any())
            {
                return;
            }


#if PERFORMANCE
            object cookie = null;
            try {
                PerformanceLogger.Start(ref cookie);
                UpdateAdornments_Performance(
                    snapshot,
                    viewModel,
                    firstVisibleLine,
                    analysisLines
                    );
            } catch (OperationCanceledException) {
                PerformanceLogger.Mark("Cancel");
                throw;
            } finally {
                PerformanceLogger.End(cookie);
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.Clear(!Checked ? BackColor : HighlightBackColor);

            Color foreColor = !Checked ? ForeColor : HighlightForeColor;

            double spaceLeft, spaceWidth;

            using (var sf = new StringFormat()) {
                RectangleF rect = ClientRectangle;
                rect.Width *= 2.0f;

                sf.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, 16) });

                var testString = VisibleWhitespace ? "················{" : "                {";
                var rgns       = e.Graphics.MeasureCharacterRanges(testString, Font, rect, sf);
                var bounds     = rgns[0].GetBounds(e.Graphics);
                spaceLeft  = bounds.Left;
                spaceWidth = bounds.Width / 16;

#if DEBUG
                sf.Alignment = StringAlignment.Far;
                rect.Width  /= 2;
                using (var brush = new SolidBrush(foreColor))
                    e.Graphics.DrawString(string.Format("{0}, {1}", spaceLeft, spaceWidth), Font, brush, rect, sf);
#endif
            }

            try {
                if (Analysis == null || Analysis.Snapshot == null)
                {
                    return;
                }
                var snapshot = Analysis.Snapshot.TextBuffer.CurrentSnapshot;

                using (var brush = new SolidBrush(foreColor)) {
                    foreach (var line in snapshot.Lines)
                    {
                        var text = line.GetText();
                        if (VisibleWhitespace)
                        {
                            text = text.Replace(' ', '·');
                        }
                        e.Graphics.DrawString(text, Font, brush, 0, line.LineNumber * Font.Height);
                    }
                }


                if (Theme == null)
                {
                    return;
                }

                var analysisLines = Analysis.GetLines(0, snapshot.LineCount);

                foreach (var line in analysisLines)
                {
                    float top    = line.FirstLine * Font.Height;
                    float bottom = (line.LastLine + 1) * Font.Height;
                    float left   = (float)Math.Floor(line.Indent * spaceWidth + spaceLeft);

                    bool glow;
                    using (var pen = GetLinePen(line.FormatIndex, out glow)) {
                        if (pen != null)
                        {
                            if (glow)
                            {
                                using (var transparentBrush = new SolidBrush(Color.FromArgb(24, pen.Color))) {
                                    for (int i = 1; i < LineStyle.Thick.GetStrokeThickness(); ++i)
                                    {
                                        e.Graphics.FillRectangle(transparentBrush, left - i, top, i + i + 1, bottom - top);
                                    }
                                }
                            }
                            e.Graphics.DrawLine(pen, left, top, left, bottom);
                        }
                    }
                }
            } catch (Exception ex) {
                Trace.WriteLine("LineTextPreview::OnPaint Error");
                Trace.WriteLine(" - Exception: " + ex.ToString());
                using (var brush = new SolidBrush(foreColor)) {
                    e.Graphics.DrawString(ex.ToString(), Font, brush, 0.0f, 0.0f);
                }
            }
        }