Example #1
0
        /// <summary>
        /// Attempts to locate associated editor document. Implementation depends on the platform.
        /// </summary>
        /// <typeparam name="T">Type of the document to locate</typeparam>
        public static T GetEditorDocument <T>(this ITextBuffer textBuffer) where T : class, IEditorDocument
        {
            var editorBuffer = textBuffer.ToEditorBuffer();
            var document     = editorBuffer?.GetService <T>();

            if (document != null)
            {
                return(document);
            }

            // May be top-level projection buffer such as REPL or markdown
            var pb = textBuffer as IProjectionBuffer;

            document = pb?.SourceBuffers?.Select((tb) => tb.GetService <T>())?.FirstOrDefault(x => x != null);
            if (document == null)
            {
                var viewData = TextViewConnectionListener.GetTextViewDataForBuffer(textBuffer);
                if (viewData?.LastActiveView != null)
                {
                    var controller = ViewController.FromTextView(viewData.LastActiveView);
                    if (controller != null && controller.TextBuffer != null)
                    {
                        document = controller.TextBuffer.GetService <T>();
                    }
                }
            }
            return(document);
        }
Example #2
0
        public IWpfTextView CreateTextView(IEnumerable <SnapshotSpan> lines)
        {
            var parentView = TextViewConnectionListener.GetTextViewDataForBuffer(lines.First().Snapshot.TextBuffer).LastActiveView;

            var buffer = ProjectionFactory.CreateProjectionBuffer(
                null,
                lines.SelectMany(s => new object[] { Environment.NewLine }.Concat(
                                     // Use the text from the outer ProjectionBuffer, which can
                                     // include language services from other projected buffers.
                                     // This makes the tooltip include syntax highlighting that
                                     // does not exist in the innermost Markdown buffer.
                                     parentView.BufferGraph
                                     .MapUpToBuffer(s, SpanTrackingMode.EdgeExclusive, parentView.TextBuffer)
                                     .Select(s2 => s2.Snapshot.CreateTrackingSpan(s, SpanTrackingMode.EdgeExclusive))
                                     ))
                .Skip(1)        // Skip first newline
                .ToList(),
                ProjectionBufferOptions.None
                );
            var view = TextEditorFactory.CreateTextView(buffer, TextEditorFactory.NoRoles);

            view.Background = Brushes.Transparent;
            SizeToFit(view);
            return(view);
        }
Example #3
0
        internal void Create(HtmlEditorDocument document, IVsContainedLanguage containedLanguage, IVsTextBufferCoordinator bufferCoordinator, LanguageProjectionBuffer languageBuffer, out IVsTextViewFilter containedLanguageViewfilter)
        {
            containedLanguageViewfilter = null;
            TextViewData textViewDataForBuffer = TextViewConnectionListener.GetTextViewDataForBuffer(document.TextBuffer);

            if (textViewDataForBuffer == null || textViewDataForBuffer.LastActiveView == null)
            {
                return;
            }
            TextView = textViewDataForBuffer.LastActiveView;
            IVsTextViewIntellisenseHostProvider vsTextViewIntellisenseHostProvider = TextView.QueryInterface <IVsTextViewIntellisenseHostProvider>();

            if (vsTextViewIntellisenseHostProvider == null)
            {
                return;
            }

            Guid   gUID = typeof(IVsTextViewIntellisenseHost).GUID;
            IntPtr intPtr;

            vsTextViewIntellisenseHostProvider.CreateIntellisenseHost(bufferCoordinator, ref gUID, out intPtr);
            if (intPtr == IntPtr.Zero)
            {
                return;
            }

            IVsTextViewIntellisenseHost vsTextViewIntellisenseHost = Marshal.GetObjectForIUnknown(intPtr) as IVsTextViewIntellisenseHost;

            Marshal.Release(intPtr);
            if (vsTextViewIntellisenseHost == null)
            {
                return;
            }

            HtmlMainController htmlMainController = HtmlMainController.FromTextView(TextView);
            ICommandTarget     chainedController  = htmlMainController.ChainedController;

            if (chainedController == null)
            {
                return;
            }

            OleToCommandTargetShim oleToCommandTargetShim = chainedController as OleToCommandTargetShim;

            if (containedLanguage.GetTextViewFilter(vsTextViewIntellisenseHost, oleToCommandTargetShim.OleTarget, out containedLanguageViewfilter) != 0)
            {
                return;
            }

            IOleCommandTarget      oleTarget = containedLanguageViewfilter as IOleCommandTarget;
            OleToCommandTargetShim containedLanguageTarget = new OleToCommandTargetShim(TextView, oleTarget);

            ContainedLanguageTarget = containedLanguageTarget;

            _languageBuffer = languageBuffer;
            _languageBuffer.MappingsChanged += OnMappingsChanged;
        }
Example #4
0
        /// <summary>
        /// Retrieves last active view, if any, for the text buffer instance.
        /// Text buffer is not guaranteed to have a view.
        /// </summary>
        public static ITextView CurrentTextView(this ITextBuffer textBuffer)
        {
            ITextView textView = null;

            if (textBuffer != null)
            {
                var textViewData = TextViewConnectionListener.GetTextViewDataForBuffer(textBuffer);
                textView = textViewData?.LastActiveView;
            }
            return(textView);
        }
Example #5
0
        public static ITextView CurrentTextView(this ITextBuffer viewBuffer) {
            ITextView textView = null;
            if (viewBuffer != null) {
                TextViewData textViewData = TextViewConnectionListener.GetTextViewDataForBuffer(viewBuffer);
                if (textViewData != null) {
                    textView = textViewData.LastActiveView;
                }
            }

            return textView;
        }
Example #6
0
        /// <summary>
        /// Retrieves document instance from text buffer
        /// </summary>
        public static IEditorDocument TryFromTextBuffer(ITextBuffer textBuffer)
        {
            IEditorDocument document = ServiceManager.GetService <IEditorDocument>(textBuffer);

            if (document == null)
            {
                TextViewData viewData = TextViewConnectionListener.GetTextViewDataForBuffer(textBuffer);
                if (viewData != null && viewData.LastActiveView != null)
                {
                    MdMainController controller = MdMainController.FromTextView(viewData.LastActiveView);
                    if (controller != null && controller.TextBuffer != null)
                    {
                        document = ServiceManager.GetService <MdEditorDocument>(controller.TextBuffer);
                    }
                }
            }

            return(document);
        }
Example #7
0
        public static T TryFromTextBuffer <T>(ITextBuffer textBuffer, string contentType) where T : class, IEditorDocument
        {
            var document = ServiceManager.GetService <T>(textBuffer);

            if (document == null)
            {
                document = FindInProjectedBuffers <T>(textBuffer, contentType);
                if (document == null)
                {
                    TextViewData viewData = TextViewConnectionListener.GetTextViewDataForBuffer(textBuffer);
                    if (viewData != null && viewData.LastActiveView != null)
                    {
                        var controller = ViewController.FromTextView(viewData.LastActiveView);
                        if (controller != null && controller.TextBuffer != null)
                        {
                            document = ServiceManager.GetService <T>(controller.TextBuffer);
                        }
                    }
                }
            }
            return(document);
        }