Пример #1
0
        private MockVsTextView OpenItemWorker(string project, string[] path)
        {
            // matching the API of VisualStudioSolution.OpenItem
            string[] temp = new string[path.Length + 1];
            temp[0] = project;
            Array.Copy(path, 0, temp, 1, path.Length);
            HierarchyItem item = WaitForItemWorker(temp);

            if (item.IsNull)
            {
                return(null);
            }

            if (!CachedInfo._languageNamesByExtension.TryGetValue(Path.GetExtension(item.CanonicalName), out global::System.String languageName))
            {
                languageName = "code";
            }

            MockVsTextView res = CreateTextViewWorker(languageName, File.ReadAllText(item.CanonicalName), view =>
            {
                ErrorHandler.ThrowOnFailure(((IVsTextView)view).GetBuffer(out IVsTextLines lines));
                IntPtr linesPtr = Marshal.GetIUnknownForObject(lines);
                try
                {
                    ErrorHandler.ThrowOnFailure(
                        _rdt.RegisterAndLockDocument(
                            (uint)_VSRDTFLAGS.RDT_NoLock,
                            item.CanonicalName,
                            item.Hierarchy,
                            item.ItemId,
                            linesPtr,
                            out global::System.UInt32 cookie
                            )
                        );
                }
                finally
                {
                    Marshal.Release(linesPtr);
                }
            }, item.CanonicalName);

            if (_docCmdTarget != null)
            {
                ((IFocusable)_docCmdTarget).LostFocus();
            }
            _docCmdTarget = res;
            ((IFocusable)res).GetFocus();

            return(res);
        }
Пример #2
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);
        }
Пример #3
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);
        }
Пример #4
0
 public EditorCommandTarget(MockVsTextView view)
 {
     _view = view;
 }