Skip to content

Commit

Permalink
Revert "Revert "Deprecate usage of Workspace in TextLoader (breaking …
Browse files Browse the repository at this point in the history
…change) (dotnet#63616)" (dotnet#64228)"

This reverts commit 9d2a58e.
  • Loading branch information
tmat committed Sep 26, 2022
1 parent abb9948 commit 0a15f56
Show file tree
Hide file tree
Showing 40 changed files with 208 additions and 161 deletions.
7 changes: 7 additions & 0 deletions docs/Breaking API Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,10 @@ Roslyn does not support implementing completion for arbitrary languages.
### `Microsoft.CodeAnalysis.CodeStyle.NotificationOption` is now immutable

All property setters now throw an exception.

# Version 4.4.0

`Workspace.OnWorkspaceFailed` is no longer called when an error occurs while reading source file content from disk.

The `Workspace` and `DocumentId` parameters of `TextLoader.LoadTextAndVersionAsync(Workspace, DocumentId, CancellationToken)` are deprecated.
The method now receives an instance of an immutable empty `Workspace` with default workspace services, and a fake `DocumentId`.
3 changes: 2 additions & 1 deletion eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAna
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.SyntaxAnnotation,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,Microsoft.CodeAnalysis.Text.TextSpan,Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Simplification.Simplifier.ReduceAsync(Microsoft.CodeAnalysis.Document,System.Collections.Generic.IEnumerable{Microsoft.CodeAnalysis.Text.TextSpan},Microsoft.CodeAnalysis.Options.OptionSet,System.Threading.CancellationToken); Use overload that takes SimplifierOptions
M:Microsoft.CodeAnalysis.Editing.SyntaxEditor.#ctor(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Host.HostWorkspaceServices); Use overload that takes HostSolutionServices instead
M:Microsoft.CodeAnalysis.Editing.SyntaxEditor.#ctor(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Host.HostWorkspaceServices); Use overload that takes HostSolutionServices instead
M:Microsoft.CodeAnalysis.FileTextLoader.#ctor(System.String,System.Text.Encoding); use WorkspaceFileTextLoader
2 changes: 1 addition & 1 deletion src/EditorFeatures/Core/Interactive/InteractiveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private void AddSubmissionProjectNoLock(ITextBuffer submissionBuffer, string lan
solution = initProject.Solution.AddDocument(
DocumentId.CreateNewId(initializationScriptProjectId, debugName: initializationScriptPath),
Path.GetFileName(initializationScriptPath),
new FileTextLoader(initializationScriptPath, defaultEncoding: null));
new WorkspaceFileTextLoader(solution.Services, initializationScriptPath, defaultEncoding: null));
}

var newSubmissionProject = CreateSubmissionProjectNoLock(solution, _currentSubmissionProjectId, _lastSuccessfulSubmissionProjectId, languageName, imports, references);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace Microsoft.CodeAnalysis.Editor.Implementation.Workspaces
{
[ExportWorkspaceService(typeof(ITextFactoryService), ServiceLayer.Editor), Shared]
internal class EditorTextFactoryService : ITextFactoryService
internal sealed class EditorTextFactoryService : ITextFactoryService
{
private readonly ITextBufferCloneService _textBufferCloneService;
private readonly ITextBufferFactoryService _textBufferFactory;
Expand All @@ -37,7 +37,7 @@ public EditorTextFactoryService(

private static readonly Encoding s_throwingUtf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);

public SourceText CreateText(Stream stream, Encoding? defaultEncoding, CancellationToken cancellationToken = default)
public SourceText CreateText(Stream stream, Encoding? defaultEncoding, CancellationToken cancellationToken)
{
// this API is for a case where user wants us to figure out encoding from the given stream.
// if defaultEncoding is given, we will use it if we couldn't figure out encoding used in the stream ourselves.
Expand Down Expand Up @@ -70,7 +70,7 @@ public SourceText CreateText(Stream stream, Encoding? defaultEncoding, Cancellat
}
}

public SourceText CreateText(TextReader reader, Encoding? encoding, CancellationToken cancellationToken = default)
public SourceText CreateText(TextReader reader, Encoding? encoding, CancellationToken cancellationToken)
{
// this API is for a case where user just wants to create a source text with explicit encoding.
var buffer = CreateTextBuffer(reader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ private static DocumentInfo CreateDesignTimeOnlyDocument(ProjectId projectId, st

internal sealed class FailingTextLoader : TextLoader
{
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(CancellationToken cancellationToken)
{
Assert.True(false, $"Content of document {documentId} should never be loaded");
Assert.True(false, $"Content of document should never be loaded");
throw ExceptionUtilities.Unreachable;
}
}
Expand Down Expand Up @@ -497,28 +497,28 @@ public async Task StartDebuggingSession_CapturingDocuments(bool captureAllDocume
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, encodingA),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, encodingA),
filePath: sourceFileA.Path));

var documentIdB = DocumentId.CreateNewId(projectP.Id, debugName: "B");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdB,
name: "B",
loader: new FileTextLoader(sourceFileB.Path, encodingB),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileB.Path, encodingB),
filePath: sourceFileB.Path));

