C# 编辑器项目 括号折叠策略
2022/7/13 1:23:49
本文主要是介绍C# 编辑器项目 括号折叠策略,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 using ICSharpCode.AvalonEdit.Document; 2 using ICSharpCode.AvalonEdit.Folding; 3 using ICSharpCode.NRefactory.Editor; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 10 namespace CMS.Developer.AvalonEditor 11 { 12 /// <summary> 13 /// 大括号折叠策略 14 /// </summary> 15 public class BraceFoldingStrategy 16 { 17 public char OpeningBrace { get; set; } 18 19 public char ClosingBrace { get; set; } 20 21 public BraceFoldingStrategy() 22 { 23 this.OpeningBrace = '{'; 24 this.ClosingBrace = '}'; 25 } 26 27 public void UpdateFoldings(FoldingManager manager, TextDocument document) 28 { 29 int firstErrorOffset; 30 IEnumerable<NewFolding> newFoldings = CreateNewFoldings(document, out firstErrorOffset); 31 manager.UpdateFoldings(newFoldings, firstErrorOffset); 32 } 33 34 public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset) 35 { 36 firstErrorOffset = -1; 37 return CreateNewFoldings(document); 38 } 39 40 public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document) 41 { 42 List<NewFolding> newFoldings = new List<NewFolding>(); 43 44 Stack<int> startOffsets = new Stack<int>(); 45 int lastNewLineOffset = 0; 46 char openingBrace = this.OpeningBrace; 47 char closingBrace = this.ClosingBrace; 48 for (int i = 0; i < document.TextLength; i++) 49 { 50 char c = document.GetCharAt(i); 51 if (c == openingBrace) 52 { 53 startOffsets.Push(i); 54 } 55 else if (c == closingBrace && startOffsets.Count > 0) 56 { 57 int startOffset = startOffsets.Pop(); 58 if (startOffset < lastNewLineOffset) 59 { 60 newFoldings.Add(new NewFolding(startOffset, i + 1)); 61 } 62 } 63 else if (c == '\n' || c == '\r') 64 { 65 lastNewLineOffset = i + 1; 66 } 67 } 68 newFoldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset)); 69 return newFoldings; 70 } 71 72 } 73 }
这篇关于C# 编辑器项目 括号折叠策略的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#