/
DocumentId.cs
111 lines (88 loc) · 3.58 KB
/
DocumentId.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
111
// 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 Microsoft.CodeAnalysis.Shared.Extensions;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// An identifier that can be used to retrieve the same <see cref="Document"/> across versions of the
/// workspace.
/// </summary>
[DebuggerDisplay("{GetDebuggerDisplay(),nq}")]
public sealed class DocumentId : IEquatable<DocumentId>, IObjectWritable
{
public ProjectId ProjectId { get; }
public Guid Id { get; }
private readonly string? _debugName;
private DocumentId(ProjectId projectId, Guid guid, string? debugName)
{
this.ProjectId = projectId;
this.Id = guid;
_debugName = debugName;
}
/// <summary>
/// Creates a new <see cref="DocumentId"/> instance.
/// </summary>
/// <param name="projectId">The project id this document id is relative to.</param>
/// <param name="debugName">An optional name to make this id easier to recognize while debugging.</param>
public static DocumentId CreateNewId(ProjectId projectId, string? debugName = null)
{
if (projectId == null)
{
throw new ArgumentNullException(nameof(projectId));
}
return new DocumentId(projectId, Guid.NewGuid(), debugName);
}
public static DocumentId CreateFromSerialized(ProjectId projectId, Guid id, string? debugName = null)
{
if (projectId == null)
{
throw new ArgumentNullException(nameof(projectId));
}
if (id == Guid.Empty)
{
throw new ArgumentException(nameof(id));
}
return new DocumentId(projectId, id, debugName);
}
internal string? DebugName => _debugName;
internal string GetDebuggerDisplay()
=> string.Format("({0}, #{1} - {2})", this.GetType().Name, this.Id, _debugName);
public override string ToString()
=> GetDebuggerDisplay();
public override bool Equals(object? obj)
=> this.Equals(obj as DocumentId);
public bool Equals(DocumentId? other)
{
// Technically, we don't need to check project id.
return
other is object &&
this.Id == other.Id &&
this.ProjectId == other.ProjectId;
}
public override int GetHashCode()
=> Hash.Combine(this.ProjectId, this.Id.GetHashCode());
public static bool operator ==(DocumentId? left, DocumentId? right)
=> EqualityComparer<DocumentId?>.Default.Equals(left, right);
public static bool operator !=(DocumentId? left, DocumentId? right)
=> !(left == right);
bool IObjectWritable.ShouldReuseInSerialization => true;
void IObjectWritable.WriteTo(ObjectWriter writer)
{
ProjectId.WriteTo(writer);
writer.WriteGuid(Id);
writer.WriteString(DebugName);
}
internal static DocumentId ReadFrom(ObjectReader reader)
{
var projectId = ProjectId.ReadFrom(reader);
var guid = reader.ReadGuid();
var debugName = reader.ReadString();
return CreateFromSerialized(projectId, guid, debugName);
}
}
}