예제 #1
0
        public async ValueTask EndDebuggingSessionAsync(Solution compileTimeSolution, EditAndContinueDiagnosticUpdateSource diagnosticUpdateSource, IDiagnosticAnalyzerService diagnosticService, CancellationToken cancellationToken)
        {
            ImmutableArray <DocumentId> documentsToReanalyze;

            var client = await RemoteHostClient.TryGetClientAsync(_workspace, cancellationToken).ConfigureAwait(false);

            if (client == null)
            {
                GetLocalService().EndDebuggingSession(_sessionId, out documentsToReanalyze);
            }
            else
            {
                var documentsToReanalyzeOpt = await client.TryInvokeAsync <IRemoteEditAndContinueService, ImmutableArray <DocumentId> >(
                    (service, cancallationToken) => service.EndDebuggingSessionAsync(_sessionId, cancellationToken),
                    cancellationToken).ConfigureAwait(false);

                documentsToReanalyze = documentsToReanalyzeOpt.HasValue ? documentsToReanalyzeOpt.Value : ImmutableArray <DocumentId> .Empty;
            }

            var designTimeDocumentsToReanalyze = await CompileTimeSolutionProvider.GetDesignTimeDocumentsAsync(
                compileTimeSolution, documentsToReanalyze, designTimeSolution : _workspace.CurrentSolution, cancellationToken).ConfigureAwait(false);

            // clear all reported rude edits:
            diagnosticService.Reanalyze(_workspace, documentIds: designTimeDocumentsToReanalyze);

            // clear emit/apply diagnostics reported previously:
            diagnosticUpdateSource.ClearDiagnostics(isSessionEnding: true);

            Dispose();
        }
예제 #2
0
        public async Task TryGetCompileTimeDocumentAsync()
        {
            var workspace = new TestWorkspace(composition: FeaturesTestCompositions.Features);
            var projectId = ProjectId.CreateNewId();

            var projectFilePath    = Path.Combine(TempRoot.Root, "a.csproj");
            var additionalFilePath = Path.Combine(TempRoot.Root, "a", "X.razor");
            var designTimeFilePath = Path.Combine(TempRoot.Root, "a", "X.razor.g.cs");
            var generatedHintName  = @"_a_X_razor.cs";

            var generator = new TestSourceGenerator()
            {
                ExecuteImpl = context => context.AddSource(generatedHintName, "")
            };
            var sourceGeneratedPathPrefix = Path.Combine(typeof(TestSourceGenerator).Assembly.GetName().Name, typeof(TestSourceGenerator).FullName);
            var analyzerConfigId          = DocumentId.CreateNewId(projectId);
            var documentId           = DocumentId.CreateNewId(projectId);
            var additionalDocumentId = DocumentId.CreateNewId(projectId);
            var designTimeDocumentId = DocumentId.CreateNewId(projectId);

            var designTimeSolution = workspace.CurrentSolution.
                                     AddProject(ProjectInfo.Create(projectId, VersionStamp.Default, "proj", "proj", LanguageNames.CSharp, filePath: projectFilePath)).
                                     WithProjectMetadataReferences(projectId, TargetFrameworkUtil.GetReferences(TargetFramework.NetStandard20)).
                                     AddAnalyzerReference(projectId, new TestGeneratorReference(generator)).
                                     AddAdditionalDocument(additionalDocumentId, "additional", SourceText.From(""), filePath: additionalFilePath).
                                     AddAnalyzerConfigDocument(analyzerConfigId, "config", SourceText.From(""), filePath: "RazorSourceGenerator.razorencconfig").
                                     AddDocument(documentId, "a.cs", "").
                                     AddDocument(DocumentInfo.Create(
                                                     designTimeDocumentId,
                                                     name: "a",
                                                     folders: Array.Empty <string>(),
                                                     sourceCodeKind: SourceCodeKind.Regular,
                                                     loader: null,
                                                     filePath: designTimeFilePath,
                                                     isGenerated: true,
                                                     designTimeOnly: true,
                                                     documentServiceProvider: null));

            var designTimeDocument = designTimeSolution.GetRequiredDocument(designTimeDocumentId);

            var provider            = workspace.Services.GetRequiredService <ICompileTimeSolutionProvider>();
            var compileTimeSolution = provider.GetCompileTimeSolution(designTimeSolution);

            Assert.False(compileTimeSolution.ContainsAnalyzerConfigDocument(analyzerConfigId));
            Assert.False(compileTimeSolution.ContainsDocument(designTimeDocumentId));
            Assert.True(compileTimeSolution.ContainsDocument(documentId));

            var sourceGeneratedDoc = (await compileTimeSolution.Projects.Single().GetSourceGeneratedDocumentsAsync()).Single();

            var compileTimeDocument = await CompileTimeSolutionProvider.TryGetCompileTimeDocumentAsync(designTimeDocument, compileTimeSolution, CancellationToken.None, sourceGeneratedPathPrefix);

            Assert.Same(sourceGeneratedDoc, compileTimeDocument);

            var actualDesignTimeDocumentIds = await CompileTimeSolutionProvider.GetDesignTimeDocumentsAsync(
                compileTimeSolution, ImmutableArray.Create(documentId, sourceGeneratedDoc.Id), designTimeSolution, CancellationToken.None, sourceGeneratedPathPrefix);

            AssertEx.Equal(new[] { documentId, designTimeDocumentId }, actualDesignTimeDocumentIds);
        }
