IntelliSense 成员完成 (托管包结构)

IntelliSense 成员完成是显示特定范围的成员列表如类、结构、枚举或命名空间的工具提示。 例如,在 c# 中,因此,如果在当前范围内用户键入 “this”在句点之后,类的所有成员列表或结构在用户可以选择的列表存在。

托管包框架 (MPF)提供工具提示和管理在工具提示中的列表;需要的只是从提供出现在列表中的该数据的分析器的协作。

工作机制

下面是成员列表显示两种使用 MPF 类,例如:

  • 确定插入符号在标识符或在成员完成字符并选择 列出成员 后从 IntelliSense 菜单。

  • IScanner 扫描仪检测到成员完成字符并将 MemberSelect 一个标记触发器该字符的。

成员完成字符指示类、结构或枚举的成员将按照。 例如,在中,而在 C++ 字符是 . 或 ->, c# 或 Visual Basic 成员完成字符是 .。 ,在成员选择的字符浏览时,触发器值设置为。

IntelliSense 成员列表命令

SHOWMEMBERLIST 命令启动调用 Source 类的 Completion 方法,并 Completion 方法,因此,调用与 DisplayMemberList分析原因的 ParseSource 方法分析程序。

该分析器确定当前位置的上下文以及该标记在或的当前位置之前下。 根据此标记,用于列表的说明。 例如,在 c# 中,因此,如果在类成员确定插入符号并选择 列出成员,可以获得类的所有成员列表。 如果您确定插入符号,在对象变量的过程后,可以访问类的所有成员列表对象表示。 请注意,如果插入符号位于成员确定,当成员显示在列表中,选择成员从列表替换脱字号所在与个列表中的成员。

标记触发器

MemberSelect 触发器启动调用 Source 类的 Completion 方法,并 Completion 方法,因此,调用与 MemberSelect 分析原因的分析器 (如果这个标记触发器还包括了 MatchBraces 标志,将显示成员的选择以及大括号) 的分析原因是 MemberSelectAndHighlightBraces

该分析器确定当前位置的上下文以及类型化的,则成员选择的字符之前。 通过此信息,分析器生成请求范围内的任何成员列表。 这将从 ParseSource 方法返回的 AuthoringScope 对象列表声明存储。 如果任何声明返回,成员完成工具提示中将显示。 工具提示。 CompletionSet 类的实例管理。

启用对成员完成支持

您必须具有 CodeSense 注册表项设置为 1 支持所有 IntelliSense 操作。 此注册表项可以设置一个命名参数传递给 ProvideLanguageServiceAttribute 用户属性与语言包。 语言服务类在 LanguagePreferences 类读取此注册表项的值从 EnableCodeSense 属性。

如果扫描仪返回 MemberSelect标记触发器,这样,分析器会返回列表的说明,则完成列表的成员显示。

支持在扫描仪的成员完成

,当该字符分析时,扫描程序必须能够检测成员完成字符和设置 MemberSelect 标记触发器。

示例

这是检测成员完成字符和设置适当的 TokenTriggers 标志的简化示例。 此示例仅用于阐释目的。 假定,扫描仪包含标识并从文本行的标记的方法 GetNextToken 。 代码示例设置触发器,只要它看到正确类型的字符。

using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace TestLanguagePackage
{
    public class TestScanner : IScanner
    {
        private Lexer lex;
        private const char memberSelectChar = '.';

        public bool ScanTokenAndProvideInfoAboutIt(TokenInfo tokenInfo,
                                                   ref int state)
        {
            bool foundToken = false
            string token = lex.GetNextToken();
            if (token != null)
            {
                foundToken = true;
                char c = token[0];
                if (c == memberSelectChar)
                {
                        tokenInfo.Trigger |= TokenTriggers.MemberSelect;
                }
            }
            return foundToken;
        }
    }
}

支持在分析器的成员完成

指向成员完成, Source 类调用 GetDeclarations 方法。 您必须实现在从 Declarations 类派生的类的列表。 请参见 Declarations 类有关必须执行的方法的详细信息。

,在成员选择的字符与键入时,该分析器调用与 MemberSelectMemberSelectAndHighlightBraces 。 在 ParseRequest 对象提供的位置是,在成员选择的字符开头。 该分析器必须收集在源代码中出现在成员列表在该特定位置所有成员的名称。 然后该分析器必须分析当前行确定用户希望与成员选择字符的大小。

