国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

來源:懂視網 責編:小采 時間:2020-11-27 22:35:58
文檔

.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol
推薦度:
導讀.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol

前言

ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊!

先來看一下運行效果:

一、項目結構

這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll.

二、代碼折疊

需要實現IFoldingStrategy中的 GenerateFoldMarkers 方法,代碼如下:

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("http:/// <summary>")) // Look for method starts
 {
 startLines.Push(i);
 }
 if (text.Trim().StartsWith("http:/// <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('/');
 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的配置文件,否則高亮將失效。

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#");
 

 }

為了保持代碼適時進行折疊,這里監聽文本變化,如下所示:

 private void TextEditor_TextChanged(object sender, EventArgs e)
 {
 //更新,以便進行代碼折疊
 textEditor.Document.FoldingManager.UpdateFoldings(null, null);
 }

最后說明的是,我們可以定義一個格式化代碼的類,來格式化C#代碼:

總結

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol
推薦度:
標簽: net 高亮 net代碼
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美区一区二区三 | 久久久影院亚洲精品 | 劲爆欧美精品13页 | 国产成人久久精品亚洲小说 | 精品欧美视频 | 亚洲一区免费在线观看 | 久久伊人网站 | 九九51精品国产免费看 | www.亚洲一区 | 中文字幕高清 | 欧美精品一区二区三区免费播放 | 亚洲成人精品久久 | 亚洲精品国产成人7777 | 日韩高清一区 | 91精品一区二区三区在线观看 | 全免费a级毛片免费毛视频 情侣国产在线 | 亚洲欧美日韩国产色另类 | 日本五十路视频 | 亚洲视频一区二区三区 | 一区二区三区观看 | 亚洲综合视频一区 | 日韩午夜免费视频 | 性xxxx欧美| 亚洲国产成人久久99精品 | 免费观看国产精品 | 伊人久久国产 | 最新国产精品视频 | 国产成人精品亚洲一区 | 欧美变态人zozo禽交 | 一区二区影视 | 久久久久亚洲精品美女 | 欧美性野久久久久久久久 | 亚洲精品第1页 | 国产在线高清不卡免费播放 | 国产精品高清一区二区三区 | 精品国产欧美 | 一区二区三区在线播放 | 欧美日韩小视频 | 日韩视频一区二区在线观看 | 殴美激情 | 欧美成人性色生活18黑人 |