var documentIdC = DocumentId.CreateNewId(projectP.Id, debugName: "C");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdC,
name: "C",
loader: new FileTextLoader(sourceFileC.Path, encodingC),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileC.Path, encodingC),
filePath: sourceFileC.Path));

var documentIdE = DocumentId.CreateNewId(projectP.Id, debugName: "E");
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdE,
name: "E",
loader: new FileTextLoader(sourceFileE.Path, encodingE),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileE.Path, encodingE),
filePath: sourceFileE.Path));

// check that are testing documents whose hash algorithm does not match the PDB (but the hash itself does):
Expand Down Expand Up @@ -558,7 +558,7 @@ public async Task StartDebuggingSession_CapturingDocuments(bool captureAllDocume

// change content of B on disk again:
sourceFileB.WriteAllText(sourceB3, encodingB);
solution = solution.WithDocumentTextLoader(documentIdB, new FileTextLoader(sourceFileB.Path, encodingB), PreservationMode.PreserveValue);
solution = solution.WithDocumentTextLoader(documentIdB, new WorkspaceFileTextLoader(solution.Services, sourceFileB.Path, encodingB), PreservationMode.PreserveValue);

EnterBreakState(debuggingSession);

Expand Down Expand Up @@ -4387,7 +4387,7 @@ public async Task MultiSession()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
filePath: sourceFileA.Path));

var tasks = Enumerable.Range(0, 10).Select(async i =>
Expand Down Expand Up @@ -4473,7 +4473,7 @@ public async Task WatchHotReloadServiceTest()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
filePath: sourceFileA.Path));

var hotReload = new WatchHotReloadService(workspace.Services, ImmutableArray.Create("Baseline", "AddDefinitionToExistingType", "NewTypeDefinition"));
Expand Down Expand Up @@ -4540,7 +4540,7 @@ public async Task UnitTestingHotReloadServiceTest()
solution = solution.AddDocument(DocumentInfo.Create(
id: documentIdA,
name: "A",
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
loader: new WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
filePath: sourceFileA.Path));

var hotReload = new UnitTestingHotReloadService(workspace.Services);
Expand Down
3 changes: 2 additions & 1 deletion src/EditorFeatures/Test/Workspaces/TextFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Implementation.Workspaces;
using Microsoft.CodeAnalysis.Host;
Expand Down Expand Up @@ -119,7 +120,7 @@ public async Task TestCreateFromTemporaryStorageWithEncoding()
private static void TestCreateTextInferredEncoding(ITextFactoryService textFactoryService, byte[] bytes, Encoding? defaultEncoding, Encoding expectedEncoding)
{
using var stream = new MemoryStream(bytes);
var text = textFactoryService.CreateText(stream, defaultEncoding);
var text = textFactoryService.CreateText(stream, defaultEncoding, CancellationToken.None);
Assert.Equal(expectedEncoding, text.Encoding);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ internal TestDocumentLoader(TestHostDocument hostDocument, string text)
_text = text;
}

