/
TextChange.cs
97 lines (83 loc) · 3 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
// 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 enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Text
{
/// <summary>
/// Describes a single change when a particular span is replaced with a new text.
/// </summary>
public readonly struct TextChange : IEquatable<TextChange>
{
/// <summary>
/// The original span of the changed text.
/// </summary>
public TextSpan Span { get; }
/// <summary>
/// The new text.
/// </summary>
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>();
}
}