/// <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); }
public void OnBlockCreated(ITextBuffer editorBuffer, LanguageProjectionBuffer projectionBuffer) { WindowHelpers.WaitFor(delegate { var textView = TextViewConnectionListener.GetFirstViewForBuffer(editorBuffer); if (textView == null) { return(false); } // Add the inner buffer's EditorDocument to the outer buffer before // broken editor code tries to create a new EditorDocument from the // outer buffer. var editorDocument = JSONEditorDocument.FromTextBuffer(projectionBuffer.IProjectionBuffer); ServiceManager.AddService(editorDocument, textView.TextBuffer); editorDocument.Closing += delegate { ServiceManager.RemoveService <JSONEditorDocument>(textView.TextBuffer); }; // JSONIndenter uses TextView.TextBuffer, and therefore operates on the // entire Markdown buffer, breaking everything. I manually force it to // use the inner projection buffer instead. Beware that this breaks its // ViewCaret property, and I can't fix that unless I mock its TextView. var indenter = ServiceManager.GetService <ISmartIndent>(textView); indenter.GetType().GetField("_textBuffer", BindingFlags.Instance | BindingFlags.NonPublic) .SetValue(indenter, projectionBuffer.IProjectionBuffer); return(true); }); }
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); }
public void OnBlockCreated(ITextBuffer editorBuffer, LanguageProjectionBuffer projectionBuffer) { var componentModel = (IComponentModel)ServiceProvider.GetService(typeof(SComponentModel)); var workspace = editorBuffer.Properties.GetOrCreateSingletonProperty(() => new MarkdownWorkspace(MefV1HostServices.Create(componentModel.DefaultExportProvider)) ); var contentType = projectionBuffer.IProjectionBuffer.ContentType.DisplayName; var projectId = editorBuffer.Properties.GetOrCreateSingletonProperty(contentType, () => { var newProject = workspace.CurrentSolution .AddProject(contentType + " Markdown Project", "Markdown", contentTypeLanguages[contentType]) .AddMetadataReferences( DefaultReferences.Select(name => VSWorkspace.CreatePortableExecutableReference( Path.Combine(referenceAssemblyPath, name + ".dll"), MetadataReferenceProperties.Assembly )) ); workspace.TryApplyChanges(newProject.Solution); return(newProject.Id); }); workspace.CreateDocument(projectId, projectionBuffer.IProjectionBuffer); WindowHelpers.WaitFor(delegate { var textView = TextViewConnectionListener.GetFirstViewForBuffer(editorBuffer); if (textView == null) { return(false); } InstallCommandTarget(textView, projectionBuffer.IProjectionBuffer); return(true); }); }
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; }
/// <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); }
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; }
public void OnBlockCreated(ITextBuffer editorBuffer, LanguageProjectionBuffer projectionBuffer) { WindowHelpers.WaitFor(delegate { var textView = TextViewConnectionListener.GetFirstViewForBuffer(editorBuffer); if (textView == null) { return(false); } // Attach the inner buffer's Document to the outer // buffer so that it can be found from the TextView var editorDocument = JSONEditorDocument.FromTextBuffer(projectionBuffer.IProjectionBuffer) ?? JSONEditorDocument.Attach(projectionBuffer.IProjectionBuffer); ServiceManager.AddService(editorDocument, editorBuffer); editorDocument.Closing += delegate { ServiceManager.RemoveService <JSONEditorDocument>(textView.TextBuffer); }; return(true); }); }
/// <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); }
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); }
protected override void OnNavigate(EventArgs e) { if (_source.TextBuffer != null && _item.Line > 0 && _item.Column > 0) { var textView = TextViewConnectionListener.GetFirstViewForBuffer(_source.TextBuffer); if (textView != null) { var snapshot = textView.TextBuffer.CurrentSnapshot; try { int start = snapshot.GetLineFromLineNumber(_item.Line - 1).Start + _item.Column - 1; int end = start + _item.Length; var endLine = snapshot.GetLineFromPosition(end); var textManager = VsAppShell.Current.GetGlobalService <IVsTextManager>(typeof(SVsTextManager)); var textLines = textView.TextBuffer.GetBufferAdapter <IVsTextLines>(); textManager.NavigateToLineAndColumn(textLines, VSConstants.LOGVIEWID_TextView, _item.Line - 1, _item.Column - 1, endLine.LineNumber, end - endLine.Start); } catch (Exception) { } } } }
public static ITextView GetFirstView(this ITextBuffer textBuffer) { return(TextViewConnectionListener.GetFirstViewForBuffer(textBuffer)); }
public IEnumerable <IEditorView> GetAllViews(IEditorBuffer editorBuffer) => TextViewConnectionListener.GetViewsForBuffer(editorBuffer.As <ITextBuffer>()).Select(v => v.ToEditorView()).Where(v => v != null);