public EditorWrapper(String contentType) { ThreadHelper.ThrowIfNotOnUIThread(); var serviceProvider = (IServiceProvider)ServiceProvider.GlobalProvider.GetService(typeof(IServiceProvider)); var componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)); if (componentModel == null) { throw new InvalidOperationException("SComponentModel service is not available"); } var service = componentModel.GetService <EditorServices>(); var message = ""; var type = service.ContentTypeRegistryService.GetContentType(contentType); // var CSharpLanguageServiceId = new Guid("694dd9b6-b865-4c5b-ad85-86356e9c88dc"); // var VisualBasicLanguageServiceId = new Guid("e34acdc0-baae-11d0-88bf-00a0c9110049"); VsTextLines = (IVsTextLines)service.VsEditorAdaptersFactoryService.CreateVsTextBufferAdapter(serviceProvider, type); VsTextLines.InitializeContent(message, message.Length); // vsTextBuffer.SetLanguageServiceID(ref CSharpLanguageServiceId); var textRoles = service.TextEditorFactoryService.CreateTextViewRoleSet( PredefinedTextViewRoles.Analyzable, PredefinedTextViewRoles.Document, PredefinedTextViewRoles.Editable, PredefinedTextViewRoles.Interactive, //PredefinedTextViewRoles.PrimaryDocument, PredefinedTextViewRoles.Structured, PredefinedTextViewRoles.Zoomable ); VsTextView = service.VsEditorAdaptersFactoryService.CreateVsTextViewAdapter(serviceProvider, textRoles); var initFlags = (UInt32)TextViewInitFlags.VIF_HSCROLL | (UInt32)TextViewInitFlags.VIF_SET_DRAGDROPMOVE | // (uint)TextViewInitFlags.VIF_SET_VISIBLE_WHITESPACE | (UInt32)TextViewInitFlags.VIF_VSCROLL | (UInt32)TextViewInitFlags3.VIF_NO_HWND_SUPPORT ; var initView = new INITVIEW[] { new INITVIEW { fSelectionMargin = 0, // original: 0 fWidgetMargin = 0, // original: 0 fVirtualSpace = 0, fDragDropMove = 1, } }; VsTextView.Initialize(VsTextLines, IntPtr.Zero, initFlags, initView); WpfTextViewHost = service.VsEditorAdaptersFactoryService.GetWpfTextViewHost(VsTextView); WpfTextViewHost.TextView.Options.SetOptionValue("Appearance/Category", "text"); WpfTextViewHost.TextView.Options.SetOptionValue("TextView/UseVisibleWhitespace", true); WpfTextViewHost.TextView.Options.SetOptionValue("TextViewHost/LineNumberMargin", true); }
void CreateCodeEditor() { var editorSvc = Services.GetComponentService <IVsEditorAdaptersFactoryService>(); codeWindow = editorSvc.CreateVsCodeWindowAdapter(Services.ServiceProvider); // disable splitter since it will cause a crash var codeWindowEx = (IVsCodeWindowEx)codeWindow; var initView = new INITVIEW[1]; ErrorHandler.ThrowOnFailure(codeWindowEx.Initialize( (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: string.Empty, szValueAuxUserContext: string.Empty, InitViewFlags: 0, pInitView: initView)); ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(textBuffer)); //needed for xeto/jeto files, not implemented for c# etc so we don't worry about the result Guid clsIdView = VSConstants.LOGVIEWID.TextView_guid; //codeWindow.SetViewClassID(ref clsIdView); if (ErrorHandler.Succeeded(codeWindow.GetPrimaryView(out viewAdapter))) { // get the view first so host is created //var wpfView = editorSvc.GetWpfTextView(viewAdapter); wpfViewHost = editorSvc.GetWpfTextViewHost(viewAdapter); System.Windows.FrameworkElement wpfElement = wpfViewHost?.HostControl; if (wpfElement != null) { // get real host? var parent = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement; while (parent != null) { wpfElement = parent; parent = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement; } editorControl.Content = wpfElement.ToEto(); return; } } // something went wrong editorControl.Content = new Scrollable { Content = new Label { Text = "Could not load editor" } }; }
public CodeEditorHost(IVsTextLines textBuffer) { ThreadHelper.ThrowIfNotOnUIThread(); var editorSvc = Services.GetComponentService <IVsEditorAdaptersFactoryService>(); codeWindow = editorSvc.CreateVsCodeWindowAdapter(Services.ServiceProvider); // disable splitter since it will cause a crash var codeWindowEx = (IVsCodeWindowEx)codeWindow; var initView = new INITVIEW[1]; ErrorHandler.ThrowOnFailure(codeWindowEx.Initialize( (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: string.Empty, szValueAuxUserContext: string.Empty, InitViewFlags: 0, pInitView: initView)); var buffer = editorSvc.GetDataBuffer(textBuffer); if (buffer == null) { ErrorHandler.ThrowOnFailure(VSConstants.VS_E_INCOMPATIBLEDOCDATA); } ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(textBuffer)); //needed for xeto/jeto files, not implemented for c# etc so we don't worry about the result //Guid clsIdView = VSConstants.LOGVIEWID.TextView_guid; //codeWindow.SetViewClassID(ref clsIdView); ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(out viewAdapter)); // get the view first so host is created //var wpfView = editorSvc.GetWpfTextView(viewAdapter); textViewHost = editorSvc.GetWpfTextViewHost(viewAdapter); wpfElement = textViewHost?.HostControl; if (wpfElement != null) { // get real host? var parent = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement; while (parent != null) { wpfElement = parent; parent = VisualTreeHelper.GetParent(wpfElement) as System.Windows.FrameworkElement; } } }
private IVsCodeWindow CreateVsCodeWindow(IVsTextLines documentBuffer) { // Create a code window adapter. var codeWindow = VisualStudioServices.VsEditorAdaptersFactoryService.CreateVsCodeWindowAdapter(_oleServiceProvider); // Disable the splitter control on the editor as leaving it enabled causes a crash if the user // tries to use it here :( var codeWindowEx = (IVsCodeWindowEx)codeWindow; var initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DEFAULT, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, "", "", 0, initView); //Associate our IVsTextLines with our new code window. ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(documentBuffer)); return(codeWindow); }
void CreateCodeEditor() { var editorSvc = Services.GetComponentService <IVsEditorAdaptersFactoryService>(); var codeWindow = editorSvc.CreateVsCodeWindowAdapter(Services.ServiceProvider); codeWindow.SetBuffer(textBuffer); Guid clsIdView = VSConstants.LOGVIEWID.TextView_guid; codeWindow.SetViewClassID(ref clsIdView); if (codeWindow.GetPrimaryView(out viewAdapter) == 0) { // disable splitter since it will cause a crash var codeWindowEx = (IVsCodeWindowEx)codeWindow; var initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: string.Empty, szValueAuxUserContext: string.Empty, InitViewFlags: 0, pInitView: initView); // get the view first so host is created var wpfView = editorSvc.GetWpfTextView(viewAdapter); var wpfViewHost = editorSvc.GetWpfTextViewHost(viewAdapter); var wpfElement = wpfViewHost?.HostControl; if (wpfElement != null) { editorControl.Content = wpfElement.ToEto(); return; } } // something went wrong editorControl.Content = new Scrollable { Content = new Label { Text = "Could not load editor" } }; }
private void InitializeEditor() { IComponentModel m_componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)); oleServiceProvider = (IOleServiceProvider)GetService(typeof(IOleServiceProvider)); bufferFactory = m_componentModel.GetService <ITextBufferFactoryService>(); m_EditorAdapterFactory = m_componentModel.GetService <IVsEditorAdaptersFactoryService>(); bufferAdapter = m_EditorAdapterFactory.CreateVsTextBufferAdapter(oleServiceProvider, bufferFactory.TextContentType); int result = bufferAdapter.InitializeContent("ed", 2); viewAdapter = m_EditorAdapterFactory.CreateVsTextViewAdapter(oleServiceProvider); ((IVsWindowPane)viewAdapter).SetSite(oleServiceProvider); INITVIEW[] _initView = new INITVIEW[] { new INITVIEW() }; _initView[0].fSelectionMargin = 0; _initView[0].fWidgetMargin = 0; _initView[0].fVirtualSpace = 0; _initView[0].fDragDropMove = 1; _initView[0].fVirtualSpace = 0; viewAdapter.Initialize(bufferAdapter as IVsTextLines, IntPtr.Zero, (uint)TextViewInitFlags.VIF_HSCROLL | (uint)TextViewInitFlags3.VIF_NO_HWND_SUPPORT, _initView); }
private void CreateVsCodeWindow() { int hr = VSConstants.S_OK; Guid clsidVsCodeWindow = typeof(VsCodeWindowClass).GUID; Guid iidVsCodeWindow = typeof(IVsCodeWindow).GUID; Guid clsidVsTextBuffer = typeof(VsTextBufferClass).GUID; Guid iidVsTextLines = typeof(IVsTextLines).GUID; // create/site a VsTextBuffer object _vsTextBuffer = (IVsTextBuffer)NuoDbVSPackagePackage.Instance.CreateInstance(ref clsidVsTextBuffer, ref iidVsTextLines, typeof(IVsTextBuffer)); IObjectWithSite ows = (IObjectWithSite)_vsTextBuffer; ows.SetSite(_editor); //string strSQL = "select * from sometable"; //hr = _vsTextBuffer.InitializeContent(strSQL, strSQL.Length); switch (NuoDbVSPackagePackage.Instance.GetMajorVStudioVersion()) { case 10: hr = _vsTextBuffer.SetLanguageServiceID(ref GuidList.guidSQLLangSvc_VS2010); break; case 11: hr = _vsTextBuffer.SetLanguageServiceID(ref GuidList.guidSQLLangSvc_VS2012); break; } // create/initialize/site a VsCodeWindow object _vsCodeWindow = (IVsCodeWindow)NuoDbVSPackagePackage.Instance.CreateInstance(ref clsidVsCodeWindow, ref iidVsCodeWindow, typeof(IVsCodeWindow)); INITVIEW[] initView = new INITVIEW[1]; initView[0].fSelectionMargin = 0; initView[0].fWidgetMargin = 0; initView[0].fVirtualSpace = 0; initView[0].fDragDropMove = 1; initView[0].fVirtualSpace = 0; IVsCodeWindowEx vsCodeWindowEx = (IVsCodeWindowEx)_vsCodeWindow; hr = vsCodeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR | (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, 0, null, null, (uint)TextViewInitFlags.VIF_SET_WIDGET_MARGIN | (uint)TextViewInitFlags.VIF_SET_SELECTION_MARGIN | (uint)TextViewInitFlags.VIF_SET_VIRTUAL_SPACE | (uint)TextViewInitFlags.VIF_SET_DRAGDROPMOVE | (uint)TextViewInitFlags2.VIF_SUPPRESS_STATUS_BAR_UPDATE | (uint)TextViewInitFlags2.VIF_SUPPRESSBORDER | (uint)TextViewInitFlags2.VIF_SUPPRESSTRACKCHANGES | (uint)TextViewInitFlags2.VIF_SUPPRESSTRACKGOBACK, initView); hr = _vsCodeWindow.SetBuffer((IVsTextLines)_vsTextBuffer); IVsWindowPane vsWindowPane = (IVsWindowPane)_vsCodeWindow; hr = vsWindowPane.SetSite(_editor); hr = vsWindowPane.CreatePaneWindow(this.Handle, 0, 0, this.Parent.Size.Width, this.Parent.Size.Height, out _hWndCodeWindow); IVsTextView vsTextView; hr = _vsCodeWindow.GetPrimaryView(out vsTextView); // sink IVsTextViewEvents, so we can determine when a VsCodeWindow object actually has the focus. IConnectionPointContainer connptCntr = (IConnectionPointContainer)vsTextView; Guid riid = typeof(IVsTextViewEvents).GUID; IConnectionPoint cp; connptCntr.FindConnectionPoint(ref riid, out cp); cp.Advise(_editor, out cookie); // sink IVsTextLinesEvents, so we can determine when a VsCodeWindow text has been changed. connptCntr = (IConnectionPointContainer)_vsTextBuffer; riid = typeof(IVsTextLinesEvents).GUID; connptCntr.FindConnectionPoint(ref riid, out cp); cp.Advise(_editor, out cookie); }
/// <summary> /// Used to create Code Window /// </summary> /// <param name="parentHandle">Parent window handle</param> /// <param name="place">The place.</param> /// <param name="allowModal">if set to <c>true</c> [allow modal].</param> /// <param name="forceLanguageService"></param> /// <param name="codeWindow">Represents a multiple-document interface (MDI) child that contains one or more code views.</param> private void CreateCodeWindow(IntPtr parentHandle, Rectangle place, bool allowModal, Guid? forceLanguageService, out IVsCodeWindow codeWindow) { codeWindow = CreateLocalInstance<IVsCodeWindow>(typeof(VsCodeWindowClass), _serviceProvider); // initialize code window INITVIEW[] initView = new INITVIEW[1]; initView[0].fSelectionMargin = 0; initView[0].IndentStyle = vsIndentStyle.vsIndentStyleSmart; initView[0].fWidgetMargin = 0; initView[0].fVirtualSpace = 0; initView[0].fDragDropMove = 1; initView[0].fVisibleWhitespace = 0; uint initViewFlags = (uint)TextViewInitFlags.VIF_SET_WIDGET_MARGIN | (uint)TextViewInitFlags.VIF_SET_SELECTION_MARGIN | (uint)TextViewInitFlags2.VIF_SUPPRESSBORDER | //(uint)TextViewInitFlags2.VIF_SUPPRESS_STATUS_BAR_UPDATE | (uint)TextViewInitFlags2.VIF_SUPPRESSTRACKCHANGES; if (allowModal) initViewFlags |= (uint)TextViewInitFlags2.VIF_ACTIVEINMODALSTATE; _codewindowbehaviorflags cwFlags = _codewindowbehaviorflags.CWB_DEFAULT; if (!EnableSplitter) cwFlags |= _codewindowbehaviorflags.CWB_DISABLESPLITTER; if (!EnableNavigationBar) cwFlags |= _codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR; IVsCodeWindowEx codeWindowEx = codeWindow as IVsCodeWindowEx; ErrorHandler.ThrowOnFailure(codeWindowEx.Initialize((uint)cwFlags, 0, null, null, initViewFlags, initView)); // set buffer _textBuffer = CreateLocalInstance<IVsTextBuffer>(typeof(VsTextBufferClass), _serviceProvider); _textBuffer.InitializeContent("", 0); if (forceLanguageService.HasValue) { Guid languageService = forceLanguageService.Value; SetLanguageServiceInternal(languageService); } ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer((IVsTextLines)_textBuffer)); // create pane window _windowPane = (IVsWindowPane)codeWindow; ErrorHandler.ThrowOnFailure(_windowPane.SetSite(_serviceProvider)); ErrorHandler.ThrowOnFailure(_windowPane.CreatePaneWindow(parentHandle, place.X, place.Y, place.Width, place.Height, out editorHwnd)); //set the inheritKeyBinding guid so that navigation keys work. The VS 2008 SDK does this from the language service. // The VS2005 sdk doesn't IOleServiceProvider sp = codeWindow as IOleServiceProvider; if (sp != null) { ServiceProvider site = new ServiceProvider(sp); IVsWindowFrame frame = site.GetService(typeof(IVsWindowFrame).GUID) as IVsWindowFrame; if (frame != null) { Guid CMDUIGUID_TextEditor = new Guid(0x8B382828, 0x6202, 0x11d1, 0x88, 0x70, 0x00, 0x00, 0xF8, 0x75, 0x79, 0xD2); ErrorHandler.ThrowOnFailure(frame.SetGuidProperty((int)__VSFPROPID.VSFPROPID_InheritKeyBindings, ref CMDUIGUID_TextEditor)); } } }
public int Initialize(uint grfCodeWindowBehaviorFlags, VSUSERCONTEXTATTRIBUTEUSAGE usageAuxUserContext, string szNameAuxUserContext, string szValueAuxUserContext, uint initViewFlags, INITVIEW[] pInitView) { var vsCodeWindow = (IVsCodeWindowEx) _vsCodeWindow; return vsCodeWindow.Initialize(grfCodeWindowBehaviorFlags, usageAuxUserContext, szNameAuxUserContext, szValueAuxUserContext, initViewFlags, pInitView); }
/// <summary> /// Method to call to cause us to place the file at the given path into our hosted editor. /// </summary> public Tuple<Control, IVsTextView> SetDisplayedFile(string filePath) { ClearEditor(); try { //Get an invisible editor over the file, this makes it much easier than having to manually figure out the right content type, //language service, and it will automatically associate the document with its owning project, meaning we will get intellisense //in our editor with no extra work. IVsInvisibleEditorManager invisibleEditorManager = (IVsInvisibleEditorManager)GetService(typeof(SVsInvisibleEditorManager)); ErrorHandler.ThrowOnFailure(invisibleEditorManager.RegisterInvisibleEditor(filePath, pProject: null, dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING, pFactory: null, ppEditor: out this.invisibleEditor)); //The doc data is the IVsTextLines that represents the in-memory version of the file we opened in our invisibe editor, we need //to extract that so that we can create our real (visible) editor. IntPtr docDataPointer = IntPtr.Zero; Guid guidIVSTextLines = typeof(IVsTextLines).GUID; ErrorHandler.ThrowOnFailure(this.invisibleEditor.GetDocData(fEnsureWritable: 1, riid: ref guidIVSTextLines, ppDocData: out docDataPointer)); try { IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer); //Get the component model so we can request the editor adapter factory which we can use to spin up an editor instance. IComponentModel componentModel = (IComponentModel)GetService(typeof(SComponentModel)); IVsEditorAdaptersFactoryService editorAdapterFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>(); //Create a code window adapter. this.codeWindow = editorAdapterFactoryService.CreateVsCodeWindowAdapter(OleServiceProvider); //Disable the splitter control on the editor as leaving it enabled causes a crash if the user //tries to use it here :( IVsCodeWindowEx codeWindowEx = (IVsCodeWindowEx)this.codeWindow; INITVIEW[] initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: "", szValueAuxUserContext: "", InitViewFlags: 0, pInitView: initView); //docData.SetStateFlags((uint)BUFFERSTATEFLAGS.BSF_USER_READONLY); //set read only //Associate our IVsTextLines with our new code window. ErrorHandler.ThrowOnFailure(this.codeWindow.SetBuffer((IVsTextLines)docData)); //Get our text view for our editor which we will use to get the WPF control that hosts said editor. ErrorHandler.ThrowOnFailure(this.codeWindow.GetPrimaryView(out this.textView)); //Get our WPF host from our text view (from our code window). IWpfTextViewHost textViewHost = editorAdapterFactoryService.GetWpfTextViewHost(this.textView); return Tuple.Create<Control, IVsTextView>(textViewHost.HostControl, this.textView); //Debug.Assert(contentControl != null); //contentControl.Content = textViewHost.HostControl; } finally { if (docDataPointer != IntPtr.Zero) { //Release the doc data from the invisible editor since it gave us a ref-counted copy. Marshal.Release(docDataPointer); } } } catch { } return null; }
private IVsCodeWindow CreateVsCodeWindow(IVsTextLines documentBuffer) { // Create a code window adapter. var codeWindow = VisualStudioServices.VsEditorAdaptersFactoryService.CreateVsCodeWindowAdapter(_oleServiceProvider); // Disable the splitter control on the editor as leaving it enabled causes a crash if the user // tries to use it here :( var codeWindowEx = (IVsCodeWindowEx)codeWindow; var initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DEFAULT, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, "", "", 0, initView); //Associate our IVsTextLines with our new code window. ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(documentBuffer)); return codeWindow; }
/// <summary> /// Method to call to cause us to place the file at the given path into our hosted editor. /// </summary> public void SetDisplayedFile(string filePath) { ClearOldEditor(); //Get an invisible editor over the file, this makes it much easier than having to manually figure out the right content type, //language service, and it will automatically associate the document with its owning project, meaning we will get intellisense //in our editor with no extra work. IVsInvisibleEditorManager invisibleEditorManager = (IVsInvisibleEditorManager)GetService(typeof(SVsInvisibleEditorManager)); ErrorHandler.ThrowOnFailure(invisibleEditorManager.RegisterInvisibleEditor(filePath, pProject: null, dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING, pFactory: null, ppEditor: out this.invisibleEditor)); //The doc data is the IVsTextLines that represents the in-memory version of the file we opened in our invisibe editor, we need //to extract that so that we can create our real (visible) editor. IntPtr docDataPointer = IntPtr.Zero; Guid guidIVSTextLines = typeof(IVsTextLines).GUID; ErrorHandler.ThrowOnFailure(this.invisibleEditor.GetDocData(fEnsureWritable: 1, riid: ref guidIVSTextLines, ppDocData: out docDataPointer)); try { IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer); //Get the component model so we can request the editor adapter factory which we can use to spin up an editor instance. IComponentModel componentModel = (IComponentModel)GetService(typeof(SComponentModel)); IVsEditorAdaptersFactoryService editorAdapterFactoryService = componentModel.GetService <IVsEditorAdaptersFactoryService>(); //Create a code window adapter. this.codeWindow = editorAdapterFactoryService.CreateVsCodeWindowAdapter(OleServiceProvider); //Disable the splitter control on the editor as leaving it enabled causes a crash if the user //tries to use it here :( IVsCodeWindowEx codeWindowEx = (IVsCodeWindowEx)this.codeWindow; INITVIEW[] initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: "", szValueAuxUserContext: "", InitViewFlags: 0, pInitView: initView); //Associate our IVsTextLines with our new code window. ErrorHandler.ThrowOnFailure(this.codeWindow.SetBuffer((IVsTextLines)docData)); //Get our text view for our editor which we will use to get the WPF control that hosts said editor. ErrorHandler.ThrowOnFailure(this.codeWindow.GetPrimaryView(out this.textView)); //Get our WPF host from our text view (from our code window). IWpfTextViewHost textViewHost = editorAdapterFactoryService.GetWpfTextViewHost(this.textView); Debug.Assert(this.control != null); //We already have an open window, so just insert this editor in place of the old one. this.control.InsertNewEditor(textViewHost.HostControl); } finally { if (docDataPointer != IntPtr.Zero) { //Release the doc data from the invisible editor since it gave us a ref-counted copy. Marshal.Release(docDataPointer); } } }
private void CreateVsCodeWindow() { int hr = VSConstants.S_OK; //ILocalRegistry localRegistry = (ILocalRegistry)Package.GetGlobalService(typeof(SLocalRegistry)); IComponentModel componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); Guid clsidVsCodeWindow = typeof(VsCodeWindowClass).GUID; Guid iidVsCodeWindow = typeof(IVsCodeWindow).GUID; Guid iidVsTextLines = typeof(IVsTextLines).GUID; Guid clsidTextBuffer = typeof(VsTextBufferClass).GUID; Guid iidTextBuffer = VSConstants.IID_IUnknown; // create/site a VsTextBuffer object var editorAdaptersFactoryService = componentModel.GetService<IVsEditorAdaptersFactoryService>(); vsTextBuffer = editorAdaptersFactoryService.CreateVsTextBufferAdapter(OleServiceProvider); string strSQL = Global.textStub; hr = vsTextBuffer.InitializeContent(strSQL, strSQL.Length); hr = vsTextBuffer.SetLanguageServiceID(ref GuidList.guidCppLangSvc); // create/initialize/site a VsCodeWindow object _vsCodeWindow = editorAdaptersFactoryService.CreateVsCodeWindowAdapter(OleServiceProvider); INITVIEW[] initView = new INITVIEW[1]; initView[0].fSelectionMargin = 0; initView[0].fWidgetMargin = 0; initView[0].fVirtualSpace = 0; initView[0].fDragDropMove = 1; initView[0].fVirtualSpace = 0; IVsCodeWindowEx vsCodeWindowEx = (IVsCodeWindowEx)_vsCodeWindow; hr = vsCodeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR | (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER, 0, null, null, (uint)TextViewInitFlags.VIF_SET_WIDGET_MARGIN | (uint)TextViewInitFlags.VIF_SET_SELECTION_MARGIN | (uint)TextViewInitFlags.VIF_SET_VIRTUAL_SPACE | (uint)TextViewInitFlags.VIF_SET_DRAGDROPMOVE | (uint)TextViewInitFlags2.VIF_SUPPRESS_STATUS_BAR_UPDATE | (uint)TextViewInitFlags2.VIF_READONLY | (uint)TextViewInitFlags2.VIF_SUPPRESSBORDER | (uint)TextViewInitFlags2.VIF_SUPPRESSTRACKCHANGES | (uint)TextViewInitFlags2.VIF_SUPPRESSTRACKGOBACK, initView); hr = _vsCodeWindow.SetBuffer((IVsTextLines)vsTextBuffer); IVsTextView vsTextView; hr = _vsCodeWindow.GetPrimaryView(out vsTextView); textViewHost = editorAdaptersFactoryService.GetWpfTextViewHost(vsTextView); textViewHost.TextView.Options.SetOptionValue(DefaultTextViewHostOptions.ChangeTrackingId, false); textViewHost.TextView.Options.SetOptionValue(DefaultTextViewHostOptions.GlyphMarginId, false); textViewHost.TextView.Options.SetOptionValue(DefaultTextViewHostOptions.HorizontalScrollBarId, false); textViewHost.TextView.Options.SetOptionValue(DefaultTextViewHostOptions.OutliningMarginId, false); textViewHost.TextView.Options.SetOptionValue(DefaultTextViewOptions.ViewProhibitUserInputId, true); Content = textViewHost.HostControl; }
private void CreateEditor(string filePath) { //IVsInvisibleEditors are in-memory represenations of typical Visual Studio editors. //Language services, highlighting and error squiggles are hooked up to these editors //for us once we convert them to WpfTextViews. IVsInvisibleEditor invisibleEditor; ErrorHandler.ThrowOnFailure(_invisibleEditorManager.RegisterInvisibleEditor( filePath , pProject: null , dwFlags: (uint)_EDITORREGFLAGS.RIEF_ENABLECACHING , pFactory: null , ppEditor: out invisibleEditor)); //Then when creating the IVsInvisibleEditor, find and lock the document IntPtr docData; IVsHierarchy hierarchy; var runningDocTable = (IVsRunningDocumentTable)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsRunningDocumentTable)); uint docCookie; ErrorHandler.ThrowOnFailure(runningDocTable.FindAndLockDocument( dwRDTLockType: (uint)_VSRDTFLAGS.RDT_ReadLock, pszMkDocument: filePath, ppHier: out hierarchy, pitemid: out uint itemId, ppunkDocData: out docData, pdwCookie: out docCookie)); IntPtr docDataPointer; var guidIVsTextLines = typeof(IVsTextLines).GUID; ErrorHandler.ThrowOnFailure(invisibleEditor.GetDocData( fEnsureWritable: 1 , riid: ref guidIVsTextLines , ppDocData: out docDataPointer)); _docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer); //Make Buffer Readonly _docData.GetStateFlags(out uint oldFlags); _docData.SetStateFlags(oldFlags | (uint)BUFFERSTATEFLAGS.BSF_USER_READONLY); //Create a code window adapter _codeWindow = _editorAdapter.CreateVsCodeWindowAdapter(ProfilerPlugin.Instance.OLEServiceProvider); //Disable the splitter control on the editor as leaving it enabled causes a crash if the user //tries to use it here :( IVsCodeWindowEx codeWindowEx = (IVsCodeWindowEx)_codeWindow; INITVIEW[] initView = new INITVIEW[1]; codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER,// | ((uint)TextViewInitFlags2.VIF_READONLY), VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, szNameAuxUserContext: "", szValueAuxUserContext: "", InitViewFlags: 0, pInitView: initView); ErrorHandler.ThrowOnFailure(_codeWindow.SetBuffer(_docData)); //Get a text view for our editor which we will then use to get the WPF control for that editor. ErrorHandler.ThrowOnFailure(_codeWindow.GetPrimaryView(out _textView)); _textViewHost = _editorAdapter.GetWpfTextViewHost(_textView); }
public int Initialize(IVsTextLines pBuffer, IntPtr hwndParent, uint InitFlags, INITVIEW[] pInitView) { return VSConstants.S_OK; }
int IVsTextView.Initialize(IVsTextLines pBuffer, IntPtr hwndParent, uint InitFlags, INITVIEW[] pInitView ) { return VSConstants.E_NOTIMPL; }
public int Initialize(IVsTextLines pBuffer, IntPtr hwndParent, uint InitFlags, INITVIEW[] pInitView) { throw new Exception("The method or operation is not implemented."); }
int IVsTextView.Initialize(IVsTextLines pBuffer, System.IntPtr hwndParent, uint InitFlags, INITVIEW[] pInitView) { throw new System.NotImplementedException(); }