locked
Code window is not getting displayed in VS2010 RRS feed

  • Question

  • I am using the LanguageService example given in VS2005 SDK in VS2010. According to the article at http://msdn.microsoft.com/en-us/library/ee805910(VS.100).aspx. Using IVsWindowPane.createWindowPane() should be working correctly but in my case it is not setting the values for IWpfTextViewHost, WpfTextView as when I look for their values in debug mode, I get following message. 

    '((Microsoft.VisualStudio.Editor.Implementation.VsTextViewAdapter)(tv)).WpfTextView' threw an exception of type 'System.InvalidOperationException'

    I wanted to know whether I am missing anything in the code. Following are the two main methods used for Code Window creation. I know the method of using IVsUIElementPane but I want to use IVsWindowPane only. Any help would be appreciated.

     

       private void CreateCodeWindow(IntPtr parentHandle, out IVsCodeWindow codeWindow)

            {

                ILocalRegistry localRegistry = QueryService<ILocalRegistry>(typeof(SLocalRegistry));

                // set buffer

                Guid guidVsTextBuffer = typeof(VsTextBufferClass).GUID;

                IVsTextBuffer textBuffer = CreateObject(localRegistry, guidVsTextBuffer, typeof(IVsTextBuffer).GUID) as IVsTextBuffer;

                Guid CLSID_VisualBasicLangService = new Guid("{E34ACDC0-BAAE-11D0-88BF-00A0C9110049}");

     

                int hr = textBuffer.SetLanguageServiceID(ref CLSID_VisualBasicLangService);

                if (hr != VSConstants.S_OK)

                {

                  Marshal.ThrowExceptionForHR(hr);

                }

     

                // create code window

                Guid guidVsCodeWindow = typeof(VsCodeWindowClass).GUID;

                codeWindow = (IVsCodeWindow)CreateObject(localRegistry, guidVsCodeWindow, typeof(IVsCodeWindow).GUID);

     

                // 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;

     

                IVsCodeWindowEx codeWindowEx = codeWindow as IVsCodeWindowEx;

                 hr = codeWindowEx.Initialize((uint)_codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR |

                    (uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER,

                    0, null, null,

                    (int)TextViewInitFlags.VIF_SET_WIDGET_MARGIN |

                    (int)TextViewInitFlags.VIF_SET_SELECTION_MARGIN |

                    (int)TextViewInitFlags2.VIF_ACTIVEINMODALSTATE |

                    (int)TextViewInitFlags2.VIF_SUPPRESSBORDER |

                    (int)TextViewInitFlags2.VIF_SUPPRESS_STATUS_BAR_UPDATE |

                    (int)TextViewInitFlags2.VIF_SUPPRESSTRACKCHANGES,

                    initView);

     

                if (hr != VSConstants.S_OK)

                {

                    Marshal.ThrowExceptionForHR(hr);

                }

     

     

     

                hr = codeWindow.SetBuffer(textBuffer as IVsTextLines);

                if (hr != VSConstants.S_OK)

                {

                    Marshal.ThrowExceptionForHR(hr);

                }

     

                // create pane window

                IVsWindowPane windowPane = codeWindow as IVsWindowPane;

     

                hr = windowPane.SetSite(serviceProvider);

                if (hr != VSConstants.S_OK)

                {

                    Marshal.ThrowExceptionForHR(hr);

                }

     

                hr = windowPane.CreatePaneWindow(parentHandle, 10, 10, 50, 50, out editorHwnd);

     

                if (hr != VSConstants.S_OK)

                {

                    Marshal.ThrowExceptionForHR(hr);

                }

                IVsTextView tv;

                codeWindow.GetPrimaryView(out tv);

     

            }

     

     

            private object CreateObject(ILocalRegistry localRegistry, Guid clsid, Guid iid)

            {

     

     

              /*

              IVsEditorAdaptersFactoryService editorAdapterService;

              IComponentModel componentModel = (IComponentModel)GetService(typeof(SComponentModel));

              editorAdapterService = componentModel.GetService<IVsEditorAdaptersFactoryService>();

              IVsTextBuffer textBuffer = adaptersFactory.CreateVsTextBufferAdapter(serviceProvider);

              textBuffer.InitializeContent("text", 4);

              IVsCodeWindow codeWindow = adaptersFactory.CreateVsCodeWindowAdapter(serviceProvider);

              codeWindow.SetBuffer(textBuffer as IVsTextLines);

     

              return codeWindow;

     

              */

              object objectInstance;

                IntPtr unknown = IntPtr.Zero;

     

                int hr = localRegistry.CreateInstance(clsid, null, ref iid, (uint)CLSCTX.CLSCTX_INPROC_SERVER, out unknown);

     

                if (hr != VSConstants.S_OK)

                {

                    Marshal.ThrowExceptionForHR(hr);

                }

     

                try

                {

                    objectInstance = Marshal.GetObjectForIUnknown(unknown);

                }

                finally

                {

                    if (unknown != IntPtr.Zero)

                    {

                        Marshal.Release(unknown);

                    }

                }            

     

                // Try to site object instance

                IObjectWithSite objectWithSite = objectInstance as IObjectWithSite;

                if (objectWithSite != null)

                    objectWithSite.SetSite(serviceProvider);

     

                return objectInstance;

            }


     


    Prasad Gawde
    • Moved by CoolDadTx Thursday, February 24, 2011 3:30 PM Extensibility related (From:Visual C# IDE)
    Thursday, February 24, 2011 1:24 PM

Answers

  • From a quick look, the one thing I see missing is a call to textBuffer.InitializeContent (or textBuffer.Load).  I think in VS2005, we were more lenient about letting people omit that, but in 2010, it's necessary.  I'd add it before the call to SetLanguageServiceID and see if that helps.

     

    • Marked as answer by Prasad Gawde Friday, February 25, 2011 5:15 AM
    Thursday, February 24, 2011 9:01 PM

All replies

  • I need not say this but the same code works perfectly with VS2005
    Prasad Gawde
    Thursday, February 24, 2011 1:26 PM
  • There is a lot of change from 2005 -> 2010, so something that worked in 2005 isn't guaranteed to work in 2010, though it seems this should.  Is it only under the debugger that you see these issues?  The InvalidOperationException is interesting, are you seeing this in code when you try to get the WPF text view or are you seeing this say in the watch window?

    Ryan

    Thursday, February 24, 2011 4:11 PM
  • When I run this code. the application runs but no code window is created. I am using a UserControl to host the code window so the area is left blank without code window. I found out the error by using Quick watch. 

    Any clue to get around this problem???


    Prasad Gawde
    Thursday, February 24, 2011 5:34 PM
  • >'((Microsoft.VisualStudio.Editor.Implementation.VsTextViewAdapter)(tv)).WpfTextView' threw an exception of type 'System.InvalidOperationException'

    Well, that is not a public type, so it isn't shocking it may not work 'properly' under the debugger, or more specifically it isn't intended to ever be accessed at the point in its lifetime you are attempting to access it.  

    Looking at why it would throw InvalidOperationException it looks like it happens if you call into that property when the adaptor is in an uninitialized state. The way it gets properly initialized looks really complex (I am not an editor dev), but it MAY be sufficient to not access that property until AFTER you have called SetBuffer on the adapter object.

    I will point an editor dev or two at this thread as it is really more editor related, there is an editor forum where this question would likely get a faster response, I will see about getting this moved over there as well.

    Ryan

    Thursday, February 24, 2011 8:01 PM
  • From a quick look, the one thing I see missing is a call to textBuffer.InitializeContent (or textBuffer.Load).  I think in VS2005, we were more lenient about letting people omit that, but in 2010, it's necessary.  I'd add it before the call to SetLanguageServiceID and see if that helps.

     

    • Marked as answer by Prasad Gawde Friday, February 25, 2011 5:15 AM
    Thursday, February 24, 2011 9:01 PM
  • Thanks Ryan and Michael,

    the issue is resolved by Michael's suggestion.


    Prasad Gawde
    Friday, February 25, 2011 5:15 AM