다음을 통해 공유


연습: 코어 편집기 만들기 및 등록을 편집기 파일 형식

이 연습을 시작 하는 Vspackage를 만드는 방법을 보여 줍니다 있는 Visual Studio 코어 편집기.myext 파일 이름 확장명을 가진 파일 때 로드 된입니다.

사전 요구 사항

이 연습을 완료 하려면 설치 해야 해당 Visual Studio 2010 SDK.

참고

Visual Studio SDK에 대 한 자세한 내용은 참조 하십시오. Visual Studio 개요를 확장합니다..Visual Studio SDK를 다운로드 하는 방법를 참조 하십시오. Visual Studio 확장성 개발자 센터 MSDN 웹 사이트에서.

Visual Studio 패키지 프로젝트 템플릿에 대해 위치

세 가지 다른 위치에서 찾을 수 있습니다 Visual Studio 패키지 프로젝트 서식 파일은 새 프로젝트 대화 상자:

  1. 아래에서 Visual Basic 확장성. 프로젝트의 기본 언어는 Visual Basic입니다.

  2. C#에서 확장 합니다. 프로젝트의 기본 언어는 C#입니다.

  3. 아래에서 기타 프로젝트 형식 확장성. 프로젝트의 기본 언어는 c + +입니다.

있는 Vspackage를 만들려면