public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(CancellationToken cancellationToken)
=> Task.FromResult(TextAndVersion.Create(SourceText.From(_text), VersionStamp.Create(), _hostDocument.FilePath));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.DecompiledSource;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.PdbSourceDocument;
using Microsoft.CodeAnalysis.Shared.Extensions;
Expand Down Expand Up @@ -278,7 +279,7 @@ private bool RemoveDocumentFromWorkspace(Workspace workspace, MetadataAsSourceGe
var documentId = _openedDocumentIds.GetValueOrDefault(fileInfo);
Contract.ThrowIfNull(documentId);

workspace.OnDocumentClosed(documentId, new FileTextLoader(fileInfo.TemporaryFilePath, MetadataAsSourceGeneratedFileInfo.Encoding));
workspace.OnDocumentClosed(documentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, fileInfo.TemporaryFilePath, MetadataAsSourceGeneratedFileInfo.Encoding));
workspace.OnProjectRemoved(documentId.ProjectId);

_openedDocumentIds = _openedDocumentIds.RemoveKey(fileInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Tuple<ProjectInfo, DocumentId> GetProjectInfoAndDocumentId(Workspace work
generatedDocumentId,
Path.GetFileName(TemporaryFilePath),
filePath: TemporaryFilePath,
loader: loadFileFromDisk ? new FileTextLoader(TemporaryFilePath, Encoding) : null);
loader: loadFileFromDisk ? new WorkspaceFileTextLoader(workspace.Services.SolutionServices, TemporaryFilePath, Encoding) : null);

var projectInfo = ProjectInfo.Create(
projectId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public bool TryRemoveDocumentFromWorkspace(Workspace workspace, string filePath)
{
if (_fileToDocumentInfoMap.TryGetValue(filePath, out var info))
{
workspace.OnDocumentClosed(info.DocumentId, new FileTextLoader(filePath, info.Encoding));
workspace.OnDocumentClosed(info.DocumentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, filePath, info.Encoding));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void TryRemoveMiscellaneousDocument(Uri uri)
}
}

private class SourceTextLoader : TextLoader
private sealed class SourceTextLoader : TextLoader
{
private readonly SourceText _sourceText;
private readonly string _fileUri;
Expand All @@ -104,7 +104,7 @@ public SourceTextLoader(SourceText sourceText, string fileUri)
_fileUri = fileUri;
}

public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(CancellationToken cancellationToken)
=> Task.FromResult(TextAndVersion.Create(_sourceText, VersionStamp.Create(), _fileUri));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Features/Lsif/Generator/CompilerInvocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ DocumentInfo CreateDocumentInfo(string unmappedPath)
DocumentId.CreateNewId(projectId, mappedPath),
name: mappedPath,
filePath: mappedPath,
loader: new FileTextLoader(mappedPath, parsedCommandLine.Encoding));
loader: new WorkspaceFileTextLoader(languageServices.SolutionServices, mappedPath, parsedCommandLine.Encoding));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CodeCleanup;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.Utilities;
Expand Down Expand Up @@ -325,7 +326,8 @@ private async Task FormatDocumentCreatedFromTemplateAsync(IVsHierarchy hierarchy

var documentId = DocumentId.CreateNewId(projectToAddTo.Id);

var forkedSolution = projectToAddTo.Solution.AddDocument(DocumentInfo.Create(documentId, filePath, loader: new FileTextLoader(filePath, defaultEncoding: null), filePath: filePath));
var fileLoader = new WorkspaceFileTextLoader(projectToAddTo.Solution.Services, filePath, defaultEncoding: null);
var forkedSolution = projectToAddTo.Solution.AddDocument(DocumentInfo.Create(documentId, filePath, loader: fileLoader, filePath: filePath));
var addedDocument = forkedSolution.GetRequiredDocument(documentId);

var globalOptions = _componentModel.GetService<IGlobalOptionService>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public void CloseDocument(TextDocument document, SourceText text)
}
}

