Partager via


Procédure pas - à - pas : Obtenir une liste des extraits de code installés (managed de package)

Un extrait est un peu de code qui peut être inséré dans la mémoire tampon source avec une commande de menu (qui permet de choisir entre une liste d'extraits de code installés) ou en sélectionnant un raccourci de l'extrait de code d'une liste de saisie semi-automatique Intellisense.

la méthode d' EnumerateExpansions obtient tous les extraits de code pour un langage GUID de détail. Les raccourcis pour ces extraits de code peuvent être insérés dans une liste de saisie semi-automatique Intellisense.

Consultez Prise en charge des extraits de code (managed de package) pour plus d'informations sur l'implémentation des extraits de code dans un service de langage managé (MPF) du package.

Pour récupérer une liste d'extraits de code

  • Le code suivant montre comment obtenir une liste des extraits de code pour une langue donnée. Les résultats sont stockés dans un tableau de structures d' VsExpansion . Cette méthode utilise la méthode statique d' GetGlobalService pour obtenir l'interface d' IVsTextManager du service d' SVsTextManager . Toutefois, vous pouvez également utiliser le fournisseur de services donné à votre VSPackage et appeler la méthode d' 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]);
                            }
                        }
                    }
                }
            }
        }
    }
    

pour appeler la méthode de GetSnippets

  • La méthode suivante montre comment appeler la méthode d' GetSnippets à l'opération d'analyse. La méthode d' OnParseComplete est appelée après une opération d'analyse qui a été démarrée par la raison Check.

Notes

Les listis de tableau des expansionsList mis en cache pour des raisons de performance.Les modifications apportées aux extraits de code ne sont pas dans la liste jusqu'à ce que le service de langage soit interrompu et rechargé (par exemple, en arrêtant et le redémarrage 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);
        }
    }
}

pour utiliser les informations d'extrait de code

  • Le code suivant montre comment utiliser des informations d'extrait de code retournées par la méthode d' GetSnippets . La méthode d' AddSnippets en est appelée à partir de l'analyseur en réponse à analysent la raison utilisé pour remplir la liste des extraits de code. Cela doit avoir lieu après que le complet analysent a été fait pour la première fois.

    La méthode d' AddDeclaration génère une liste de déclarations qui seront s'affiche dans une liste de saisie semi-automatique.

    La classe d' TestDeclaration contient toutes les informations pouvant être affichées dans une liste de saisie semi-automatique ainsi que le type de déclaration.

    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"));
                }
            }
        }
    }
    

Voir aussi

Concepts

Prise en charge des extraits de code (managed de package)