-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TextLoader.cs
195 lines (165 loc) · 7.53 KB
/
TextLoader.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// 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.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// A class that represents access to a source text and its version from a storage location.
/// </summary>
public abstract class TextLoader
{
private const double MaxDelaySecs = 1.0;
private const int MaxRetries = 5;
internal static readonly TimeSpan RetryDelay = TimeSpan.FromSeconds(MaxDelaySecs / MaxRetries);
internal virtual string? FilePath => null;
/// <summary>
/// Load a text and a version of the document.
/// </summary>
/// <exception cref="IOException" />
/// <exception cref="InvalidDataException"/>
/// <exception cref="OperationCanceledException"/>
public abstract Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken);
/// <summary>
/// Load a text and a version of the document in the workspace.
/// </summary>
/// <exception cref="IOException" />
/// <exception cref="InvalidDataException"/>
/// <exception cref="OperationCanceledException"/>
internal virtual TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
// this implementation exists in case a custom derived type does not have access to internals
return LoadTextAndVersionAsync(workspace, documentId, cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal async Task<TextAndVersion> LoadTextAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
var retries = 0;
while (true)
{
try
{
return await LoadTextAndVersionAsync(workspace, documentId, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
}
catch (IOException e)
{
if (++retries > MaxRetries)
{
return CreateFailedText(workspace, documentId, e.Message);
}
// fall out to try again
}
catch (InvalidDataException e)
{
return CreateFailedText(workspace, documentId, e.Message);
}
// try again after a delay
await Task.Delay(RetryDelay, cancellationToken).ConfigureAwait(false);
}
}
internal TextAndVersion LoadTextSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
var retries = 0;
while (true)
{
try
{
return LoadTextAndVersionSynchronously(workspace, documentId, cancellationToken);
}
catch (IOException e)
{
if (++retries > MaxRetries)
{
return CreateFailedText(workspace, documentId, e.Message);
}
// fall out to try again
}
catch (InvalidDataException e)
{
return CreateFailedText(workspace, documentId, e.Message);
}
// try again after a delay
Thread.Sleep(RetryDelay);
}
}
private TextAndVersion CreateFailedText(Workspace workspace, DocumentId documentId, string message)
{
// Notify workspace for backwards compatibility.
workspace.OnWorkspaceFailed(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, message, documentId));
Location location;
string display;
var filePath = FilePath;
if (filePath == null)
{
location = Location.None;
display = documentId.ToString();
}
else
{
location = Location.Create(filePath, textSpan: default, lineSpan: default);
display = filePath;
}
return TextAndVersion.Create(
SourceText.From(string.Empty, Encoding.UTF8),
VersionStamp.Default,
string.Empty,
Diagnostic.Create(WorkspaceDiagnosticDescriptors.ErrorReadingFileContent, location, new[] { display, message }));
}
/// <summary>
/// Creates a new <see cref="TextLoader"/> from an already existing source text and version.
/// </summary>
public static TextLoader From(TextAndVersion textAndVersion)
{
if (textAndVersion == null)
{
throw new ArgumentNullException(nameof(textAndVersion));
}
return new TextDocumentLoader(textAndVersion);
}
/// <summary>
/// Creates a <see cref="TextLoader"/> from a <see cref="SourceTextContainer"/> and version.
///
/// The text obtained from the loader will be the current text of the container at the time
/// the loader is accessed.
/// </summary>
public static TextLoader From(SourceTextContainer container, VersionStamp version, string? filePath = null)
{
if (container == null)
{
throw new ArgumentNullException(nameof(container));
}
return new TextContainerLoader(container, version, filePath);
}
private sealed class TextDocumentLoader : TextLoader
{
private readonly TextAndVersion _textAndVersion;
internal TextDocumentLoader(TextAndVersion textAndVersion)
=> _textAndVersion = textAndVersion;
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
=> Task.FromResult(_textAndVersion);
internal override TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
=> _textAndVersion;
}
private sealed class TextContainerLoader : TextLoader
{
private readonly SourceTextContainer _container;
private readonly VersionStamp _version;
private readonly string? _filePath;
internal TextContainerLoader(SourceTextContainer container, VersionStamp version, string? filePath)
{
_container = container;
_version = version;
_filePath = filePath;
}
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
=> Task.FromResult(LoadTextAndVersionSynchronously(workspace, documentId, cancellationToken));
internal override TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
=> TextAndVersion.Create(_container.CurrentText, _version, _filePath);
}
}
}