Exemplo n.º 1
0
        /// <inheritdoc/>
        public override VisualLineElement ConstructElement(int offset)
        {
            if (foldingManager == null)
            {
                return(null);
            }
            int            foldedUntil    = -1;
            FoldingSection foldingSection = null;

            foreach (FoldingSection fs in foldingManager.GetFoldingsContaining(offset))
            {
                if (fs.IsFolded)
                {
                    if (fs.EndOffset > foldedUntil)
                    {
                        foldedUntil    = fs.EndOffset;
                        foldingSection = fs;
                    }
                }
            }
            if (foldedUntil > offset && foldingSection != null)
            {
                // Handle overlapping foldings: if there's another folded folding
                // (starting within the foldingSection) that continues after the end of the folded section,
                // then we'll extend our fold element to cover that overlapping folding.
                bool foundOverlappingFolding;
                do
                {
                    foundOverlappingFolding = false;
                    foreach (FoldingSection fs in FoldingManager.GetFoldingsContaining(foldedUntil))
                    {
                        if (fs.IsFolded && fs.EndOffset > foldedUntil)
                        {
                            foldedUntil             = fs.EndOffset;
                            foundOverlappingFolding = true;
                        }
                    }
                } while (foundOverlappingFolding);

                string title = foldingSection.Title;
                if (string.IsNullOrEmpty(title))
                {
                    title = "...";
                }
                var p = new VisualLineElementTextRunProperties(CurrentContext.GlobalTextRunProperties);
                p.SetForegroundBrush(textBrush);
                var textFormatter = TextFormatterFactory.Create(CurrentContext.TextView);
                var text          = FormattedTextElement.PrepareText(textFormatter, title, p);
                return(new FoldingLineElement(foldingSection, text, foldedUntil - offset)
                {
                    textBrush = textBrush
                });
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private void RefreshNonPrintableCharacterTexts()
        {
            // NOTE: 改行マークの変更
            // 既定の改行マークは VisualLineTextSource.CreateTextRunForNewLine() でハードコーディングされている。
            // private 関数のため変更できず、VisualLineTextSource は sealed クラスであり継承して誤魔化すこともできない。
            // AvalonEdit では、画面描画にあたりこの改行マークを TextLine に変換しており、
            // TextViewCachedElements.nonPrintableCharacterTexts の Dictionary を参照して変換先を生成する。
            // ここを突き、nonPrintableCharacterTexts を書き換えることで、生成される TextLine を制御する。

            var globalProterties = (TextRunProperties)typeof(ICSharpCode.AvalonEdit.Rendering.TextView)
                                   .GetMethod("CreateGlobalTextRunProperties", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod)
                                   .Invoke(this, null);
            var formatter = (TextFormatter)typeof(ICSharpCode.AvalonEdit.Rendering.TextView).Assembly
                            .GetType("ICSharpCode.AvalonEdit.Utils.TextFormatterFactory")
                            .GetMethod("Create", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod)
                            .Invoke(null, new[] { this });
            var cachedElements = typeof(ICSharpCode.AvalonEdit.Rendering.TextView)
                                 .GetField("cachedElements", BindingFlags.Instance | BindingFlags.NonPublic)
                                 .GetValue(this);
            var nonPrintableCharacterTexts = (Dictionary <string, TextLine>)cachedElements.GetType()
                                             .GetField("nonPrintableCharacterTexts", BindingFlags.Instance | BindingFlags.NonPublic)
                                             .GetValue(cachedElements);

            var elementProperties = new VisualLineElementTextRunProperties(globalProterties);

            elementProperties.SetForegroundBrush(this.NonPrintableCharacterBrush);
            var cr   = FormattedTextElement.PrepareText(formatter, this.VisualCharacterCR, elementProperties);
            var lf   = FormattedTextElement.PrepareText(formatter, this.VisualCharacterLF, elementProperties);
            var crlf = FormattedTextElement.PrepareText(formatter, this.VisualCharacterCRLF, elementProperties);

            nonPrintableCharacterTexts ??= new();
            nonPrintableCharacterTexts["\\r"] = cr;
            nonPrintableCharacterTexts["\\n"] = lf;
            nonPrintableCharacterTexts["¶"]   = crlf;

            cachedElements.GetType()
            .GetField("nonPrintableCharacterTexts", BindingFlags.Instance | BindingFlags.NonPublic)
            .SetValue(cachedElements, nonPrintableCharacterTexts);
        }