편집기 팩터리를 추가 하려면

  1. 마우스는 MyPackage 프로젝트 하 고 추가 하 고 다음을 클릭 클래스.

  2. 새 항목 추가 대화 상자에서 있는지 확인 하십시오는 클래스 서식 파일을 선택한 경우 형식 EditorFactory.cs 이름과 클릭에 대 한 추가 클래스를 프로젝트에 추가 합니다.

    EditorFactory.cs 파일을 자동으로 열 수 있습니다.

  3. 코드에서 다음 어셈블리를 참조 합니다.

    Imports System.Runtime.InteropServices
    Imports Microsoft.VisualStudio
    Imports Microsoft.VisualStudio.Shell
    Imports Microsoft.VisualStudio.Shell.Interop
    Imports Microsoft.VisualStudio.OLE.Interop
    Imports Microsoft.VisualStudio.TextManager.Interop
    Imports IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider
    
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.TextManager.Interop;
    using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
    
  4. GUID를 추가 EditorFactory 추가 하 여 클래스의 Guid 특성 클래스 선언 바로 앞.

    Guidgen.exe 프로그램을 사용 하 여 새 GUID를 생성할 수 있습니다는 Visual Studio 명령 프롬프트에서 또는 클릭 하 여 GUID 만들기 에 있는 도구 메뉴. 여기서 사용 되는 GUID는 하나의 예입니다. 프로젝트에는 사용 하지 않습니다.

        <Guid("0eea3187-c5fa-48d4-aa72-b5eecd3b17b1")> _
    
        [Guid("0eea3187-c5fa-48d4-aa72-b5eecd3b17b1")] 
    
  5. 클래스 정의에 부모 패키지 및 서비스 공급자를 포함 하는 두 개의 private 변수를 추가 합니다.

    Class EditorFactory
        Private parentPackage As Package
        Private serviceProvider As IOleServiceProvider
    
    class EditorFactory
    {
        private Package parentPackage;
        private IOleServiceProvider serviceProvider;
    }
    
  6. 형식 매개 변수를 사용 하는 공용 클래스 생성자를 추가 합니다. Package:

    Public Sub New(ByVal parentPackage As Package)
        Me.parentPackage = parentPackage
    End Sub
    
    public EditorFactory(Package parentPackage)
    {
        this.parentPackage = parentPackage;
    }
    
  7. 수정는 EditorFactory 클래스에서 파생 하는 선언에 IVsEditorFactory 인터페이스입니다.

    Class EditorFactory Implements IVsEditorFacto
    
    class EditorFactory : IVsEditorFactory
    
  8. 마우스 오른쪽 단추로 IVsEditorFactory, 클릭 인터페이스 구현, 다음을 클릭 하 고 명시적 인터페이스 구현.

    이를 구현 해야 하는 네 가지 메서드 추가 IVsEditorFactory 인터페이스입니다.

  9. 내용을 대체는 IVsEditorFactory.Close 메서드에 다음 코드를 사용 합니다.

    Return VSConstants.S_OK
    
    return VSConstants.S_OK;
    
  10. 내용을 대체는 IVsEditorFactory.SetSite 다음 코드를 사용 합니다.

    Me.serviceProvider = psp
    Return VSConstants.S_OK
    
    this.serviceProvider = psp;
    return VSConstants.S_OK;
    
  11. 내용을 대체는 IVsEditorFactory.MapLogicalView 메서드에 다음 코드를 사용 합니다.

    Dim retval As Integer = VSConstants.E_NOTIMPL
    pbstrPhysicalView = Nothing ' We support only one view.
    If rguidLogicalView.Equals(VSConstants.LOGVIEWID_Designer)OrElse _
    rguidLogicalView.Equals(VSConstants.LOGVIEWID_Primary) Then
        retval = VSConstants.S_OK
    End If
    Return retval
    
    int retval = VSConstants.E_NOTIMPL;
    pbstrPhysicalView = null;   // We support only one view.
    if (rguidLogicalView.Equals(VSConstants.LOGVIEWID_Designer) ||
    rguidLogicalView.Equals(VSConstants.LOGVIEWID_Primary))
    {
        retval = VSConstants.S_OK;
    }
    return retval;
    
  12. 내용을 대체는 IVsEditorFactory.CreateEditorInstance 메서드에 다음 코드를 사용 합니다.

    Dim retval As Integer = VSConstants.E_FAIL        
    
    ' Initialize these to empty to start with 
    ppunkDocView = IntPtr.Zero
    ppunkDocData = IntPtr.Zero
    pbstrEditorCaption = ""
    pguidCmdUI = Guid.Empty
    pgrfCDW = 0
    
    If (grfCreateDoc And (VSConstants.CEF_OPENFILE Or _
    VSConstants.CEF_SILENT)) = 0 Then
        Throw New ArgumentException("Only Open or Silent is valid")
    End If
    If punkDocDataExisting <> IntPtr.Zero Then
        Return VSConstants.VS_E_INCOMPATIBLEDOCDATA
    End If
    
    ' Instantiate a text buffer of type VsTextBuffer. 
    ' Note: we only need an IUnknown (object) interface for 
    ' this invocation. 
    Dim clsidTextBuffer As Guid = GetType(VsTextBufferClass).GUID
    Dim iidTextBuffer As Guid = VSConstants.IID_IUnknown
    Dim pTextBuffer As Object = pTextBuffer = _
    parentPackage.CreateInstance(clsidTextBuffer, iidTextBuffer, _
    GetType(Object))
    
    If Not pTextBuffer Is Nothing Then
        ' "Site" the text buffer with the service provider we were 
        ' provided. 
        Dim textBufferSite As IObjectWithSite = TryCast(pTextBuffer, _
        IObjectWithSite)
        If Not textBufferSite Is Nothing Then
            textBufferSite.SetSite(Me.serviceProvider)
        End If
    
        ' Instantiate a code window of type IVsCodeWindow. 
        Dim clsidCodeWindow As Guid = GetType(VsCodeWindowClass).GUID
        Dim iidCodeWindow As Guid = GetType(IVsCodeWindow).GUID
        Dim pCodeWindow As IVsCodeWindow = _
        CType(Me.parentPackage.CreateInstance(clsidCodeWindow, _
        iidCodeWindow, GetType(IVsCodeWindow)), IVsCodeWindow)
        If Not pCodeWindow Is Nothing Then
            ' Give the text buffer to the code window. 
            ' We are giving up ownership of the text buffer! 
            pCodeWindow.SetBuffer(CType(pTextBuffer, IVsTextLines))
    
            ' Now tell the caller about all this new stuff 
            ' that has been created. 
            ppunkDocView = Marshal.GetIUnknownForObject(pCodeWindow)
            ppunkDocData = Marshal.GetIUnknownForObject(pTextBuffer)
    
            ' Specify the command UI to use so keypresses are 
            ' automatically dealt with. 
            pguidCmdUI = VSConstants.GUID_TextEditorFactory
    
            ' This caption is appended to the filename and 
            ' lets us know our invocation of the core editor 
            ' is up and running. 
            pbstrEditorCaption = " [MyPackage]"
    
            retval = VSConstants.S_OK
        End If
    End If
    Return retval
    
    int retval = VSConstants.E_FAIL;
    
    // Initialize these to empty to start with
    ppunkDocView       = IntPtr.Zero;
    ppunkDocData       = IntPtr.Zero;
    pbstrEditorCaption = "";
    pguidCmdUI         = Guid.Empty; 
    pgrfCDW            = 0;
    
    if ((grfCreateDoc & (VSConstants.CEF_OPENFILE | 
          VSConstants.CEF_SILENT)) == 0)
    { 
        throw new ArgumentException("Only Open or Silent is valid");
    }
    if (punkDocDataExisting != IntPtr.Zero)
    {
        return VSConstants.VS_E_INCOMPATIBLEDOCDATA;
    }
    
    // Instantiate a text buffer of type VsTextBuffer.
    // Note: we only need an IUnknown (object) interface for 
    // this invocation.
    Guid clsidTextBuffer = typeof(VsTextBufferClass).GUID;
    Guid iidTextBuffer   = VSConstants.IID_IUnknown;
    object pTextBuffer   = pTextBuffer = parentPackage.CreateInstance(
          ref clsidTextBuffer,
          ref iidTextBuffer,
          typeof(object));
    
    if (pTextBuffer != null)
    {
        // "Site" the text buffer with the service provider we were
        // provided.
        IObjectWithSite textBufferSite = pTextBuffer as IObjectWithSite;
        if (textBufferSite != null)
        {
            textBufferSite.SetSite(this.serviceProvider);
        }
    
        // Instantiate a code window of type IVsCodeWindow.
        Guid clsidCodeWindow = typeof(VsCodeWindowClass).GUID;
        Guid iidCodeWindow   = typeof(IVsCodeWindow).GUID;
        IVsCodeWindow pCodeWindow =
        (IVsCodeWindow)this.parentPackage.CreateInstance( 
              ref clsidCodeWindow,
              ref iidCodeWindow,
              typeof(IVsCodeWindow));
        if (pCodeWindow != null)
        {
            // Give the text buffer to the code window.
            // We are giving up ownership of the text buffer!
            pCodeWindow.SetBuffer((IVsTextLines)pTextBuffer);
    
            // Now tell the caller about all this new stuff 
            // that has been created.
            ppunkDocView = Marshal.GetIUnknownForObject(pCodeWindow);
            ppunkDocData = Marshal.GetIUnknownForObject(pTextBuffer);
    
            // Specify the command UI to use so keypresses are 
            // automatically dealt with.
            pguidCmdUI = VSConstants.GUID_TextEditorFactory;
    
            // This caption is appended to the filename and
            // lets us know our invocation of the core editor 
            // is up and running.
            pbstrEditorCaption = " [MyPackage]";
    
            retval = VSConstants.S_OK;
        } 
    } 
    return retval; 
    
  13. 프로젝트를 컴파일하고 오류가 없는지 확인 합니다.