예제 #3
0
        private static async Task <ImmutableArray <Diagnostic> > AnalyzeSemanticsImplAsync(Document designTimeDocument, CancellationToken cancellationToken)
        {
            var workspace = designTimeDocument.Project.Solution.Workspace;

            if (workspace.Services.HostServices is not IMefHostExportProvider mefServices)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            // avoid creating and synchronizing compile-time solution if the Hot Reload/EnC session is not active
            if (mefServices.GetExports <EditAndContinueLanguageService>().SingleOrDefault()?.Value.IsSessionActive != true)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            var designTimeSolution  = designTimeDocument.Project.Solution;
            var compileTimeSolution = workspace.Services.GetRequiredService <ICompileTimeSolutionProvider>().GetCompileTimeSolution(designTimeSolution);

            var compileTimeDocument = await CompileTimeSolutionProvider.TryGetCompileTimeDocumentAsync(designTimeDocument, compileTimeSolution, cancellationToken).ConfigureAwait(false);

            if (compileTimeDocument == null)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            // EnC services should never be called on a design-time solution.

            var proxy = new RemoteEditAndContinueServiceProxy(workspace);

            var activeStatementSpanProvider = new ActiveStatementSpanProvider(async(documentId, filePath, cancellationToken) =>
            {
                var trackingService = workspace.Services.GetRequiredService <IActiveStatementTrackingService>();
                return(await trackingService.GetSpansAsync(compileTimeSolution, documentId, filePath, cancellationToken).ConfigureAwait(false));
            });

            return(await proxy.GetDocumentDiagnosticsAsync(compileTimeDocument, designTimeDocument, activeStatementSpanProvider, cancellationToken).ConfigureAwait(false));
        }
예제 #4
0
        private static async Task <ImmutableArray <Diagnostic> > AnalyzeSemanticsImplAsync(Workspace workspace, Document designTimeDocument, CancellationToken cancellationToken)
        {
            var designTimeSolution  = designTimeDocument.Project.Solution;
            var compileTimeSolution = workspace.Services.GetRequiredService <ICompileTimeSolutionProvider>().GetCompileTimeSolution(designTimeSolution);

            var compileTimeDocument = await CompileTimeSolutionProvider.TryGetCompileTimeDocumentAsync(designTimeDocument, compileTimeSolution, cancellationToken).ConfigureAwait(false);

            if (compileTimeDocument == null)
            {
                return(ImmutableArray <Diagnostic> .Empty);
            }

            // EnC services should never be called on a design-time solution.

            var proxy = new RemoteEditAndContinueServiceProxy(workspace);

            var activeStatementSpanProvider = new ActiveStatementSpanProvider(async(documentId, filePath, cancellationToken) =>
            {
                var trackingService = workspace.Services.GetRequiredService <IActiveStatementTrackingService>();
                return(await trackingService.GetSpansAsync(compileTimeSolution, documentId, filePath, cancellationToken).ConfigureAwait(false));
            });

            return(await proxy.GetDocumentDiagnosticsAsync(compileTimeDocument, designTimeDocument, activeStatementSpanProvider, cancellationToken).ConfigureAwait(false));
        }