다음을 통해 공유


연습: 설치 코드 조각 (패키지 관리 프레임 워크)를 가져오는 중

(설치 코드 조각 목록 중에서 선택 해)는 메뉴 명령을 사용 하거나 이와 원본 버퍼에 삽입할 수 있는 코드 조각을 코드 조각 되는 IntelliSense 완성 목록에서 코드 조각 바로 가기를 선택 합니다.

EnumerateExpansions 메서드는 GUID는 특정 언어에 대 한 모든 코드 조각을 가져옵니다. 이러한 조각에 대 한 바로 가기 키를 IntelliSense 완성 목록에 삽입할 수 있습니다.

참조 하십시오 코드 조각 (패키지 관리 프레임 워크)에 대 한 지원 패키지 관리 프레임 워크 (MPF) 언어 서비스에 코드 조각 구현에 대 한 자세한 내용은.

코드 조각 목록을 검색 하려면

  • 다음 코드는 지정 된 언어에 대 한 코드 조각 목록을 가져오는 방법을 보여 줍니다. 결과를 배열에 저장 된 VsExpansion 구조체입니다. 정적이이 메서드를 사용 하 여 GetGlobalService 메서드는 IVsTextManager 의 인터페이스는 SVsTextManager 서비스. 그러나 VSPackage 및 통화를 제공 하는 서비스 공급자도 사용할 수 있는 QueryService 방법입니다.

    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio.Package;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.TextManager.Interop;
    
    [Guid("00000000-0000-0000-0000-000000000000")] //create a new GUID for the language service
    namespace TestLanguage
    {
        class TestLanguageService : LanguageService
        {
            private void GetSnippets(Guid languageGuid,
                                     ref ArrayList expansionsList)
            {
                IVsTextManager textManager = (IVsTextManager)Package.GetGlobalService(typeof(SVsTextManager));
                if (textManager != null)
                {
                    IVsTextManager2 textManager2 = (IVsTextManager2)textManager;
                    if (textManager2 != null)
                    {
                        IVsExpansionManager expansionManager = null;
                        textManager2.GetExpansionManager(out expansionManager);
                        if (expansionManager != null)
                        {
                            // Tell the environment to fetch all of our snippets.
                            IVsExpansionEnumeration expansionEnumerator = null;
                            expansionManager.EnumerateExpansions(languageGuid,
                            0,     // return all info
                            null,    // return all types
                            0,     // return all types
                            1,     // include snippets without types
                            0,     // do not include duplicates
                            out expansionEnumerator);
                            if (expansionEnumerator != null)
                            {
                                // Cache our expansions in a VsExpansion array 
                                uint count   = 0;
                                uint fetched = 0;
                                VsExpansion expansionInfo = new VsExpansion();
                                IntPtr[] pExpansionInfo   = new IntPtr[1];
    
                                // Allocate enough memory for one VSExpansion structure. This memory is filled in by the Next method.
                                pExpansionInfo[0] = Marshal.AllocCoTaskMem(Marshal.SizeOf(expansionInfo));
    
                                expansionEnumerator.GetCount(out count);
                                for (uint i = 0; i < count; i++)
                                {
                                    expansionEnumerator.Next(1, pExpansionInfo, out fetched);
                                    if (fetched > 0)
                                    {
                                        // Convert the returned blob of data into a structure that can be read in managed code.
                                        expansionInfo = (VsExpansion)Marshal.PtrToStructure(pExpansionInfo[0], typeof(VsExpansion));
    
                                        if (!String.IsNullOrEmpty(expansionInfo.shortcut))
                                        {
                                            expansionsList.Add(expansionInfo);
                                        }
                                    }
                                }
                                Marshal.FreeCoTaskMem(pExpansionInfo[0]);
                            }
                        }
                    }
                }
            }
        }
    }
    

GetSnippets 메서드를 호출 하려면

  • 다음 메서드를 호출 하는 방법을 보여 줍니다 있는 GetSnippets 메서드는 구문 분석 작업이 완료 될 때. OnParseComplete 메서드가 호출 구문 분석 작업을 때문에 시작 된 Check.

참고

expansionsList Listis 성능상의 이유로 캐시 배열입니다.언어 서비스를 중지 하 고 다시 로드 될 때까지 목록에 수 코드 조각은 변경 내용이 반영 되지 않는 (중지 하 고 다시 시작 하 여 예를 들어, Visual Studio).

class TestLanguageService : LanguageService
{
    private ArrayList expansionsList;

    public override void OnParseComplete(ParseRequest req)
    {
        if (this.expansionsList == null)
        {
            this.expansionsList = new ArrayList();
            GetSnippets(this.GetLanguageServiceGuid(),
                ref this.expansionsList);
        }
    }
}

조각 정보를 사용 하려면

  • 다음 코드 조각 반환 된 정보를 사용 하는 방법을 보여 줍니다 있는 GetSnippets 메서드. AddSnippets 파서의 응답 하 여 코드 조각 목록을 채우는 데 사용 되는 구문 분석 이유로 메서드를 호출 합니다. 첫 번째 시간에 대 한 전체 구문 분석 작업이 끝난 다음 자리를 걸립니다.

    AddDeclaration 메서드 선언 완성 목록에 표시 되는 나중에 목록을 작성 합니다.

    TestDeclaration 클래스 선언의 뿐만 아니라 완성 목록에 표시 될 수 있는 모든 정보가 포함 되어 있습니다.

    class TestAuthoringScope : AuthoringScope
    {
        public void AddDeclarations(TestDeclaration declaration)
        {
            if (m_declarations == null)
                m_declarations = new List<TestDeclaration>();
            m_declarations.Add(declaration);
         }
    }
    class TestDeclaration 
    {
        private string m_name;
        private string m_description;
        private string m_type;
    
        public TestDeclaration(string name, string desc, string type)
        {
            m_name = name;
            m_description = desc;
            m_type = type;
        }
    
    class TestLanguageService : LanguageService
    {
        internal void AddSnippets(ref TestAuthoringScope scope)
        {
            if (this.expansionsList != null && this.expansionsList.Count > 0)
            {
                int count = this.expansionsList.Count;
                for (int i = 0; i < count; i++)
                {
                    VsExpansion expansionInfo = (VsExpansion)this.expansionsList[i];
                    scope.AddDeclaration(new TestDeclaration(expansionInfo.title,
                         expansionInfo.description,
                         "snippet"));
                }
            }
        }
    }
    

참고 항목

개념

코드 조각 (패키지 관리 프레임 워크)에 대 한 지원