Ejemplo n.º 1
0
        async void ShowMultipleFiles(string[] files)
        {
            var openDocuments = EditorHost.GetOpenDocuments();
            var searchCtx     = Presenter.StartSearch($"Go to files", null, false);

            try {
                var msbuildContentType = ContentTypeRegistry.GetContentType(MSBuildContentType.Name);
                foreach (var file in files)
                {
                    string lineText;
                    try {
                        if (!File.Exists(file))
                        {
                            continue;
                        }
                        if (!openDocuments.TryGetValue(file, out var buf))
                        {
                            buf = BufferFactory.CreateTextBuffer(File.OpenText(file), msbuildContentType);
                        }
                        lineText = buf.CurrentSnapshot.GetLineFromPosition(0).GetText();
                    } catch (Exception ex) {
                        LoggingService.LogError($"Error getting text for file {file}", ex);
                        continue;
                    }
                    var classifiedSpans = ImmutableArray <ClassifiedText> .Empty;
                    classifiedSpans = classifiedSpans.Add(new ClassifiedText(lineText, PredefinedClassificationTypeNames.NaturalLanguage));
                    await searchCtx.OnReferenceFoundAsync(new FoundReference (file, 0, 0, ReferenceUsage.Declaration, classifiedSpans, new TextSpan(-1, 0)));
                }
            } catch (Exception ex) when(!(ex is OperationCanceledException && searchCtx.CancellationToken.IsCancellationRequested))
            {
                LoggingService.LogError($"Error in show multiple imports", ex);
            }
            await searchCtx.OnCompletedAsync();
        }
        private IEnumerable <Tuple <string, IContentType> > GetFileNameToContentTypeMappings()
        {
            foreach (var fileNameToContentTypeProduction in FileNameToContentTypeProductions)
            {
                // MEF ensures that there will be at least one content type in the metadata. We take the first one.
                // We prefer this over defining a different attribute from ContentType[] for this purpose.
                IEnumerator <string> cts = fileNameToContentTypeProduction.Metadata.ContentTypes.GetEnumerator();
                cts.MoveNext();

                IContentType contentType = ContentTypeRegistry.GetContentType(cts.Current);

                if (contentType != null)
                {
                    yield return(Tuple.Create(fileNameToContentTypeProduction.Metadata.FileName, contentType));
                }
            }
        }
Ejemplo n.º 3
0
        private MockVsTextView CreateTextViewWorker(
            string contentType,
            string content,
            Action <MockVsTextView> onCreate,
            string file = null
            )
        {
            var buffer = new MockTextBuffer(content, ContentTypeRegistry.GetContentType(contentType), file);

            var view = new MockTextView(buffer);
            var res  = new MockVsTextView(_serviceProvider, this, view);

            view.Properties[typeof(MockVsTextView)] = res;
            onCreate?.Invoke(res);

            var classifier = res.Classifier;

            if (classifier != null)
            {
                classifier.GetClassificationSpans(new SnapshotSpan(buffer.CurrentSnapshot, 0, buffer.CurrentSnapshot.Length));
            }

            // Initialize code window
            LanguageServiceInfo info;

            if (CachedInfo.LangServicesByName.TryGetValue(contentType, out info))
            {
                var id = info.Attribute.LanguageServiceSid;
                var serviceProvider = Container.GetExportedValue <MockVsServiceProvider>();
                var langInfo        = (IVsLanguageInfo)serviceProvider.GetService(id);
                if (langInfo == null)
                {
                    throw new NotImplementedException("Unable to get IVsLanguageInfo for " + info.Attribute.LanguageName);
                }
                IVsCodeWindowManager mgr;
                var codeWindow = new MockCodeWindow(serviceProvider, view);
                view.Properties[typeof(MockCodeWindow)] = codeWindow;
                if (ErrorHandler.Succeeded(langInfo.GetCodeWindowManager(codeWindow, out mgr)))
                {
                    if (ErrorHandler.Failed(mgr.AddAdornments()))
                    {
                        Console.WriteLine("Failed to add adornments to text view");
                    }
                }
            }

            // Initialize intellisense imports
            var providers = Container.GetExports <IIntellisenseControllerProvider, IContentTypeMetadata>();

            foreach (var provider in providers)
            {
                foreach (var targetContentType in provider.Metadata.ContentTypes)
                {
                    if (buffer.ContentType.IsOfType(targetContentType))
                    {
                        provider.Value.TryCreateIntellisenseController(
                            view,
                            new[] { buffer }
                            );
                        break;
                    }
                }
            }

            // tell the world we have a new view...
            foreach (var listener in Container.GetExports <IVsTextViewCreationListener, IContentTypeMetadata>())
            {
                foreach (var targetContentType in listener.Metadata.ContentTypes)
                {
                    if (buffer.ContentType.IsOfType(targetContentType))
                    {
                        listener.Value.VsTextViewCreated(res);
                    }
                }
            }

            OnDispose(() => res.Close());
            return(res);
        }
Ejemplo n.º 4
0
        private MockVsTextView CreateTextViewWorker(string contentType, string file, string content)
        {
            var buffer = new MockTextBuffer(content, ContentTypeRegistry.GetContentType(contentType), file);

            foreach (var classifier in Container.GetExports <IClassifierProvider, IContentTypeMetadata>())
            {
                foreach (var targetContentType in classifier.Metadata.ContentTypes)
                {
                    if (buffer.ContentType.IsOfType(targetContentType))
                    {
                        classifier.Value.GetClassifier(buffer);
                    }
                }
            }

            var view = new MockTextView(buffer);
            var res  = new MockVsTextView(_serviceProvider, this, view);

            view.Properties[typeof(MockVsTextView)] = res;

            // Initialize code window
            LanguageServiceInfo info;

            if (CachedInfo.LangServicesByName.TryGetValue(contentType, out info))
            {
                var id = info.Attribute.LanguageServiceSid;
                var serviceProvider = Container.GetExportedValue <MockVsServiceProvider>();
                var langInfo        = (IVsLanguageInfo)serviceProvider.GetService(id);
                IVsCodeWindowManager mgr;
                var codeWindow = new MockCodeWindow(serviceProvider, view);
                view.Properties[typeof(MockCodeWindow)] = codeWindow;
                if (ErrorHandler.Succeeded(langInfo.GetCodeWindowManager(codeWindow, out mgr)))
                {
                    if (ErrorHandler.Failed(mgr.AddAdornments()))
                    {
                        Console.WriteLine("Failed to add adornments to text view");
                    }
                }
            }

            // Initialize intellisense imports
            var providers = Container.GetExports <IIntellisenseControllerProvider, IContentTypeMetadata>();

            foreach (var provider in providers)
            {
                foreach (var targetContentType in provider.Metadata.ContentTypes)
                {
                    if (buffer.ContentType.IsOfType(targetContentType))
                    {
                        provider.Value.TryCreateIntellisenseController(
                            view,
                            new[] { buffer }
                            );
                        break;
                    }
                }
            }

            // tell the world we have a new view...
            foreach (var listener in Container.GetExports <IVsTextViewCreationListener, IContentTypeMetadata>())
            {
                foreach (var targetContentType in listener.Metadata.ContentTypes)
                {
                    if (buffer.ContentType.IsOfType(targetContentType))
                    {
                        listener.Value.VsTextViewCreated(res);
                    }
                }
            }

            return(res);
        }