-
Notifications
You must be signed in to change notification settings - Fork 4k
/
TextLoader.cs
103 lines (88 loc) · 4.07 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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
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
{
/// <summary>
/// Load a text and a version of the document in the workspace.
/// </summary>
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>
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);
}
/// <summary>
/// Creates a new 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 TextLoader from a 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 class TextDocumentLoader : TextLoader
{
private readonly TextAndVersion _textAndVersion;
internal TextDocumentLoader(TextAndVersion textAndVersion)
{
_textAndVersion = textAndVersion;
}
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
return Task.FromResult(_textAndVersion);
}
internal override TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
return _textAndVersion;
}
}
private 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)
{
return Task.FromResult(LoadTextAndVersionSynchronously(workspace, documentId, cancellationToken));
}
internal override TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
return TextAndVersion.Create(_container.CurrentText, _version, _filePath);
}
}
}
}