Exemplo n.º 1
0
        public RoslynHost(NuGetConfiguration nuGetConfiguration = null, IEnumerable <Assembly> additionalAssemblies = null)
        {
            _nuGetConfiguration = nuGetConfiguration;

            _workspaces = new ConcurrentDictionary <DocumentId, RoslynWorkspace>();
            _diagnosticsUpdatedNotifiers = new ConcurrentDictionary <DocumentId, Action <DiagnosticsUpdatedArgs> >();

            var assemblies = new[]
            {
                Assembly.Load("Microsoft.CodeAnalysis"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
                Assembly.Load("Microsoft.CodeAnalysis.Features"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
                typeof(RoslynHost).Assembly,
            };

            if (additionalAssemblies != null)
            {
                assemblies = assemblies.Concat(additionalAssemblies).ToArray();
            }

            var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
                            .Distinct()
                            .SelectMany(x => x.GetTypes())
                            .Concat(new[] { typeof(Microsoft.CodeAnalysis.CodeFixes.CodeFixService) })
                            .Concat(new[] { typeof(DocumentationProviderServiceFactory) })
                            .ToArray();

            _compositionContext = new ContainerConfiguration()
                                  .WithParts(partTypes)
                                  .WithDefaultConventions(new AttributeFilterProvider())
                                  .CreateContainer();

            _host = MefHostServices.Create(_compositionContext);

            _parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: new[] { "__DEMO__", "__DEMO_EXPERIMENTAL__" });

            _referenceAssembliesPath      = GetReferenceAssembliesPath();
            _documentationProviderService = new DocumentationProviderServiceFactory.DocumentationProviderService();

            DefaultReferences = _defaultReferenceAssemblies.Select(t =>
                                                                   (MetadataReference)MetadataReference.CreateFromFile(t.Location,
                                                                                                                       documentation: GetDocumentationProvider(t.Location))).ToImmutableArray();

            DefaultImports = _defaultReferenceAssemblyTypes.Select(x => x.Namespace).Distinct().ToImmutableArray();

            GetService <IDiagnosticService>().DiagnosticsUpdated += OnDiagnosticsUpdated;

            _compositionContext.GetExport <ISemanticChangeNotificationService>().OpenedDocumentSemanticChanged +=
                OnOpenedDocumentSemanticChanged;
        }
Exemplo n.º 2
0
        public RoslynHost(NuGetConfiguration nuGetConfiguration       = null,
                          IEnumerable <Assembly> additionalAssemblies = null,
                          IEnumerable <string> additionalReferencedAssemblyLocations = null)
        {
            _nuGetConfiguration = nuGetConfiguration;

            _workspaces = new ConcurrentDictionary <DocumentId, RoslynWorkspace>();
            _diagnosticsUpdatedNotifiers = new ConcurrentDictionary <DocumentId, Action <DiagnosticsUpdatedArgs> >();

            var assemblies = new[]
            {
                Assembly.Load("Microsoft.CodeAnalysis"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
                Assembly.Load("Microsoft.CodeAnalysis.Features"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
                typeof(RoslynHost).Assembly,
            };

            if (additionalAssemblies != null)
            {
                assemblies = assemblies.Concat(additionalAssemblies).ToArray();
            }

            var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
                            .Distinct()
                            .SelectMany(x => x.GetTypes())
                            .Concat(new[] { typeof(DocumentationProviderServiceFactory) })
                            .ToArray();

            _compositionContext = new ContainerConfiguration()
                                  .WithParts(partTypes)
                                  .CreateContainer();

            _host = MefHostServices.Create(_compositionContext);

            _parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script, preprocessorSymbols: PreprocessorSymbols);

            (_referenceAssembliesPath, _documentationPath) = GetReferenceAssembliesPath();
            _documentationProviderService = new DocumentationProviderServiceFactory.DocumentationProviderService();

            DefaultReferences = GetMetadataReferences(additionalReferencedAssemblyLocations);

            DefaultImports = _defaultReferenceAssemblyTypes.Select(x => x.Namespace).Distinct().ToImmutableArray();

            GetService <IDiagnosticService>().DiagnosticsUpdated += OnDiagnosticsUpdated;
        }
Exemplo n.º 3
0
        public RoslynHost(NuGetConfiguration nuGetConfiguration = null, IEnumerable <Assembly> additionalAssemblies = null)
        {
            _nuGetConfiguration = nuGetConfiguration;

            _workspaces = new ConcurrentDictionary <DocumentId, RoslynWorkspace>();
            _diagnosticsUpdatedNotifiers = new ConcurrentDictionary <DocumentId, Action <DiagnosticsUpdatedArgs> >();

            var assemblies = new[]
            {
                Assembly.Load("Microsoft.CodeAnalysis"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
                Assembly.Load("Microsoft.CodeAnalysis.Features"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
                typeof(RoslynHost).Assembly,
            };

            if (additionalAssemblies != null)
            {
                assemblies = assemblies.Concat(additionalAssemblies).ToArray();
            }

            var editorFeaturesAssembly = Assembly.Load("Microsoft.CodeAnalysis.EditorFeatures");
            var editorFeaturesTypes    = SafeGetAssemblyTypes(editorFeaturesAssembly);

            // we can't import this entire assembly due to composition errors
            // and we don't need all the VS services
            var editorFeaturesParts = editorFeaturesTypes
                                      .Where(x => x.Namespace == "Microsoft.CodeAnalysis.CodeFixes")
                                      .Concat(new[] { typeof(DocumentationProviderServiceFactory) });

            _compositionContext = new ContainerConfiguration()
                                  .WithAssemblies(MefHostServices.DefaultAssemblies.Concat(assemblies))
                                  .WithParts(editorFeaturesParts)
                                  .WithDefaultConventions(new AttributeFilterProvider())
                                  .CreateContainer();

            _host = MefHostServices.Create(_compositionContext);

            _parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script);

            _referenceAssembliesPath      = GetReferenceAssembliesPath();
            _documentationProviderService = new DocumentationProviderServiceFactory.DocumentationProviderService();

            DefaultReferences = _defaultReferenceAssemblies.Select(t =>
                                                                   (MetadataReference)MetadataReference.CreateFromFile(t.Location,
                                                                                                                       documentation: GetDocumentationProvider(t.Location))).ToImmutableArray();

            DefaultImports = _defaultReferenceAssemblyTypes.Select(x => x.Namespace).Distinct().ToImmutableArray();

            GetService <IDiagnosticService>().DiagnosticsUpdated += OnDiagnosticsUpdated;

            _compositionContext.GetExport <ISemanticChangeNotificationService>().OpenedDocumentSemanticChanged +=
                OnOpenedDocumentSemanticChanged;

            // MEF v1
            var csharpEditorFeatures = Assembly.Load("Microsoft.CodeAnalysis.CSharp.EditorFeatures");

            var container = new CompositionContainer(new AggregateCatalog(
                                                         new TypeCatalog(editorFeaturesTypes),
                                                         new TypeCatalog(SafeGetAssemblyTypes(csharpEditorFeatures)),
                                                         new AssemblyCatalog(typeof(RoslynHost).Assembly)),
                                                     CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe);

            ((AggregateSignatureHelpProvider)GetService <ISignatureHelpProvider>()).Initialize(container);

            CompletionService.Initialize(container);
        }
Exemplo n.º 4
0
        public RoslynHost(INuGetProvider nuGetProvider = null)
        {
            _nuGetProvider           = nuGetProvider;
            _referencesDirectives    = new ConcurrentDictionary <string, DirectiveInfo>();
            _referenceDirectivesLock = new SemaphoreSlim(1, 1);

            var assemblies = new[]
            {
                Assembly.Load("Microsoft.CodeAnalysis"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
                Assembly.Load("Microsoft.CodeAnalysis.Features"),
                Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
                typeof(RoslynHost).Assembly,
            };

            // we can't import this entire assembly due to composition errors
            // and we don't need all the VS services
            var editorFeaturesAssembly = Assembly.Load("Microsoft.CodeAnalysis.EditorFeatures");
            var types = editorFeaturesAssembly.GetTypes().Where(x => x.Namespace == "Microsoft.CodeAnalysis.CodeFixes");

            _compositionContext = new ContainerConfiguration()
                                  .WithAssemblies(MefHostServices.DefaultAssemblies.Concat(assemblies))
                                  .WithParts(types)
                                  .WithDefaultConventions(new AttributeFilterProvider())
                                  .CreateContainer();

            var host = MefHostServices.Create(_compositionContext);

            _workspace = new RoslynWorkspace(host, this);
            _workspace.ApplyingTextChange += (d, s) => ApplyingTextChange?.Invoke(d, s);

            _parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script);

            _referenceAssembliesPath      = GetReferenceAssembliesPath();
            _documentationProviderService = new DocumentationProviderServiceFactory.DocumentationProviderService();

            _references = _defaultReferenceAssemblies.Select(t =>
                                                             (MetadataReference)MetadataReference.CreateFromFile(t.Location,
                                                                                                                 documentation: GetDocumentationProvider(t.Location))).ToImmutableArray();
            var metadataReferenceResolver = CreateMetadataReferenceResolver();

            _compilationOptions = new CSharpCompilationOptions(OutputKind.NetModule,
                                                               usings: _defaultReferenceAssemblyTypes.Select(x => x.Namespace).ToImmutableArray(),
                                                               metadataReferenceResolver: metadataReferenceResolver);

            _workspace.Services.GetService <Microsoft.CodeAnalysis.SolutionCrawler.ISolutionCrawlerRegistrationService>()
            .Register(_workspace);

            _compositionContext.GetExport <ISemanticChangeNotificationService>().OpenedDocumentSemanticChanged +=
                OnOpenedDocumentSemanticChanged;

            // MEF v1
            var container = new CompositionContainer(new AggregateCatalog(
                                                         new AssemblyCatalog(Assembly.Load("Microsoft.CodeAnalysis.EditorFeatures")),
                                                         new AssemblyCatalog(Assembly.Load("Microsoft.CodeAnalysis.CSharp.EditorFeatures")),
                                                         new AssemblyCatalog(typeof(RoslynHost).Assembly)),
                                                     CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe);

            ((AggregateSignatureHelpProvider)GetService <ISignatureHelpProvider>()).Initialize(container);

            CompletionService.Initialize(container);
        }