편집기 팩터리를 등록 하려면

  1. 솔루션 탐색기, Resources.resx 파일의 문자열 테이블에 열을 두 번 클릭 항목 문자열입니다 선택 합니다.

  2. 이름 식별자를 변경 IDS_EDITORNAME 및 텍스트 MyPackage 편집기. 이 문자열 편집기 이름으로 표시 됩니다.

  3. VSPackage.resx 파일을 열고 새 추가 문자열, 101, 값으로 이름을 설정 IDS_EDITORNAME. 방금 만든 문자열에 액세스 하는 리소스 ID가 있는 패키지를 제공 합니다.

    참고

    다른 VSPackage.resx 파일에 있는 경우 해당 문자열은 name 속성이 설정 하려면 101, 다음 단계에서 다른 특수, 숫자 값으로 대체.

  4. 솔루션 탐색기, MyPackagePackage.cs 파일을 엽니다.

    이것은 주 패키지 파일입니다.

  5. 다음 사용자가 속성을 하기 전에 추가 Guid 특성입니다.

    <ProvideEditorFactoryAttribute(GetType(EditorFactory), 101)> _
    <ProvideEditorExtensionAttribute(GetType(EditorFactory), _
          ".myext", 32, NameResourceID:=101 )> _
    
    [ProvideEditorFactory(typeof(EditorFactory), 101)]
    [ProvideEditorExtension(typeof(EditorFactory), 
          ".myext", 32, NameResourceID = 101)] 
    

    ProvideEditorExtensionAttribute 특성 확장 로드 되 고 사용자 편집기 팩터리를 호출할 수 없는 파일을 언제 든 지 않도록 사용자 편집기 팩터리와.myext 파일 확장명을 연결 합니다.

  6. Private 변수를 추가할의 MyPackage 클래스에 생성자 전에 하 고 형식 지정 EditorFactory.

    Private editorFactory As EditorFactory
    
    private EditorFactory editorFactory;
    
  7. 찾기는 Initialize 메서드 (열어야 할 수도 있습니다의 Package Members 숨겨진된 영역)를 호출 하면 다음 코드를 추가 하 고 base.Initialize().

    'Create our editor factory and register it. 
    Me.editorFactory = New EditorFactory(Me)
    MyBase.RegisterEditorFactory(Me.editorFactory)
    
    // Create our editor factory and register it.
    this.editorFactory = new EditorFactory(this);
    base.RegisterEditorFactory(this.editorFactory);
    
  8. 프로그램을 컴파일하고 오류가 없는지 확인 합니다.

    이 단계 실험 레지스트리 하이브에 편집기 팩터리를 등록 Visual Studio. Resource.h 파일을 무시 하 라는 메시지가 나타나면 클릭 확인.

  9. TextFile1.myext 라는 샘플 파일을 만듭니다.

  10. 키를 눌러 F5 는 실험의 인스턴스를 열려면 Visual Studio.

  11. 실험에서 Visual Studio에 파일 메뉴를 가리키고 열기 하 고 다음을 클릭 파일.

  12. Textfile1.myext를 찾아서 누른 다음 열기.

    이제 파일을 로드 합니다.

