BraceFoldingStrategy.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using ICSharpCode.AvalonEdit.Document;
  2. using ICSharpCode.AvalonEdit.Folding;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace TeamAAS_VP.Core
  9. {
  10. /// <summary>
  11. /// Allows producing foldings from a document based on braces.
  12. /// </summary>
  13. public class BraceFoldingStrategy
  14. {
  15. /// <summary>
  16. /// Gets/Sets the opening brace. The default value is '{'.
  17. /// </summary>
  18. public char OpeningBrace { get; set; }
  19. /// <summary>
  20. /// Gets/Sets the closing brace. The default value is '}'.
  21. /// </summary>
  22. public char ClosingBrace { get; set; }
  23. /// <summary>
  24. /// Creates a new BraceFoldingStrategy.
  25. /// </summary>
  26. public BraceFoldingStrategy()
  27. {
  28. this.OpeningBrace = '{';
  29. this.ClosingBrace = '}';
  30. }
  31. public void UpdateFoldings(FoldingManager manager, TextDocument document)
  32. {
  33. int firstErrorOffset;
  34. IEnumerable<NewFolding> newFoldings = CreateNewFoldings(document, out firstErrorOffset);
  35. manager.UpdateFoldings(newFoldings, firstErrorOffset);
  36. }
  37. /// <summary>
  38. /// Create <see cref="NewFolding"/>s for the specified document.
  39. /// </summary>
  40. public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
  41. {
  42. firstErrorOffset = -1;
  43. return CreateNewFoldings(document);
  44. }
  45. /// <summary>
  46. /// Create <see cref="NewFolding"/>s for the specified document.
  47. /// </summary>
  48. public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
  49. {
  50. List<NewFolding> newFoldings = new List<NewFolding>();
  51. Stack<int> startOffsets = new Stack<int>();
  52. int lastNewLineOffset = 0;
  53. char openingBrace = this.OpeningBrace;
  54. char closingBrace = this.ClosingBrace;
  55. for (int i = 0; i < document.TextLength; i++)
  56. {
  57. char c = document.GetCharAt(i);
  58. if (c == openingBrace)
  59. {
  60. startOffsets.Push(i);
  61. }
  62. else if (c == closingBrace && startOffsets.Count > 0)
  63. {
  64. int startOffset = startOffsets.Pop();
  65. // don't fold if opening and closing brace are on the same line
  66. if (startOffset < lastNewLineOffset)
  67. {
  68. newFoldings.Add(new NewFolding(startOffset, i + 1));
  69. }
  70. }
  71. else if (c=='#' && i+6< document.TextLength && document.GetCharAt(i+1)== 'r' && document.GetCharAt(i + 2) == 'e' && document.GetCharAt(i + 3) == 'g' && document.GetCharAt(i + 4) == 'i' && document.GetCharAt(i + 5) == 'o' && document.GetCharAt(i + 6) == 'n')
  72. {
  73. startOffsets.Push(i);
  74. }
  75. else if (c == '#' && i + 6 < document.TextLength && document.GetCharAt(i + 1) == 'e' && document.GetCharAt(i + 2) == 'n' && document.GetCharAt(i + 3) == 'd' && document.GetCharAt(i + 1) == 'r' && document.GetCharAt(i + 2) == 'e' && document.GetCharAt(i + 3) == 'g' && document.GetCharAt(i + 4) == 'i' && document.GetCharAt(i + 5) == 'o' && document.GetCharAt(i + 6) == 'n' && startOffsets.Count > 0)
  76. {
  77. int startOffset = startOffsets.Pop();
  78. // don't fold if opening and closing brace are on the same line
  79. if (startOffset < lastNewLineOffset)
  80. {
  81. newFoldings.Add(new NewFolding(startOffset, i + 1));
  82. }
  83. }
  84. else if (c == '\n' || c == '\r')
  85. {
  86. lastNewLineOffset = i + 1;
  87. }
  88. }
  89. newFoldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
  90. return newFoldings;
  91. }
  92. }
  93. }