Exemplo n.º 1
0
        protected override void ApplyDocumentTextChanged(DocumentId document, SourceText newText)
        {
            _openDocumentTextLoaders[document].UpdateText(newText);

            ApplyingTextChange?.Invoke(document, newText);

            OnDocumentTextChanged(document, newText, PreservationMode.PreserveIdentity);
        }
Exemplo n.º 2
0
        protected override void ApplyDocumentTextChanged(DocumentId document, SourceText newText)
        {
            if (OpenDocumentId != document)
            {
                return;
            }

            ApplyingTextChange?.Invoke(document, newText);

            OnDocumentTextChanged(document, newText, PreservationMode.PreserveIdentity);
        }
Exemplo n.º 3
0
        protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceText text)
        {
            if (_openDocumentTextLoaders.ContainsKey(documentId))
            {
                _openDocumentTextLoaders[documentId].UpdateText(text);
            }
            else
            {
                var document = this.CurrentSolution.GetDocument(documentId);
                if (document != null)
                {
                    Encoding encoding = DetermineEncoding(text, document);

                    SaveDocumentText(documentId, document.FilePath, text, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
                    OnDocumentTextChanged(documentId, text, PreservationMode.PreserveValue);
                }
            }

            ApplyingTextChange?.Invoke(documentId, text);

            OnDocumentTextChanged(documentId, text, PreservationMode.PreserveIdentity);
        }
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);
        }