/src/EditorFeatures/TestUtilities/BraceHighlighting/MultiCharacterBraceHighlightingTests.cs

https://github.com/dotnet/roslyn · C# · 256 lines · 217 code · 32 blank · 7 comment · 15 complexity · 48a2d3ea5f6f8ece59af62e35ea7e143 MD5 · raw file

  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
  8. using Microsoft.CodeAnalysis.Test.Utilities;
  9. using Microsoft.CodeAnalysis.Text;
  10. using Microsoft.CodeAnalysis.UnitTests;
  11. using Roslyn.Test.Utilities;
  12. using Xunit;
  13. namespace Microsoft.CodeAnalysis.Editor.UnitTests.BraceHighlighting
  14. {
  15. public class MultiCharacterBraceHighlightingTests : AbstractBraceHighlightingTests
  16. {
  17. protected override TestWorkspace CreateWorkspace(string markup, ParseOptions options)
  18. => TestWorkspace.Create(
  19. NoCompilationConstants.LanguageName, compilationOptions: null, parseOptions: options, content: markup);
  20. internal override IBraceMatchingService GetBraceMatchingService(TestWorkspace workspace)
  21. => new TestBraceMatchingService();
  22. private class TestBraceMatchingService : IBraceMatchingService
  23. {
  24. public async Task<BraceMatchingResult?> GetMatchingBracesAsync(
  25. Document document, int position, CancellationToken cancellationToken = default)
  26. {
  27. var text = (await document.GetTextAsync(cancellationToken)).ToString();
  28. var braces = GetMatchingBraces(text, position);
  29. if (braces.HasValue)
  30. {
  31. Debug.Assert(text.Substring(braces.Value.LeftSpan.Start, braces.Value.LeftSpan.Length) == "<@");
  32. Debug.Assert(text.Substring(braces.Value.RightSpan.Start, braces.Value.RightSpan.Length) == "@>");
  33. }
  34. return braces;
  35. }
  36. public static BraceMatchingResult? GetMatchingBraces(
  37. string text, int position)
  38. {
  39. if (position < text.Length)
  40. {
  41. var ch = text[position];
  42. // Look for <@ @> depending on where the caret is.
  43. // ^<@ @>
  44. if (ch == '<')
  45. {
  46. Debug.Assert(text[position + 1] == '@');
  47. var secondAt = text.IndexOf('@', position + 2);
  48. return new BraceMatchingResult(new TextSpan(position, 2), new TextSpan(secondAt, 2));
  49. }
  50. // <^@ @> or <@ ^@>
  51. if (ch == '@')
  52. {
  53. if (text[position - 1] == '<')
  54. {
  55. var secondAt = text.IndexOf('@', position + 1);
  56. return new BraceMatchingResult(new TextSpan(position - 1, 2), new TextSpan(secondAt, 2));
  57. }
  58. else
  59. {
  60. Debug.Assert(text[position + 1] == '>');
  61. var lessThan = text.LastIndexOf('<', position);
  62. return new BraceMatchingResult(new TextSpan(lessThan, 2), new TextSpan(position, 2));
  63. }
  64. }
  65. // <@ @^>
  66. if (ch == '>')
  67. {
  68. Debug.Assert(text[position - 1] == '@');
  69. var lessThan = text.LastIndexOf('<', position);
  70. return new BraceMatchingResult(new TextSpan(lessThan, 2), new TextSpan(position - 1, 2));
  71. }
  72. }
  73. return null;
  74. }
  75. }
  76. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  77. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  78. public async Task TestNotOnBrace()
  79. {
  80. await TestBraceHighlightingAsync(
  81. "$$ <@ @>");
  82. }
  83. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  84. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  85. public async Task TestOnLeftOfStartBrace()
  86. {
  87. await TestBraceHighlightingAsync(
  88. "$$[|<@|] [|@>|]");
  89. }
  90. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  91. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  92. public async Task TestInsideStartBrace()
  93. {
  94. await TestBraceHighlightingAsync(
  95. "[|<$$@|] [|@>|]");
  96. }
  97. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  98. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  99. public async Task TestNotOnRightOfStartBrace()
  100. {
  101. await TestBraceHighlightingAsync(
  102. "<@$$ @>");
  103. }
  104. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  105. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  106. public async Task TestNotOnLeftOfCloseBrace()
  107. {
  108. await TestBraceHighlightingAsync(
  109. "<@ $$@>");
  110. }
  111. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  112. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  113. public async Task TestInsideCloseBrace()
  114. {
  115. await TestBraceHighlightingAsync(
  116. "[|<@|] [|@$$>|]");
  117. }
  118. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  119. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  120. public async Task TestOnRightOfCloseBrace()
  121. {
  122. await TestBraceHighlightingAsync(
  123. "[|<@|] [|@>$$|]");
  124. }
  125. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  126. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  127. public async Task TestNotAfterBrace()
  128. {
  129. await TestBraceHighlightingAsync(
  130. "<@ @> $$");
  131. }
  132. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  133. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  134. public async Task TestNotOnBrace2()
  135. {
  136. await TestBraceHighlightingAsync(
  137. "$$ <@ @><@ @>");
  138. }
  139. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  140. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  141. public async Task TestOnLeftOfStartBrace2()
  142. {
  143. await TestBraceHighlightingAsync(
  144. "$$[|<@|] [|@>|]<@ @>");
  145. }
  146. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  147. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  148. public async Task TestInsideStartBrace2()
  149. {
  150. await TestBraceHighlightingAsync(
  151. "[|<$$@|] [|@>|]<@ @>");
  152. }
  153. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  154. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  155. public async Task TestNotOnRightOfStartBrace2()
  156. {
  157. await TestBraceHighlightingAsync(
  158. "<@$$ @><@ @>");
  159. }
  160. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  161. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  162. public async Task TestNotOnLeftOfCloseBrace2()
  163. {
  164. await TestBraceHighlightingAsync(
  165. "<@ $$@><@ @>");
  166. }
  167. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  168. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  169. public async Task TestInsideCloseBrace3()
  170. {
  171. await TestBraceHighlightingAsync(
  172. "[|<@|] [|@$$>|]<@ @>");
  173. }
  174. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  175. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  176. public async Task TestOnRightOfCloseBrace2()
  177. {
  178. await TestBraceHighlightingAsync(
  179. "[|<@|] [|@>|]$$[|<@|] [|@>|]");
  180. }
  181. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  182. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  183. public async Task TestInSecondBracePair()
  184. {
  185. await TestBraceHighlightingAsync(
  186. "<@ @>[|<$$@|] [|@>|]");
  187. }
  188. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  189. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  190. public async Task TestNotAfterSecondBracePairStart()
  191. {
  192. await TestBraceHighlightingAsync(
  193. "<@ @><@$$ @>");
  194. }
  195. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  196. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  197. public async Task TestNotBeforeSecondBracePairEnd()
  198. {
  199. await TestBraceHighlightingAsync(
  200. "<@ @><@ $$@>");
  201. }
  202. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  203. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  204. public async Task TestInSecondBracePairEnd()
  205. {
  206. await TestBraceHighlightingAsync(
  207. "<@ @>[|<@|] [|@$$>|]");
  208. }
  209. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  210. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  211. public async Task TestAtSecondBracePairEnd()
  212. {
  213. await TestBraceHighlightingAsync(
  214. "<@ @>[|<@|] [|@>|]$$");
  215. }
  216. [WorkItem(18050, "https://github.com/dotnet/roslyn/issues/18050")]
  217. [WpfFact, Trait(Traits.Feature, Traits.Features.BraceHighlighting)]
  218. public async Task TestNotAfterSecondBracePairEnd()
  219. {
  220. await TestBraceHighlightingAsync(
  221. "<@ @><@ @> $$");
  222. }
  223. }
  224. }