728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] HwndSource 클래스 : FromHwnd 정적 메소드를 사용해 윈도우 핸들로 윈도우 구하기 (0) | 2021.06.07 |
---|---|
[C#/WPF] 잠금 화면 설정하기 (기능 개선) (0) | 2021.05.31 |
[C#/WPF] 비동기 가상화 컬렉션 사용하기 (0) | 2021.05.21 |
[C#/WPF] PathGeometry 클래스 : GraphicsPath 객체에서 패스 지오메트리 구하기 (0) | 2021.05.13 |
[C#/WPF] 누겟 설치 : AvalonEdit (0) | 2021.05.10 |
[C#/WPF] Dispatcher 클래스 : DisableProcessing 메소드를 사용해 디스패처 비활성하기 (0) | 2021.05.01 |
[C#/WPF] Dispatcher 클래스 : PushFrame 메소드를 사용해 UI 업데이트 하기 (0) | 2021.04.30 |
[C#/WPF] Dispatcher 클래스 : BeginInvoke 메소드 사용하기 (0) | 2021.04.30 |
[C#/WPF] ScrollViewer 클래스 : 애니메이션 스크롤 뷰어 사용하기 (0) | 2021.04.28 |
[C#/WPF] UWP API 사용하기 (0) | 2021.04.26 |
댓글을 달아 주세요