此大小作为标识符的类型,在成员选择的字符之前。 例如,在 c# 中为具有 LanguageService类型的成员变量的 languageService ,键入 languageService。导致 LanguageService 类的所有成员列表。 在 c# 中,键入此操作。生成类的所有成员列表在当前范围内。

示例

下面的示例演示一种填充 Declarations 列表。 此代码假定,则分析器构造声明并将其添加到列表通过调用 TestAuthoringScope 类的一个 AddDeclaration 方法。

using System.Collections;
using Microsoft.VisualStudio.Package;
using Microsoft.VisualStudio.TextManager.Interop;

namespace TestLanguagePackage
{
    internal class TestDeclaration
    {
        public string Name;            // Name of declaration
        public int     TypeImageIndex; // Glyph index
        public string Description;     // Description of declaration

        public TestDeclaration(string name, int typeImageIndex, string description)
        {
            this.Name = name;
            this.TypeImageIndex = typeImageIndex;
            this.Description = description;
        }
    }


    //===================================================
    internal class TestDeclarations : Declarations
    {
        private ArrayList declarations;

        public TestDeclarations()
            : base()
        {
            declarations = new ArrayList();
        }

        public void AddDeclaration(TestDeclaration declaration)
        {
            declarations.Add(declaration);
        }

        //////////////////////////////////////////////////////////////////////
        // Declarations of class methods that must be implemented.
        public override int GetCount()
        {
            // Return the number of declarations to show.
            return declarations.Count;
        }

        public override string GetDescription(int index)
        {
            // Return the description of the specified item.
            string description = "";
            if (index >= 0 && index < declarations.Count)
            {
                description = ((TestDeclaration)declarations[index]).Description;
            }
            return description;
        }

        public override string GetDisplayText(int index)
        {
            // Determine what is displayed in the tool tip list.
            string text = null;
            if (index >= 0 && index < declarations.Count)
            {
                text = ((TestDeclaration)declarations[index]).Name;
            }
            return text;
        }

        public override int GetGlyph(int index)
        {
            // Return index of image to display next to the display text.
            int imageIndex = -1;
            if (index >= 0 && index < declarations.Count)
            {
                imageIndex = ((TestDeclaration)declarations[index]).TypeImageIndex;
            }
            return imageIndex;
        }

        public override string GetName(int index)
        {
            string name = null;
            if (index >= 0 && index < declarations.Count)
            {
                name = ((TestDeclaration)declarations[index]).Name;
            }
            return name;
        }
    }


    //===================================================
    public class TestAuthoringScope : AuthoringScope
    {
        private TestDeclarations declarationsList;

        public void AddDeclaration(TestDeclaration declaration)
        {
            if (declaration != null)
            {
                if (declarationsList == null)
                {
                    declarationsList = new TestDeclarations();
                }
                declarationsList.AddDeclaration(declaration);
            }
        }

        public override Declarations GetDeclarations(IVsTextView view,
                                                     int line,
                                                     int col,
                                                     TokenInfo info,
                                                     ParseReason reason)
        {
            return declarationsList;
        }

        /////////////////////////////////////////////////
        // Remainder of AuthoringScope methods not shown.
        /////////////////////////////////////////////////
    }


    //===================================================
    class TestLanguageService : LanguageService
    {
        public override AuthoringScope ParseSource(ParseRequest req)
        {
            TestAuthoringScope scope = new TestAuthoringScope();
            if (req.Reason == ParseReason.MemberSelect ||
                req.Reason == ParseReason.MemberSelectAndHighlightBraces)
            {
                // Gather list of declarations based on what the user
                // has typed so far. In this example, this list is an array of
                // MemberDeclaration objects (a helper class you might implement
                // to hold member declarations).
                // How this list is gathered is dependent on the parser
                // and is not shown here.
                MemberDeclarations memberDeclarations;
                memberDeclarations = GetDeclarationsForScope();

                // Now populate the Declarations list in the authoring scope.
                // GetImageIndexBasedOnType() is a helper method you
                // might implement to convert a member type to an index into
                // the image list returned from the language service.
                foreach (MemberDeclaration dec in memberDeclarations)
                {
                    scope.AddDeclaration(new TestDeclaration(
                                             dec.Name,
                                             GetImageIndexBasedOnType(dec.Type),
                                             dec.Description));
                }
            }
            return scope;
        }
    }
}