첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ BraceFoldingStrategy.cs

using System.Collections.Generic;

using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Folding;

namespace TestProject
{
    /// <summary>
    /// 중괄호 폴딩 전략
    /// </summary>
    public class BraceFoldingStrategy
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 개방 중괄호 - OpeningBrace

        /// <summary>
        /// 개방 중괄호
        /// </summary>
        public char OpeningBrace { get; set; }

        #endregion
        #region 폐쇄 중괄호 - ClosingBrace

        /// <summary>
        /// 폐쇄 중괄호
        /// </summary>
        public char ClosingBrace { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - BraceFoldingStrategy()

        /// <summary>
        /// 생성자
        /// </summary>
        public BraceFoldingStrategy()
        {
            OpeningBrace = '{';
            ClosingBrace = '}';
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 새 폴딩 생성하기 - CreateNewFoldings(source)

        /// <summary>
        /// 새 폴딩 생성하기
        /// </summary>
        /// <param name="source">텍스트 소스</param>
        /// <returns>새 폴딩 열거 가능형</returns>
        public IEnumerable<NewFolding> CreateNewFoldings(ITextSource source)
        {
            List<NewFolding> newFoldingList    = new List<NewFolding>();
            Stack<int>       offsetStack       = new Stack<int>();
            int              lastNewLineOffset = 0;
            char             openingBrace      = OpeningBrace;
            char             closingBrace      = ClosingBrace;

            for(int i = 0; i < source.TextLength; i++)
            {
                char character = source.GetCharAt(i);

                if(character == openingBrace)
                {
                    offsetStack.Push(i);
                }
                else if(character == closingBrace && offsetStack.Count > 0)
                {
                    int startOffset = offsetStack.Pop();

                    if(startOffset < lastNewLineOffset)
                    {
                        newFoldingList.Add(new NewFolding(startOffset, i + 1));
                    }
                }
                else if(character == '\n' || character == '\r')
                {
                    lastNewLineOffset = i + 1;
                }
            }

            newFoldingList.Sort((a,b) => a.StartOffset.CompareTo(b.StartOffset));

            return newFoldingList;
        }

        #endregion
        #region 새 폴딩 생성하기 - CreateNewFoldings(document, firstErrorOffset)

        /// <summary>
        /// 새 폴딩 생성하기
        /// </summary>
        /// <param name="document">텍스트 문서</param>
        /// <param name="firstErrorOffset">첫번째 에러 오프셋</param>
        /// <returns>새 폴딩 열거 가능형</returns>
        public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;

            return CreateNewFoldings(document);
        }

        #endregion
        #region 폴딩 업데이트하기 - UpdateFoldings(manager, document)

        /// <summary>
        /// 폴딩 업데이트하기
        /// </summary>
        /// <param name="manager">폴딩 관리자</param>
        /// <param name="document">텍스트 문서</param>
        public void UpdateFoldings(FoldingManager manager, TextDocument document)
        {
            int firstErrorOffset;

            IEnumerable<NewFolding> newFoldingEnumerable = CreateNewFoldings(document, out firstErrorOffset);

            manager.UpdateFoldings(newFoldingEnumerable, firstErrorOffset);
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:avalonEdit="http://icsharpcode.net/sharpdevelop/avalonedit"
    Width="800"
    Height="600"
    Title="코드 편집기(Code Editor) 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <Border
            BorderBrush="Black"
            BorderThickness="1"
            SnapsToDevicePixels="True">
            <avalonEdit:TextEditor Name="textEditor" />
        </Border>
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Windows;

using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.textEditor.IsReadOnly                                      = true;  // 디폴트 : false
            this.textEditor.Options.AllowScrollBelowDocument                = true;  // 디폴트 : false
            this.textEditor.Options.AllowToggleOverstrikeMode               = true;  // 디폴트 : false
            this.textEditor.Options.ColumnRulerPosition                     = 130;   // 디폴트 : 80
            this.textEditor.Options.ConvertTabsToSpaces                     = true;  // 디폴트 : false
            this.textEditor.Options.CutCopyWholeLine                        = true;  // 디폴트 : true
            this.textEditor.Options.EnableEmailHyperlinks                   = true;  // 디폴트 : true
            this.textEditor.Options.EnableHyperlinks                        = true;  // 디폴트 : true
            this.textEditor.Options.EnableImeSupport                        = true;  // 디폴트 : true
            this.textEditor.Options.EnableRectangularSelection              = true;  // 디폴트 : true
            this.textEditor.Options.EnableTextDragDrop                      = true;  // 디폴트 : true
            this.textEditor.Options.EnableVirtualSpace                      = true;  // 디폴트 : false
            this.textEditor.Options.HideCursorWhileTyping                   = true;  // 디폴트 : true
            this.textEditor.Options.HighlightCurrentLine                    = true;  // 디폴트 : false
            this.textEditor.Options.IndentationSize                         = 4;     // 디폴트 : 4
            this.textEditor.Options.InheritWordWrapIndentation              = true;  // 디폴트 : true
            this.textEditor.Options.RequireControlModifierForHyperlinkClick = true;  // 디폴트 : true
            this.textEditor.Options.ShowBoxForControlCharacters             = true;  // 디폴트 : true
            this.textEditor.Options.ShowColumnRuler                         = true;  // 디폴트 : false
            this.textEditor.Options.ShowEndOfLine                           = false; // 디폴트 : false
            this.textEditor.Options.ShowSpaces                              = false; // 디폴트 : false
            this.textEditor.Options.ShowTabs                                = false; // 디폴트 : false
            this.textEditor.Options.WordWrapIndentation                     = 0;     // 디폴트 : 0
            this.textEditor.ShowLineNumbers                                 = true;  // 디폴트 : false
            this.textEditor.WordWrap                                        = false; // 디폴트 : false

            Loaded += Window_Loaded;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)

        /// <summary>
        /// 윈도우 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.textEditor.Load(@"..\..\MainWindow.xaml.cs");

            #region name
            /*
            XmlDoc
            C#
            JavaScript
            HTML
            ASP/XHTML
            Boo
            Coco
            CSS
            C++
            Java
            Patch
            PowerShell
            PHP
            Python
            TeX
            TSQL
            VB
            XML
            MarkDown
            MarkDownWithFontSize
            Json
            */
            #endregion
            IHighlightingDefinition definition = HighlightingManager.Instance.GetDefinition("C#");
            
            this.textEditor.SyntaxHighlighting = definition; // 디폴트 : null

            FoldingManager foldingManager = FoldingManager.Install(this.textEditor.TextArea);

          //XmlFoldingStrategy   foldingStrategy = new XmlFoldingStrategy();
            BraceFoldingStrategy foldingStrategy = new BraceFoldingStrategy();

            foldingStrategy.UpdateFoldings(foldingManager, this.textEditor.Document);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요