/
TextChange.cs
110 lines (96 loc) · 3.5 KB
/
TextChange.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Text
{
/// <summary>
/// Describes a single change when a particular span is replaced with a new text.
/// </summary>
[DataContract]
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
public readonly struct TextChange : IEquatable<TextChange>
{
/// <summary>
/// The original span of the changed text.
/// </summary>
[DataMember(Order = 0)]
public TextSpan Span { get; }
/// <summary>
/// The new text.
/// </summary>
[DataMember(Order = 1)]
public string? NewText { get; }
/// <summary>
/// Initializes a new instance of <see cref="TextChange"/>
/// </summary>
/// <param name="span">The original span of the changed text.</param>
/// <param name="newText">The new text.</param>
public TextChange(TextSpan span, string newText)
: this()
{
if (newText == null)
{
throw new ArgumentNullException(nameof(newText));
}
this.Span = span;
this.NewText = newText;
}
/// <summary>
/// Provides a string representation for <see cref="TextChange"/>.
/// </summary>
public override string ToString()
{
return string.Format("{0}: {{ {1}, \"{2}\" }}", this.GetType().Name, Span, NewText);
}
public override bool Equals(object? obj)
{
return obj is TextChange && this.Equals((TextChange)obj);
}
public bool Equals(TextChange other)
{
return
EqualityComparer<TextSpan>.Default.Equals(this.Span, other.Span) &&
EqualityComparer<string>.Default.Equals(this.NewText, other.NewText);
}
public override int GetHashCode()
{
return Hash.Combine(this.Span.GetHashCode(), this.NewText?.GetHashCode() ?? 0);
}
public static bool operator ==(TextChange left, TextChange right)
{
return left.Equals(right);
}
public static bool operator !=(TextChange left, TextChange right)
{
return !(left == right);
}
/// <summary>
/// Converts a <see cref="TextChange"/> to a <see cref="TextChangeRange"/>.
/// </summary>
/// <param name="change"></param>
public static implicit operator TextChangeRange(TextChange change)
{
Debug.Assert(change.NewText is object);
return new TextChangeRange(change.Span, change.NewText.Length);
}
/// <summary>
/// An empty set of changes.
/// </summary>
public static IReadOnlyList<TextChange> NoChanges => SpecializedCollections.EmptyReadOnlyList<TextChange>();
internal string GetDebuggerDisplay()
{
var newTextDisplay = NewText switch
{
null => "null",
{ Length: < 10 } => $"\"{NewText}\"",
{ Length: var length } => $"(NewLength = {length})"
};
return $"new TextChange(new TextSpan({Span.Start}, {Span.Length}), {newTextDisplay})";
}
}
}