Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Deprecate usage of Workspace in TextLoader (breaking change)" #64228

Merged
merged 1 commit into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/Breaking API Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,3 @@ 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: 1 addition & 2 deletions eng/config/BannedSymbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,4 @@ 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.FileTextLoader.#ctor(System.String,System.Text.Encoding); use WorkspaceFileTextLoader
M:Microsoft.CodeAnalysis.Editing.SyntaxEditor.#ctor(Microsoft.CodeAnalysis.SyntaxNode,Microsoft.CodeAnalysis.Host.HostWorkspaceServices); Use overload that takes HostSolutionServices instead
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 WorkspaceFileTextLoader(solution.Services, initializationScriptPath, defaultEncoding: null));
new FileTextLoader(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 sealed class EditorTextFactoryService : ITextFactoryService
internal 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)
public SourceText CreateText(Stream stream, Encoding? defaultEncoding, CancellationToken cancellationToken = default)
{
// 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)
public SourceText CreateText(TextReader reader, Encoding? encoding, CancellationToken cancellationToken = default)
{
// 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(CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, CancellationToken cancellationToken)
{
Assert.True(false, $"Content of document should never be loaded");
Assert.True(false, $"Content of document {documentId} 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 WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, encodingA),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileB.Path, encodingB),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileC.Path, encodingC),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileE.Path, encodingE),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileB.Path, encodingB), PreservationMode.PreserveValue);
solution = solution.WithDocumentTextLoader(documentIdB, new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(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 WorkspaceFileTextLoader(solution.Services, sourceFileA.Path, Encoding.UTF8),
loader: new FileTextLoader(sourceFileA.Path, Encoding.UTF8),
filePath: sourceFileA.Path));

var hotReload = new UnitTestingHotReloadService(workspace.Services);
Expand Down
3 changes: 1 addition & 2 deletions src/EditorFeatures/Test/Workspaces/TextFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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 @@ -120,7 +119,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, CancellationToken.None);
var text = textFactoryService.CreateText(stream, defaultEncoding);
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(CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, 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,7 +13,6 @@
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 @@ -279,7 +278,7 @@ private bool RemoveDocumentFromWorkspace(Workspace workspace, MetadataAsSourceGe
var documentId = _openedDocumentIds.GetValueOrDefault(fileInfo);
Contract.ThrowIfNull(documentId);

workspace.OnDocumentClosed(documentId, new WorkspaceFileTextLoader(workspace.Services.SolutionServices, fileInfo.TemporaryFilePath, MetadataAsSourceGeneratedFileInfo.Encoding));
workspace.OnDocumentClosed(documentId, new FileTextLoader(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 WorkspaceFileTextLoader(workspace.Services.SolutionServices, TemporaryFilePath, Encoding) : null);
loader: loadFileFromDisk ? new FileTextLoader(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 WorkspaceFileTextLoader(workspace.Services.SolutionServices, filePath, info.Encoding));
workspace.OnDocumentClosed(info.DocumentId, new FileTextLoader(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 sealed class SourceTextLoader : TextLoader
private 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(CancellationToken cancellationToken)
public override Task<TextAndVersion> LoadTextAndVersionAsync(Workspace workspace, DocumentId documentId, 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 WorkspaceFileTextLoader(languageServices.SolutionServices, mappedPath, parsedCommandLine.Encoding));
loader: new FileTextLoader(mappedPath, parsedCommandLine.Encoding));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
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 @@ -326,8 +325,7 @@ private async Task FormatDocumentCreatedFromTemplateAsync(IVsHierarchy hierarchy

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

var fileLoader = new WorkspaceFileTextLoader(projectToAddTo.Solution.Services, filePath, defaultEncoding: null);
var forkedSolution = projectToAddTo.Solution.AddDocument(DocumentInfo.Create(documentId, filePath, loader: fileLoader, filePath: filePath));
var forkedSolution = projectToAddTo.Solution.AddDocument(DocumentInfo.Create(documentId, filePath, loader: new FileTextLoader(filePath, defaultEncoding: null), 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 sealed class PreviewTextLoader : TextLoader
private class PreviewTextLoader : TextLoader
{
private readonly SourceText _text;

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

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

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

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

private void DetachFromDocument(string moniker)
Expand All @@ -309,7 +308,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 WorkspaceFileTextLoader(Services.SolutionServices, document.FilePath, defaultEncoding: null));
OnDocumentClosed(document.Id, new FileTextLoader(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 WorkspaceFileTextLoader(_project._workspace.Services.SolutionServices, fullPath, defaultEncoding: null);
var textLoader = new FileTextLoader(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 WorkspaceFileTextLoader(_project._workspace.Services.SolutionServices, filePath, defaultEncoding: null)));
documentsToChange.Add((documentId, new FileTextLoader(filePath, defaultEncoding: null)));
}
}
}
Expand Down Expand Up @@ -612,7 +612,7 @@ public SourceTextLoader(SourceTextContainer textContainer, string? filePath)
_filePath = filePath;
}

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