AvalonEdit 对于选定的文本添加前缀和后缀

2023-04-23,,

  1:         /// <summary>
  2:         /// 两边追加标志
  3:         /// </summary>
  4:         /// <param name="syntax"></param>
  5:         public void ToggleSymmetricalMarkdownFormatting(string syntax)
  6:         {
  7:             int selectionLength = this.textEditor.SelectionLength;
  8:             int selectionStart = this.textEditor.SelectionStart;
  9:             if (selectionLength == 0 && selectionStart + syntax.Length <= this.textEditor.Text.Length)
 10:             {
 11:                 string text = this.textEditor.Document.GetText(selectionStart, syntax.Length);
 12:                 if (text == syntax)
 13:                 {
 14:                     this.textEditor.SelectionStart += syntax.Length;
 15:                     return;
 16:                 }
 17:             }
 18:             char[] array = syntax.ToCharArray();
 19:             Array.Reverse(array);
 20:             string text2 = new string(array);
 21:             int num = this.textEditor.SelectionLength;
 22:             int num2 = this.textEditor.SelectionStart;
 23:             if (num2 >= syntax.Length)
 24:             {
 25:                 num2 -= syntax.Length;
 26:                 num += syntax.Length;
 27:             }
 28:             DocumentLine lineByOffset = this.textEditor.Document.GetLineByOffset(this.textEditor.CaretOffset);
 29:             if (num2 + num + syntax.Length <= lineByOffset.EndOffset)
 30:             {
 31:                 num += syntax.Length;
 32:             }
 33:             string text3 = "";
 34:             if (num > 0)
 35:             {
 36:                 text3 = this.textEditor.Document.GetText(num2, num);
 37:             }
 38:             Match match = Regex.Match(text3, string.Concat(new string[]
 39:           {
 40:             "^",
 41:             Regex.Escape(syntax),
 42:             "(.*)",
 43:             Regex.Escape(text2),
 44:             "$"
 45:           }), RegexOptions.Singleline);
 46:             bool success = match.Success;
 47:             if (success)
 48:             {
 49:                 text3 = match.Groups[1].Value;
 50:                 this.textEditor.SelectionStart = num2;
 51:                 this.textEditor.SelectionLength = num;
 52:                 this.textEditor.SelectedText = text3;
 53:                 return;
 54:             }
 55:             text3 = syntax + this.textEditor.SelectedText + text2;
 56:             this.textEditor.SelectedText = text3;
 57:             this.textEditor.SelectionLength -= syntax.Length * 2;
 58:             this.textEditor.SelectionStart += syntax.Length;
 59:         }
  1:         /// <summary>
  2:         /// 左边添加前缀
  3:         /// </summary>
  4:         /// <param name="markdownSyntaxToApply"></param>
  5:         public void ToggleAsymmetricMarkdownFormatting(string markdownSyntaxToApply)
  6:         {
  7:             bool flag = this.textEditor.SelectedText == this.textEditor.Document.GetText(this.CurrentLine);
  8:             bool flag2 = this.textEditor.SelectedText.Contains(Environment.NewLine);
  9:             bool flag3 = this.textEditor.CaretOffset == this.CurrentLine.Offset;
 10:             if ((!flag3 || !flag) && !flag2)
 11:             {
 12:                 this.textEditor.SelectedText = Environment.NewLine + this.textEditor.SelectedText;
 13:                 this.textEditor.SelectionLength -= Environment.NewLine.Length;
 14:                 this.textEditor.SelectionStart += Environment.NewLine.Length;
 15:             }
 16:             if (this.textEditor.SelectionLength > 0)
 17:             {
 18:                 string selectedText = this.textEditor.SelectedText;
 19:                 string selectedText2;
 20:                 if (selectedText.Contains(markdownSyntaxToApply))
 21:                 {
 22:                     selectedText2 = selectedText.Replace(markdownSyntaxToApply, "");
 23:                 }
 24:                 else
 25:                 {
 26:                     selectedText2 = markdownSyntaxToApply + selectedText.Replace(Environment.NewLine, Environment.NewLine + markdownSyntaxToApply);
 27:                 }
 28:                 this.textEditor.SelectedText = selectedText2;
 29:                 return;
 30:             }
 31:             DocumentLine lineByOffset = this.textEditor.Document.GetLineByOffset(this.textEditor.CaretOffset);
 32:             string text = string.Empty;
 33:             if (!(lineByOffset.Length == 0))
 34:             {
 35:                 text = Environment.NewLine;
 36:             }
 37:             this.textEditor.SelectedText = text + markdownSyntaxToApply;
 38:             this.textEditor.SelectionLength = 0;
 39:             this.textEditor.SelectionStart += markdownSyntaxToApply.Length + text.Length;
 40:         }
 41:         public DocumentLine CurrentLine
 42:         {
 43:             get
 44:             {
 45:                 return this.textEditor.Document.GetLineByOffset(this.textEditor.CaretOffset);
 46:             }
 47:         }
 48: 

AvalonEdit 对于选定的文本添加前缀和后缀的相关教程结束。

《AvalonEdit 对于选定的文本添加前缀和后缀.doc》

下载本文的Word格式文档,以方便收藏与打印。