/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Compilers/CSharp/Test/Syntax/TextExtensions.cs
74 строки
3 KB
Jared Parsons
generated move
28 сен 2020, 19:36
28 сен 2020, 19:36
1cca63b
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #nullable disable using System; using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using Xunit; namespace Microsoft.CodeAnalysis.CSharp.UnitTests { public static class TextExtensions { public static void ShouldBe<T>(this T actual, T expected) { Assert.Equal(expected, actual); } public static SourceText WithReplace(this SourceText text, int offset, int length, string newText) { var oldFullText = text.ToString(); var span = new TextSpan(offset, length); var newFullText = oldFullText.Substring(0, offset) + newText + oldFullText.Substring(span.End); return SourceText.From(newFullText); } public static SourceText WithReplaceFirst(this SourceText text, string oldText, string newText) { var oldFullText = text.ToString(); int offset = oldFullText.IndexOf(oldText, StringComparison.Ordinal); int length = oldText.Length; var span = new TextSpan(offset, length); var newFullText = oldFullText.Substring(0, offset) + newText + oldFullText.Substring(span.End); return SourceText.From(newFullText); } public static SourceText WithReplace(this SourceText text, int startIndex, string oldText, string newText) { var oldFullText = text.ToString(); int offset = oldFullText.IndexOf(oldText, startIndex, StringComparison.Ordinal); // Use an offset to find the first element to replace at int length = oldText.Length; var span = new TextSpan(offset, length); var newFullText = oldFullText.Substring(0, offset) + newText + oldFullText.Substring(span.End); return SourceText.From(newFullText); } public static SourceText WithInsertAt(this SourceText text, int offset, string newText) { return WithReplace(text, offset, 0, newText); } public static SourceText WithInsertBefore(this SourceText text, string existingText, string newText) { var oldFullText = text.ToString(); int offset = oldFullText.IndexOf(existingText, StringComparison.Ordinal); var span = new TextSpan(offset, 0); var newFullText = oldFullText.Substring(0, offset) + newText + oldFullText.Substring(offset); return SourceText.From(newFullText); } public static SourceText WithRemoveAt(this SourceText text, int offset, int length) { return WithReplace(text, offset, length, string.Empty); } public static SourceText WithRemoveFirst(this SourceText text, string oldText) { return WithReplaceFirst(text, oldText, string.Empty); } } }