예제 #1
0
        public static RoslynWorkspace GetWorkspace(AvalonStudio.Projects.ISolution solution)
        {
            if (!s_solutionWorkspaces.ContainsKey(solution))
            {
                //await PackageManager.EnsurePackage("AvalonStudio.Languages.CSharp", IoC.Get<IConsole>());

                //var dotnetDirectory = Path.Combine(PackageManager.GetPackageDirectory("AvalonStudio.Languages.CSharp"), "content");

                var currentDir = AvalonStudio.Platforms.Platform.ExecutionPath;

                var loadedAssemblies = System.AppDomain.CurrentDomain.GetAssemblies();

                var assemblies = new[]
                {
                    // Microsoft.CodeAnalysis.Workspaces
                    typeof(WorkspacesResources).GetTypeInfo().Assembly,
                    // Microsoft.CodeAnalysis.CSharp.Workspaces
                    typeof(CSharpWorkspaceResources).GetTypeInfo().Assembly,
                    // Microsoft.CodeAnalysis.Features
                    typeof(FeaturesResources).GetTypeInfo().Assembly,
                    // Microsoft.CodeAnalysis.CSharp.Features
                    typeof(CSharpFeaturesResources).GetTypeInfo().Assembly,
                    typeof(RoslynWorkspace).Assembly,
                };

                var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
                                .Distinct()
                                .SelectMany(x => x.GetTypes())
                                .ToArray();

                var compositionContext = new ContainerConfiguration()
                                         .WithParts(partTypes)
                                         .CreateContainer();

                var host = MefHostServices.Create(compositionContext);

                var workspace = new RoslynWorkspace(host, null, compositionContext, DotNetCliService.Instance.Info.Executable, DotNetCliService.Instance.Info.BasePath);

                workspace.RegisterWorkspace(solution);
            }

            return(s_solutionWorkspaces[solution]);
        }
예제 #2
0
 public DocumentTrackingService(Workspace workspace)
 {
     _workspace = (RoslynWorkspace)workspace;
 }
예제 #3
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);
        }
 public DocumentTrackingService(Workspace workspace)
 {
     _workspace = (RoslynWorkspace)workspace;
 }