Identifying the editor factory used to create the currently active document RRS feed

  • Question

  • Hi all,
    does anyone know how to find the Guid for the factory responsible for creating the currently active document (I want it to identify the currently active keyboard scope)?

    Any ideas much appreciated - (i have been trying to solve this for over a week now)
    Shane Norris
    Wednesday, March 25, 2009 7:06 PM


  • Hi Shane,

    That error code correlates to DISP_E_MEMBERNOTFOUND. The reason for this is that the GetProperty call doesn't support this particular property. You need to use IVsWindowFrame.GetGuidProperty instead.

    Ed Dore
    Thursday, March 26, 2009 10:02 PM

All replies

  • How to get the current editor in VS?
    Return to the source, learn the way!
    • Proposed as answer by FengChen Thursday, March 26, 2009 1:41 AM
    Thursday, March 26, 2009 1:41 AM
  • Thanks Feng, It looked like it should have been the answer but no go I'm afraid.

    In the following test code when a C# source file window is currently selected it does not get past line 13. and with no edit window
    open it exits at 9.
    Any further ideas?

    1        private void MenuItemCallback(object sender, EventArgs e) 
    2        { 
    3            IVsMonitorSelection ms = (IVsMonitorSelection)GetService(typeof(SVsShellMonitorSelection)); 
    4            object val; 
    5            ms.GetCurrentElementValue( 
    6                (uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out val); 
    7            IVsWindowFrame frame = val as IVsWindowFrame; 
    8            if (frame == null
    9                return
    10            val = null
    11            frame.GetProperty((int)__VSFPROPID.VSFPROPID_guidEditorType, out val);
    12            if (val == null
    13                return
    14            System.Windows.Forms.MessageBox.Show(string.Format("..guidEditorType is {0}", (Guid)val)); 
    15        } 

    Shane Norris,
    Thursday, March 26, 2009 3:52 AM
  • Also I tested the return from GetProperty(). The docs state
    "If the method succeeds, it returns S_OK. If it fails, it returns an error code."
    But there is nothing more about the possible error codes (value is -2147352573)

    Shane Norris
    Thursday, March 26, 2009 3:59 AM
  • Could you please provide enough information to reproduce your issue?
    How to ask questions to increase your chances of an answer
    Return to the source, learn the way!
    Thursday, March 26, 2009 5:28 AM
  • Sorry Feng..

     1. generate a C# VSPackage using the project wizard and include support for a menu command.

     2. replace the menu callback with the code below and insert #using directives for Microsoft.VisualStudio.Shell and Microsoft.VisualStudio if necessary.

    3. run project in debug mode and set breakpoints on line 16 and 20.

    Depending on if you have opened an editor window or not the menu callback will either exit at line 16 if the editor window is not selected or 20 if the editor is selected when you run the menu command.

    When the break point on 20 is reached you can see by inspection that the result variable holds some
    unknown error condition.

    Thanks, for taking the time to help
    Shane Norris

    1        //using Microsoft.VisualStudio.Shell; -- these additional usings are required at top of file 
    2        //using Microsoft.VisualStudio; 
    4        /// <summary> 
    5        /// This method should be directly interchangable for the boilerplate callback generated 
    6        /// when running the VSPackage wizard and choosing to include a menu command. 
    7        /// </summary> 
    8        private void MenuItemCallback(object sender, EventArgs e) 
    9        { 
    10            IVsMonitorSelection ms = (IVsMonitorSelection)GetService(typeof(SVsShellMonitorSelection)); 
    11            object val; 
    12            ms.GetCurrentElementValue( 
    13                (uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out val); 
    14            IVsWindowFrame frame = val as IVsWindowFrame; 
    15            if (frame == null
    16                return
    17            val = null
    18            int result = frame.GetProperty((int)__VSFPROPID.VSFPROPID_guidEditorType, out val); 
    19            if (result != VSConstants.S_OK || val == null
    20                return
    21            System.Windows.Forms.MessageBox.Show(string.Format("..guidEditorType is {0}", (Guid)val)); 
    22        } 

    Thursday, March 26, 2009 6:45 AM
  • Hi Shane,

    That error code correlates to DISP_E_MEMBERNOTFOUND. The reason for this is that the GetProperty call doesn't support this particular property. You need to use IVsWindowFrame.GetGuidProperty instead.

    Ed Dore
    Thursday, March 26, 2009 10:02 PM
  • Cool, that fixed it thanks Ed!

    Turns out the editor factory of the current document does not allways equate to the scope in all cases anyhow though (only for the Text & CSS Editors as well as those created with the app wizard) so it's back to the drawing board - but at least that code works. 

    PS Ed, where bouts did you look up the numeric value for DISP_E_MEMBERNOTFOUND?

    Thanks again
    Shane Norris
    Friday, March 27, 2009 10:18 AM
  • WinError.h and it's also defined by the VSConstants.DISP_E_MEMBERNOTFOUND field in the MPF's Microsoft.VisualStudio.Shell.dll assembly.

    Now that I think about it, there can be any number of editors associated with a particular file extension. Thats why they are registered with a priority flag. And from the editor factory implemenations I've seen, I never see an editor factory passing an instance of itself off to the editor it creates.

    Not sure I completely understand what you are attempting to do with respect to identifying active keyboard scope. Generally speaking, the editor has nothing to do with keyboard messages. These are aways routed by the IDE to the editor/toolwindow. Can you elaborate a bit on what you are trying to do?

    Ed Dore
    Saturday, March 28, 2009 6:35 PM
  • Hi Ed,
     I should have remembered WinError.h (duh!).

    The reason I'm after the scope is to display the currently available shortcut keys to the user. As it stands I have a toolwindow that lists the shortcuts sorted into the scopes they are applied in and filtered by whether or not they are active.

    It currently resonds to changes in the focus window to refresh the active shortcuts - but this list still includes a bunch of keys from all the other keyboard scopes outside the active one which is to many to displaye to the user (the objective is to have a shortlist in a similar fasion to the dynamic help window). Whats needed is the currently active keyboard scope so I can hide the uninteresting shortcuts (it's all in a treeview the user can find them in anyhow the branches will just be closed).

    The reason I had been looking at the editor factory for the solution is that when you create a custom editor with the project wizard VisibilityConstraints in the VSCT file uses the factory guid for the context attribute.

    But as we have seen thats not the answer and on further thought afterwards I should have realised this wouldn't work since you have things like the HTML Editor Design and HTML Editor Source Views which each have there own keyboard scope but I assume would be from the same factory.

    Anyhow the projects been pushed onto the stack at the moment while I work on other things (90% complete but without that missing ingredient its not really usable).

    If you have any ideas it would be great to have a fresh take on things,
    thanks again for solving the initial question.
    Shane Norris
    Sunday, March 29, 2009 3:21 PM