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

Add Solution.WithProjectInfo API #74289

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public void EndSession()
_sessionId = default;
}

// access to internal API:
public static Solution WithProjectInfo(Solution solution, ProjectInfo info)
=> solution.WithProjectInfo(info);

internal TestAccessor GetTestAccessor()
=> new(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ static string GetStyledText(TaggedText taggedText, bool isInCodeBlock)
return null;
}

var spanMappingService = document.Services.GetService<ISpanMappingService>();
var spanMappingService = document.DocumentServiceProvider.GetService<ISpanMappingService>();
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
if (spanMappingService == null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private async Task<ImmutableArray<ReferenceLocationDescriptor>> FixUpDescriptors
continue;
}

var spanMapper = document.Services.GetService<ISpanMappingService>();
var spanMapper = document.DocumentServiceProvider.GetService<ISpanMappingService>();
if (spanMapper == null)
{
// for normal document, just add one as they are
Expand All @@ -173,7 +173,7 @@ private async Task<ImmutableArray<ReferenceLocationDescriptor>> FixUpDescriptors
continue;
}

var excerpter = document.Services.GetService<IDocumentExcerptService>();
var excerpter = document.DocumentServiceProvider.GetService<IDocumentExcerptService>();
if (excerpter == null)
{
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ protected async Task AddDocumentSpanEntriesAsync(
var document = documentSpan.Document;
var sourceSpan = documentSpan.SourceSpan;

var excerptService = document.Services.GetService<IDocumentExcerptService>();
var excerptService = document.DocumentServiceProvider.GetService<IDocumentExcerptService>();

// Fetching options is expensive enough to try to avoid it if we can. So only fetch this if absolutely necessary.
ClassificationOptions? options = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ await documentNavigationService.TryNavigateToSpanAsync(

public static async Task<MappedSpanResult?> TryMapAndGetFirstAsync(DocumentSpan documentSpan, SourceText sourceText, CancellationToken cancellationToken)
{
var service = documentSpan.Document.Services.GetService<ISpanMappingService>();
var service = documentSpan.Document.DocumentServiceProvider.GetService<ISpanMappingService>();
if (service == null)
{
return new MappedSpanResult(documentSpan.Document.FilePath, sourceText.Lines.GetLinePositionSpan(documentSpan.SourceSpan), documentSpan.SourceSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private DisposableToolTip CreateDisposableToolTip(Document document, TextSpan so
var controlService = document.Project.Solution.Services.GetRequiredService<IContentControlService>();
var sourceText = document.GetTextSynchronously(CancellationToken.None);

var excerptService = document.Services.GetService<IDocumentExcerptService>();
var excerptService = document.DocumentServiceProvider.GetService<IDocumentExcerptService>();
if (excerptService != null)
{
var classificationOptions = Presenter._globalOptions.GetClassificationOptions(document.Project.Language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ internal override void ApplyMappedFileChanges(SolutionChanges solutionChanges)

bool ShouldApplyChangesToMappedDocuments(CodeAnalysis.Document document, [NotNullWhen(true)] out ISpanMappingService? spanMappingService)
{
spanMappingService = document.Services.GetService<ISpanMappingService>();
spanMappingService = document.DocumentServiceProvider.GetService<ISpanMappingService>();
// Only consider files that are mapped and that we are unable to apply changes to.
// TODO - refactor how this is determined - https://github.com/dotnet/roslyn/issues/47908
return spanMappingService != null && document?.CanApplyChange() == false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static VsTextSpan GetVsTextSpan(SourceText text, int position, int virtualSpace)
}

// Before attempting to open the document, check if the location maps to a different file that should be opened instead.
var spanMappingService = document.Services.GetService<ISpanMappingService>();
var spanMappingService = document.DocumentServiceProvider.GetService<ISpanMappingService>();
if (spanMappingService != null)
{
var mappedSpan = await GetMappedSpanAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static DiagnosticData Create(Diagnostic diagnostic, TextDocument document
var additionalLocations = GetAdditionalLocations(document, diagnostic);
var additionalProperties = GetAdditionalProperties(document, diagnostic);

var documentPropertiesService = document.Services.GetService<DocumentPropertiesService>();
var documentPropertiesService = document.DocumentServiceProvider.GetService<DocumentPropertiesService>();
var diagnosticsLspClientName = documentPropertiesService?.DiagnosticsLspClientName;

if (diagnosticsLspClientName != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Workspaces/Core/Portable/Rename/Renamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static async Task<RenameDocumentActionSet> RenameDocumentAsync(
if (document == null)
throw new ArgumentNullException(nameof(document));

if (document.Services.GetService<ISpanMappingService>() != null)
if (document.DocumentServiceProvider.GetService<ISpanMappingService>() != null)
{
// Don't advertise that we can file rename generated documents that map to a different file.
return new RenameDocumentActionSet([], document.Id, document.Name, [.. document.Folders], options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AdditionalDocumentState(
SolutionServices solutionServices,
DocumentInfo documentInfo,
LoadTextOptions loadTextOptions)
: this(solutionServices, documentInfo.DocumentServiceProvider, documentInfo.Attributes, CreateTextAndVersionSource(solutionServices, documentInfo, loadTextOptions), loadTextOptions)
: this(solutionServices, documentInfo.DocumentServiceProvider, documentInfo.Attributes, CreateTextAndVersionSource(solutionServices, documentInfo.TextLoader, documentInfo.FilePath, loadTextOptions), loadTextOptions)
{
}

Expand All @@ -39,6 +39,14 @@ protected override TextDocumentState UpdateAttributes(DocumentInfo.DocumentAttri
TextAndVersionSource,
LoadTextOptions);

protected override TextDocumentState UpdateDocumentServiceProvider(IDocumentServiceProvider? newProvider)
tmat marked this conversation as resolved.
Show resolved Hide resolved
=> new AdditionalDocumentState(
SolutionServices,
newProvider,
Attributes,
TextAndVersionSource,
LoadTextOptions);

public new AdditionalDocumentState UpdateText(TextLoader loader, PreservationMode mode)
=> (AdditionalDocumentState)base.UpdateText(loader, mode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AnalyzerConfigDocumentState(
SolutionServices solutionServices,
DocumentInfo documentInfo,
LoadTextOptions loadTextOptions)
: this(solutionServices, documentInfo.DocumentServiceProvider, documentInfo.Attributes, CreateTextAndVersionSource(solutionServices, documentInfo, loadTextOptions), loadTextOptions)
: this(solutionServices, documentInfo.DocumentServiceProvider, documentInfo.Attributes, CreateTextAndVersionSource(solutionServices, documentInfo.TextLoader, documentInfo.FilePath, loadTextOptions), loadTextOptions)
{
}

Expand All @@ -47,6 +47,16 @@ protected override TextDocumentState UpdateAttributes(DocumentInfo.DocumentAttri
// Reuse parsed config unless the path changed:
Attributes.FilePath == newAttributes.FilePath ? _lazyAnalyzerConfig : null);

protected override TextDocumentState UpdateDocumentServiceProvider(IDocumentServiceProvider? newProvider)
=> new AnalyzerConfigDocumentState(
SolutionServices,
DocumentServiceProvider,
Attributes,
TextAndVersionSource,
LoadTextOptions,
// Reuse parsed config:
_lazyAnalyzerConfig);

public new AnalyzerConfigDocumentState UpdateText(TextLoader loader, PreservationMode mode)
=> (AnalyzerConfigDocumentState)base.UpdateText(loader, mode);

Expand Down
39 changes: 29 additions & 10 deletions src/Workspaces/Core/Portable/Workspace/Solution/DocumentState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static DocumentState Create(
ParseOptions? options,
LoadTextOptions loadTextOptions)
{
var textSource = CreateTextAndVersionSource(languageServices.SolutionServices, info, loadTextOptions);
var textSource = CreateTextAndVersionSource(languageServices.SolutionServices, info.TextLoader, info.FilePath, loadTextOptions);

// If this is document that doesn't support syntax, then don't even bother holding
// onto any tree source. It will never be used to get a tree, and can only hurt us
Expand Down Expand Up @@ -336,7 +336,7 @@ public DocumentState UpdateChecksumAlgorithm(SourceHashAlgorithm checksumAlgorit
newTreeSource);
}

public DocumentState UpdateParseOptionsAndSourceCodeKind(ParseOptions options, bool onlyPreprocessorDirectiveChange)
public DocumentState UpdateParseOptionsAndSourceCodeKind(ParseOptions? options, bool onlyPreprocessorDirectiveChange)
tmat marked this conversation as resolved.
Show resolved Hide resolved
{
Contract.ThrowIfFalse(SupportsSyntaxTree);

Expand All @@ -350,6 +350,8 @@ public DocumentState UpdateParseOptionsAndSourceCodeKind(ParseOptions options, b
if (onlyPreprocessorDirectiveChange &&
TreeSource.TryGetValue(out var existingTreeAndVersion))
{
Debug.Assert(options != null);

var existingTree = existingTreeAndVersion.Tree;

SyntaxTree? newTree = null;
Expand All @@ -365,18 +367,25 @@ public DocumentState UpdateParseOptionsAndSourceCodeKind(ParseOptions options, b
newTreeSource = SimpleTreeAndVersionSource.Create(new TreeAndVersion(newTree, existingTreeAndVersion.Version));
}

// If we weren't able to reuse in a smart way, just reparse
newTreeSource ??= CreateLazyFullyParsedTree(
TextAndVersionSource,
LoadTextOptions,
Attributes.SyntaxTreeFilePath,
options,
LanguageServices);
if (options != null)
{
// If we weren't able to reuse in a smart way, just reparse
newTreeSource ??= CreateLazyFullyParsedTree(
TextAndVersionSource,
LoadTextOptions,
Attributes.SyntaxTreeFilePath,
options,
LanguageServices);
}
else
{
newTreeSource = null;
tmat marked this conversation as resolved.
Show resolved Hide resolved
}

return new DocumentState(
LanguageServices,
DocumentServiceProvider,
Attributes.With(sourceCodeKind: options.Kind),
(options != null) ? Attributes.With(sourceCodeKind: options.Kind) : Attributes,
tmat marked this conversation as resolved.
Show resolved Hide resolved
TextAndVersionSource,
LoadTextOptions,
options,
Expand Down Expand Up @@ -430,6 +439,16 @@ protected override TextDocumentState UpdateAttributes(DocumentInfo.DocumentAttri
newTreeSource);
}

protected override TextDocumentState UpdateDocumentServiceProvider(IDocumentServiceProvider? newProvider)
=> new DocumentState(
LanguageServices,
newProvider,
Attributes,
TextAndVersionSource,
LoadTextOptions,
ParseOptions,
TreeSource);

public new DocumentState WithAttributes(DocumentInfo.DocumentAttributes newAttributes)
=> (DocumentState)base.WithAttributes(newAttributes);

Expand Down
Loading
Loading