private class PreviewTextLoader : TextLoader
private sealed class PreviewTextLoader : TextLoader
{
private readonly SourceText _text;

internal PreviewTextLoader(SourceText documentText)
=> _text = documentText;

public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
=> Task.FromResult(LoadTextAndVersionSynchronously(workspace, documentId, cancellationToken));
public override Task<TextAndVersion> LoadTextAndVersionAsync(CancellationToken cancellationToken)
=> Task.FromResult(LoadTextAndVersionSynchronously(cancellationToken));

internal override TextAndVersion LoadTextAndVersionSynchronously(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
internal override TextAndVersion LoadTextAndVersionSynchronously(CancellationToken cancellationToken)
=> TextAndVersion.Create(_text, VersionStamp.Create());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ private ProjectInfo CreateProjectInfoForDocument(string filePath)
var languageInformation = TryGetLanguageInformation(filePath);
Contract.ThrowIfNull(languageInformation);

return MiscellaneousFileUtilities.CreateMiscellaneousProjectInfoForDocument(filePath, new FileTextLoader(filePath, defaultEncoding: null), languageInformation, Services.SolutionServices, _metadataReferences);
var loader = new WorkspaceFileTextLoader(Services.SolutionServices, filePath, defaultEncoding: null);
return MiscellaneousFileUtilities.CreateMiscellaneousProjectInfoForDocument(filePath, loader, languageInformation, Services.SolutionServices, _metadataReferences);
}

private void DetachFromDocument(string moniker)
Expand All @@ -308,7 +309,7 @@ private void DetachFromDocument(string moniker)
var document = this.CurrentSolution.GetProject(projectIdAndContainer.projectId).Documents.Single();

// We must close the document prior to deleting the project
OnDocumentClosed(document.Id, new FileTextLoader(document.FilePath, defaultEncoding: null));
OnDocumentClosed(document.Id, new WorkspaceFileTextLoader(Services.SolutionServices, document.FilePath, defaultEncoding: null));
OnProjectRemoved(document.Project.Id);

_monikersToProjectIdAndContainer.Remove(moniker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public DocumentId AddFile(string fullPath, SourceCodeKind sourceCodeKind, Immuta
}

var documentId = DocumentId.CreateNewId(_project.Id, fullPath);
var textLoader = new FileTextLoader(fullPath, defaultEncoding: null);
var textLoader = new WorkspaceFileTextLoader(_project._workspace.Services.SolutionServices, fullPath, defaultEncoding: null);
var documentInfo = DocumentInfo.Create(
documentId,
FileNameUtilities.GetFileName(fullPath),
Expand Down Expand Up @@ -401,7 +401,7 @@ public async ValueTask ProcessRegularFileChangesAsync(ImmutableSegmentedList<str
// the batch, since those have already been removed out of _documentPathsToDocumentIds.
if (!_documentsAddedInBatch.Any(d => d.Id == documentId))
{
documentsToChange.Add((documentId, new FileTextLoader(filePath, defaultEncoding: null)));
documentsToChange.Add((documentId, new WorkspaceFileTextLoader(_project._workspace.Services.SolutionServices, filePath, defaultEncoding: null)));
}
}
}
Expand Down Expand Up @@ -612,7 +612,7 @@ public SourceTextLoader(SourceTextContainer textContainer, string? filePath)
_filePath = filePath;
}

public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(CancellationToken cancellationToken)
=> Task.FromResult(TextAndVersion.Create(_textContainer.CurrentText, VersionStamp.Create(), _filePath));
}
}
Expand Down
Loading

0 comments on commit 0a15f56

Please sign in to comment.