-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TextLoader.cs
241 lines (206 loc) · 9.75 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// 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.Collections.Immutable;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
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 static readonly ConditionalWeakTable<Type, StrongBox<bool>> s_isObsoleteLoadTextAndVersionAsyncOverriden = new();
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>
/// True if <see cref="LoadTextAndVersionAsync(LoadTextOptions, CancellationToken)"/> reloads <see cref="SourceText"/> from its original binary representation (e.g. file on disk).
/// </summary>
internal virtual bool CanReloadText
=> false;
/// <summary>
/// Load a text and a version of the document.
/// </summary>
/// <param name="options">
/// Implementations of this method should use <see cref="LoadTextOptions.ChecksumAlgorithm"/> when creating <see cref="SourceText"/> from an original binary representation and
/// ignore it otherwise.
/// Callers of this method should pass <see cref="LoadTextOptions"/> specifying the desired properties of <see cref="SourceText"/>. The implementation may return a <see cref="SourceText"/>
/// that does not satisfy the given requirements. For example, legacy types that do not override this method would ignore all <paramref name="options"/>.
/// </param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <exception cref="IOException" />
/// <exception cref="InvalidDataException"/>
/// <exception cref="OperationCanceledException"/>
public virtual Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptions options, CancellationToken cancellationToken)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (s_isObsoleteLoadTextAndVersionAsyncOverriden.GetValue(
GetType(),
_ => new StrongBox<bool>(new Func<Workspace, DocumentId, CancellationToken, Task<TextAndVersion>>(LoadTextAndVersionAsync).Method.DeclaringType != typeof(TextLoader))).Value)
{
return LoadTextAndVersionAsync(workspace: null, documentId: null, cancellationToken);
}
#pragma warning restore
throw new NotImplementedException($"{GetType()} must override {nameof(LoadTextAndVersionAsync)}");
}
/// <summary>
/// Load a text and a version of the document.
/// </summary>
/// <param name="workspace">Obsolete. Null.</param>
/// <param name="documentId">Obsolete. Null.</param>
/// <exception cref="IOException" />
/// <exception cref="InvalidDataException"/>
/// <exception cref="OperationCanceledException"/>
[Obsolete("Use/override LoadTextAndVersionAsync(LoadTextOptions, CancellationToken)")]
public virtual Task<TextAndVersion> LoadTextAndVersionAsync(Workspace? workspace, DocumentId? documentId, CancellationToken cancellationToken)
=> LoadTextAndVersionAsync(new LoadTextOptions(SourceHashAlgorithms.Default), 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(LoadTextOptions options, CancellationToken cancellationToken)
{
// this implementation exists in case a custom derived type does not have access to internals
return LoadTextAndVersionAsync(options, cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal async Task<TextAndVersion> LoadTextAsync(LoadTextOptions options, CancellationToken cancellationToken)
{
var retries = 0;
while (true)
{
try
{
return await LoadTextAndVersionAsync(options, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
}
catch (IOException e)
{
if (++retries > MaxRetries)
{
return CreateFailedText(e.Message);
}
// fall out to try again
}
catch (InvalidDataException e)
{
return CreateFailedText(e.Message);
}
// try again after a delay
await Task.Delay(RetryDelay, cancellationToken).ConfigureAwait(false);
}
}
internal TextAndVersion LoadTextSynchronously(LoadTextOptions options, CancellationToken cancellationToken)
{
var retries = 0;
while (true)
{
try
{
return LoadTextAndVersionSynchronously(options, cancellationToken);
}
catch (IOException e)
{
if (++retries > MaxRetries)
{
return CreateFailedText(e.Message);
}
// fall out to try again
}
catch (InvalidDataException e)
{
return CreateFailedText(e.Message);
}
cancellationToken.ThrowIfCancellationRequested();
// try again after a delay
Thread.Sleep(RetryDelay);
}
}
private TextAndVersion CreateFailedText(string message)
{
Location location;
string display;
var filePath = FilePath;
if (filePath == null)
{
location = Location.None;
display = "<no path>";
}
else
{
location = Location.Create(filePath, textSpan: default, lineSpan: default);
display = filePath;
}
return TextAndVersion.Create(
SourceText.From(string.Empty, Encoding.UTF8),
VersionStamp.Default,
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(LoadTextOptions options, CancellationToken cancellationToken)
=> Task.FromResult(_textAndVersion);
internal override TextAndVersion LoadTextAndVersionSynchronously(LoadTextOptions options, 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;
}
internal override string? FilePath
=> _filePath;
public override Task<TextAndVersion> LoadTextAndVersionAsync(LoadTextOptions options, CancellationToken cancellationToken)
=> Task.FromResult(LoadTextAndVersionSynchronously(options, cancellationToken));
internal override TextAndVersion LoadTextAndVersionSynchronously(LoadTextOptions options, CancellationToken cancellationToken)
=> TextAndVersion.Create(_container.CurrentText, _version, _filePath);
}
}
}