> 백엔드 개발 > C#.Net 튜토리얼 > .NET 코드 편집 컨트롤(ICSharpCode.TextEditor)에 대한 자세한 소개

.NET 코드 편집 컨트롤(ICSharpCode.TextEditor)에 대한 자세한 소개

Y2J
풀어 주다: 2017-05-09 10:58:19
원래의
9637명이 탐색했습니다.

这篇文章主要给大家介绍了.NET中用ICSharpCode.TextEditor自定义代码折叠与高亮的相关资料,文中通过示例代码与图片介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。

前言

ICSharpCode.TextEditor 是一款非常不错的.NET代码编辑控件,内置了多种高亮语言支持,同时完美支持中文,非常赞!

先来看一下运行效果:

一、项目结构

这里需要注意lib文件夹下导入的类库,这个Demo需要这些dll.

二、代码折叠

需要实现IFoldingStrategy中的 GenerateFoldMarkers 方法,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

using ICSharpCode.TextEditor.Document;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace JackWangCUMT.WinForm

{

  

 /// <summary>

 /// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy

 /// </summary>

 public class MingFolding : IFoldingStrategy

 {

  /// <summary>

  /// Generates the foldings for our document.

  /// </summary>

  /// <param name="document">The current document.</param>

  /// <param name="fileName">The filename of the document.</param>

  /// <param name="parseInformation">Extra parse information, not used in this sample.</param>

  /// <returns>A list of FoldMarkers.</returns>

  public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)

  {

   List<FoldMarker> list = new List<FoldMarker>();

   //stack 先进先出

   var startLines = new Stack<int>();

   // Create foldmarkers for the whole document, enumerate through every line.

   for (int i = 0; i < document.TotalNumberOfLines; i++)

   {

    // Get the text of current line.

    string text = document.GetText(document.GetLineSegment(i));

 

    if (text.Trim().StartsWith("#region")) // Look for method starts

    {

     startLines.Push(i);

 

    }

    if (text.Trim().StartsWith("#endregion")) // Look for method endings

    {

     int start = startLines.Pop();

     // Add a new FoldMarker to the list.

     // document = the current document

     // start = the start line for the FoldMarker

     // document.GetLineSegment(start).Length = the ending of the current line = the start column of our foldmarker.

     // i = The current line = end line of the FoldMarker.

     // 7 = The end column

     list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.Region, "..."));

    }

    //支持嵌套 {}

    if (text.Trim().StartsWith("{")) // Look for method starts

    {

     startLines.Push(i);

    }

    if (text.Trim().StartsWith("}")) // Look for method endings

    {

     if (startLines.Count > 0)

     {

      int start = startLines.Pop();

      list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, "...}"));

     }

    }

 

 

    // /// <summary>

    if (text.Trim().StartsWith("/// <summary>")) // Look for method starts

    {

     startLines.Push(i);

    }

    if (text.Trim().StartsWith("/// <returns>")) // Look for method endings

    {

 

     int start = startLines.Pop();

     //获取注释文本(包括空格)

     string display = document.GetText(document.GetLineSegment(start + 1).Offset, document.GetLineSegment(start + 1).Length);

     //remove ///

     display = display.Trim().TrimStart(&#39;/&#39;);

     list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, display));

    }

   }

 

   return list;

  }

 }

}

로그인 후 복사

三、高亮配置

拷贝CSharp-Mode.xshd为 JackCSharp-Mode.xshd ,将其中的名字修改为: SyntaxDefinition name = "JackC#" ,并添加高亮关键字,如下:

这样代码中出现的JackWang就会高亮。下面的代码片段将自定义高亮文件进行加载,并用SetHighlighting进行设置,这里一定注意目录下必须有xshd的配置文件,否则高亮将失效。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

textEditor.Encoding = System.Text.Encoding.UTF8;

 textEditor.Font = new Font("Hack",12);

 textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding();

 textEditor.Text = sampleCode;

 

 //自定义代码高亮

 string path = Application.StartupPath+ "\\HighLighting";

 FileSyntaxModeProvider fsmp;

 if (Directory.Exists(path))

 {

  fsmp = new FileSyntaxModeProvider(path);

  HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);

  textEditor.SetHighlighting("JackC#");

 }

로그인 후 복사

为了保持代码适时进行折叠,这里监听文本变化,如下所示:

1

2

3

4

5

private void TextEditor_TextChanged(object sender, EventArgs e)

{

 //更新,以便进行代码折叠

 textEditor.Document.FoldingManager.UpdateFoldings(null, null);

}

로그인 후 복사

最后说明的是,我们可以定义一个格式化代码的类,来格式化C#代码:

总结

【相关推荐】

1. ASP.NET免费视频教程

2.  ASP.NET教程

3. 极客学院ASP.NET视频教程

위 내용은 .NET 코드 편집 컨트롤(ICSharpCode.TextEditor)에 대한 자세한 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