private void AddGlobalAssets(CancellationToken cancellationToken)
            {
                using (Logger.LogBlock(FunctionId.RemoteHostClientService_AddGlobalAssetsAsync, cancellationToken))
                {
                    var snapshotService = _workspace.Services.GetService<ISolutionSynchronizationService>();
                    var assetBuilder = new CustomAssetBuilder(_workspace);

                    foreach (var reference in _analyzerService.GetHostAnalyzerReferences())
                    {
                        var asset = assetBuilder.Build(reference, cancellationToken);
                        snapshotService.AddGlobalAsset(reference, asset, cancellationToken);
                    }
                }
            }
Пример #2
0
        private static async Task VerifyOptionSetsAsync(Workspace workspace, string language)
        {
            var assetBuilder = new CustomAssetBuilder(workspace.CurrentSolution);
            var serializer = new Serializer(workspace.Services);

            var asset = assetBuilder.Build(workspace.Options, language, CancellationToken.None);

            using (var stream = SerializableBytes.CreateWritableStream())
            using (var writer = new ObjectWriter(stream))
            {
                await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false);

                stream.Position = 0;
                using (var reader = new ObjectReader(stream))
                {
                    var recovered = serializer.Deserialize<OptionSet>(asset.Kind, reader, CancellationToken.None);
                    var assetFromStorage = assetBuilder.Build(recovered, language, CancellationToken.None);

                    Assert.Equal(asset.Checksum, assetFromStorage.Checksum);

                    // option should be exactly same
                    Assert.Equal(0, recovered.GetChangedOptions(workspace.Options).Count());
                }
            }
        }
        private CustomAsset GetOptionsAsset(Solution solution, string language, CancellationToken cancellationToken)
        {
            // TODO: we need better way to deal with options. optionSet itself is green node but
            //       it is not part of snapshot and can't save option to solution since we can't use language
            //       specific option without loading related language specific dlls
            var options = solution.Options;
            // we have cached options
            if (_lastOptionSetPerLanguage.TryGetValue(language, out var value) && value.Item1 == options)
            {
                return value.Item2;
            }

            // otherwise, we need to build one.
            var assetBuilder = new CustomAssetBuilder(solution);
            var asset = assetBuilder.Build(options, language, cancellationToken);

            _lastOptionSetPerLanguage[language] = ValueTuple.Create(options, asset);
            return asset;
        }
        public async Task TestHostAnalyzers()
        {
            var code = @"class Test
{
    void Method()
    {
        var t = new Test();
    }
}";

            using (var workspace = await CreateWorkspaceAsync(LanguageNames.CSharp, code))
            {
                var analyzerType = typeof(CSharpUseExplicitTypeDiagnosticAnalyzer);
                var analyzerReference = new AnalyzerFileReference(analyzerType.Assembly.Location, new TestAnalyzerAssemblyLoader());
                var mockAnalyzerService = CreateMockDiagnosticAnalyzerService(new[] { analyzerReference });

                // add host analyzer as global assets
                var snapshotService = workspace.Services.GetService<ISolutionSynchronizationService>();
                var assetBuilder = new CustomAssetBuilder(workspace);

                foreach (var reference in mockAnalyzerService.GetHostAnalyzerReferences())
                {
                    var asset = assetBuilder.Build(reference, CancellationToken.None);
                    snapshotService.AddGlobalAsset(reference, asset, CancellationToken.None);
                }

                // set option
                workspace.Options = workspace.Options.WithChangedOption(CSharpCodeStyleOptions.UseImplicitTypeWhereApparent, new CodeStyleOption<bool>(false, NotificationOption.Suggestion));

                // run analysis
                var project = workspace.CurrentSolution.Projects.First();

                var executor = new VisualStudioDiagnosticAnalyzerExecutor(mockAnalyzerService, new MyUpdateSource(workspace));
                var analyzerDriver = (await project.GetCompilationAsync()).WithAnalyzers(analyzerReference.GetAnalyzers(project.Language).Where(a => a.GetType() == analyzerType).ToImmutableArray());
                var result = await executor.AnalyzeAsync(analyzerDriver, project, CancellationToken.None);

                var analyzerResult = result.AnalysisResult[analyzerDriver.Analyzers[0]];

                // check result
                var diagnostics = analyzerResult.SemanticLocals[analyzerResult.DocumentIds.First()];
                Assert.Equal(IDEDiagnosticIds.UseExplicitTypeDiagnosticId, diagnostics[0].Id);
            }
        }