Udostępnij za pośrednictwem


Editor Factories

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

An editor factory creates editor objects and puts them in a window frame, known as a physical view. It creates the document data and document view objects that are necessary to create editors and designers. An editor factory is required to create the Visual Studio core editor and any standard editor. A custom editor can also optionally be created with an editor factory.

You create an editor factory by implementing the IVsEditorFactory interface. The following example illustrates how to implement IVsEditorFactory to create an editor factory:

[Guid(GuidList.guidEditorFactory)]
public sealed class SingleViewEditorFactory : IVsEditorFactory, IDisposable
{
    private PackageSingleViewEditor MyPackage;
    private ServiceProvider vsServiceProvider;

    public SingleViewEditorFactory(PackageSingleViewEditor packageEditor)
    {
        Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, 
            "Entering {0} constructor", this.ToString()));
        MyPackage = packageEditor;
    }

    #region "IVsEditorFactory Support"
        public int Close()
        {
            throw new NotImplementedException();
        }

        public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out System.Guid pguidCmdUI, out int pgrfCDW)
        {
            throw new NotImplementedException();
        }

        public int MapLogicalView(ref System.Guid rguidLogicalView, out string pbstrPhysicalView)
        {
            throw new NotImplementedException();
        }

        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            throw new NotImplementedException();
        }
    #endregion

    #region "IDisposable Support"
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    #endregion
}
<Guid(GuidList.guidEditorFactory)> _
Public NotInheritable Class SingleViewEditorFactory
    Implements IVsEditorFactory
    Implements IDisposable

    Private MyPackage As PackageSingleViewEditor
    Private vsServiceProvider As ServiceProvider

    Public Sub New(ByVal packageEditor As PackageSingleViewEditor)
        Trace.WriteLine(String.Format(CultureInfo.CurrentCulture,
          "Entering {0} constructor", Me.ToString()))
        MyPackage = packageEditor
    End Sub

#Region "IVsEditorFactorySupport"
    Public Function Close() As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.Close
        Throw New NotImplementedException
    End Function

    Public Function CreateEditorInstance(ByVal grfCreateDoc As UInteger, ByVal pszMkDocument As String, ByVal pszPhysicalView As String, ByVal pvHier As VisualStudio.Shell.Interop.IVsHierarchy, ByVal itemid As UInteger, ByVal punkDocDataExisting As System.IntPtr, ByRef ppunkDocView As System.IntPtr, ByRef ppunkDocData As System.IntPtr, ByRef pbstrEditorCaption As String, ByRef pguidCmdUI As System.Guid, ByRef pgrfCDW As Integer) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.CreateEditorInstance
        Throw New NotImplementedException
    End Function

    Public Function MapLogicalView(ByRef rguidLogicalView As System.Guid, ByRef pbstrPhysicalView As String) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.MapLogicalView
        Throw New NotImplementedException
    End Function

    Public Function SetSite(ByVal psp As VisualStudio.OLE.Interop.IServiceProvider) As Integer Implements VisualStudio.Shell.Interop.IVsEditorFactory.SetSite
        Throw New NotImplementedException
    End Function
#End Region

#Region "IDisposable Support"
    Public Sub Dispose() Implements IDisposable.Dispose
        Throw New NotImplementedException
    End Sub
#End Region

End Class

An editor is loaded the first time that you open a file type handled by that editor. You can choose to open either a specific editor or the default editor. If you select the default editor, the integrated development environment (IDE) determines the correct editor to open and then opens it. For more information, see Determining Which Editor Opens a File in a Project.

Registering Editor Factories

Before you can use an editor that you have created, you first must register information about it, including the file extensions it can handle.

If your VSPackage is written in managed code, you can use the Managed Package Framework (MPF) method RegisterEditorFactory to register the editor factory after your VSPackage is loaded. If your VSPackage is written in unmanaged code, then you must register your editor factory by using the SVsRegisterEditors service.

Registering an Editor Factory by Using Managed Code

You must register your editor factory in your VSPackage's the Initialize method. First call base.Initialize, and then call RegisterEditorFactory for each editor factory

In managed code, there is no need to unregister an editor factory, because the VSPackage will handle this for you. Also, if your editor factory implements IDisposable, it is automatically disposed when it is unregistered.

Registering an editor factory by using unmanaged code

In the SetSite implementation for your editor package, use the QueryService method to call SVsRegisterEditors. Doing this returns a pointer to IVsRegisterEditors. Call the RegisterEditor method by passing your implementation of the IVsEditorFactory interface. You must mplement IVsEditorFactory in a separate class.

The Editor Factory Registration Process

The following process occurs when Visual Studio loads your editor using your editor factory:

  1. The Visual Studio project system calls OpenStandardEditor.

  2. This method returns the editor factory. Visual Studio delays loading the editor's package, however, until a project system actually needs the editor.

  3. When a project system needs the editor, Visual Studio calls CreateEditorInstance, a specialized method that returns both the document view and the document data objects.

  4. If calls by Visual Studio to your editor factory using CreateEditorInstance return both a document data object and a document view object, Visual Studio then creates the document window, places the document view object in it, and makes an entry into the running document table (RDT) for the document data object.

See Also

IVsEditorFactory
Running Document Table