강력한 프로그래밍

Visual Studio 코어 편집기 처리는 다양 한 범위의 텍스트 기반 파일 형식 및 중괄호 일치, 다양 한 구문 강조 표시를 하는 것과 같은 기능을 제공 하는 언어 서비스 및 단어 완성 및 멤버 완성 목록 IntelliSense와 밀접 하 게 작동 합니다. 다음 텍스트 기반 파일로 작업 하는 경우 특정 파일 형식을 지 원하는 사용자 지정 언어 서비스와 함께 코어 편집기를 사용할 수 있습니다.

있는 Vspackage를 호출을 Visual Studio 코어 편집기 편집기 팩터리를 지정 하 여. 이 편집기 팩터리와 연결 된 파일을 로드 하는 언제 든 지 사용 됩니다. 다음 파일이 프로젝트의 일부인 경우 코어 편집기 자동 하면 Vspackage로 재정의 하지 않으면 호출 됩니다. 프로젝트 외부에서 파일이 로드 되 면, 다음 코어 편집기 명시적으로 하면 Vspackage가 호출 되어야 합니다.

코어 편집기에 대 한 자세한 내용은 참조 하십시오. 코어 편집기 내부.

참고 항목

개념

코어 편집기에서 레거시 API를 사용 하 여 인스턴스화

기타 리소스

코어 편집기 내부