| File: Shared\Extensions\ProjectExtensions.cs | Web Access |
| Project: src\roslyn\src\Workspaces\Core\Portable\Microsoft.CodeAnalysis.Workspaces.csproj (Microsoft.CodeAnalysis.Workspaces) |
// 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.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.CodeAnalysis.Options; namespace Microsoft.CodeAnalysis.Shared.Extensions; internal static partial class ProjectExtensions { internal static Project WithSolutionOptions(this Project project, OptionSet options) => project.Solution.WithOptions(options).GetProject(project.Id)!; public static TextDocument? GetTextDocument(this Project project, DocumentId? documentId) => project.Solution.GetTextDocument(documentId); internal static DocumentId? GetDocumentForExternalLocation(this Project project, Location location) { Debug.Assert(location.Kind == LocationKind.ExternalFile); return project.GetDocumentIdWithFilePath(location.GetLineSpan().Path); } internal static DocumentId? GetDocumentForFile(this Project project, AdditionalText additionalText) => project.GetDocumentIdWithFilePath(additionalText.Path); private static DocumentId? GetDocumentIdWithFilePath(this Project project, string filePath) => project.Solution.GetDocumentIdsWithFilePath(filePath).FirstOrDefault(id => id.ProjectId == project.Id); public static Document GetRequiredDocument(this Project project, DocumentId documentId) => project.GetDocument(documentId) ?? throw ISolutionExtensions.CreateDocumentNotFoundException(documentId); public static Document GetRequiredDocument(this Project project, SyntaxTree tree) => project.GetDocument(tree) ?? throw ISolutionExtensions.CreateDocumentNotFoundException(tree.FilePath); public static TextDocument GetRequiredAdditionalDocument(this Project project, DocumentId documentId) => project.GetAdditionalDocument(documentId) ?? throw ISolutionExtensions.CreateDocumentNotFoundException(documentId); public static TextDocument GetRequiredAnalyzerConfigDocument(this Project project, DocumentId documentId) => project.GetAnalyzerConfigDocument(documentId) ?? throw ISolutionExtensions.CreateDocumentNotFoundException(documentId); public static TextDocument GetRequiredTextDocument(this Project project, DocumentId documentId) => project.GetTextDocument(documentId) ?? throw ISolutionExtensions.CreateDocumentNotFoundException(documentId); public static async ValueTask<Document> GetRequiredSourceGeneratedDocumentAsync(this Project project, DocumentId documentId, CancellationToken cancellationToken) { var document = await project.GetSourceGeneratedDocumentAsync(documentId, cancellationToken).ConfigureAwait(false); return document ?? throw ISolutionExtensions.CreateDocumentNotFoundException(documentId); } }