为自定义编辑器实现“导航栏”

时间:2014-02-21 11:11:51

标签: visual-studio-extensions vsx vs-extensibility vspackage visual-studio-sdk

注册自定义语言服务扩展时,Visual Studio会在Text Editor节点(在Visual Studio选项对话框中)中为该语言创建新的选项条目。在该节点下面创建了两个名为GeneralTabs的默认节点,其中General选项卡包含语句完成和显示设置......

Dispay组中有三种选择;其中一个是Navigation Bar复选框(显示/隐藏编辑器的导航栏)。对于我的自定义语言服务,此选项已禁用。当然,它还没有实现。

enter image description here

我想知道,我必须做什么,为我的自定义编辑器提供一个导航栏...我想我必须在编辑器工厂或语言服务中实现某个界面包必须导出某个MEF组件,或者,或......

2 个答案:

答案 0 :(得分:2)

Jon Senchyna的回答引导我走向正确的方向。永远不会调用OnSynchronizeDropdowns方法(在这种情况下SDK文档是错误的)。最后的技巧是至少覆盖GetComboAttributesGetEntryAttributesGetEntryText以获取两个组合框的纯文本项目......

[ComVisible(true)]
public sealed class CustomTypeAndMemberDropdownBars : TypeAndMemberDropdownBars
{
    private readonly IList<string> declarations;

    private readonly IList<string> members;

    public CustomTypeAndMemberDropdownBars(
        LanguageService languageService, 
        IVsTextView view)
        : base(languageService)
    {
        // TODO: initialize declarations and members from the given text view...
        this.declarations = ...
        this.members = ...
    }

    private enum ComboIndex
    {
        Types = 0, 

        Members = 1
    }

    public override int GetComboAttributes(
        int combo, 
        out uint entries, 
        out uint entryType, 
        out IntPtr imageList)
    {
        entries = 0;
        imageList = IntPtr.Zero;

        entryType = (uint)DROPDOWNENTRYTYPE.ENTRY_TEXT;

        var comboType = (ComboIndex)combo;
        switch (comboType)
        {
            case ComboIndex.Types:
                entries = (uint)this.declarations.Count();
                break;

            case ComboIndex.Members:
                entries = (uint)this.members.Count();
                break;
        }

        return VSConstants.S_OK;
    }

    public override int GetEntryAttributes(
        int combo, 
        int entry, 
        out uint fontAttrs)
    {
        fontAttrs = (uint)DROPDOWNFONTATTR.FONTATTR_PLAIN;

        return VSConstants.S_OK;
    }

    public override int GetEntryText(
        int combo, 
        int entry, 
        out string text)
    {
        text = null;
        var comboType = (ComboIndex)combo;
        switch (comboType)
        {
            case ComboIndex.Types:
                text = this.declarations[entry];
                break;

            case ComboIndex.Members:
                text = this.members[entry];
                break;
        }

        return VSConstants.S_OK;
    }

    public override bool OnSynchronizeDropdowns(
        LanguageService languageService, 
        IVsTextView textView, 
        int line, 
        int col, 
        ArrayList dropDownTypes, 
        ArrayList dropDownMembers, 
        ref int selectedType, 
        ref int selectedMember)
    {
        return false;
    }
}

答案 1 :(得分:1)

我相信以下步骤应该是您所需要的:

  1. Package班级中,在ShowDropDownOptions属性
  2. 中将ProvideLanguageService属性设置为true
  3. 创建一个实现TypeAndMemberDropdownBars
  4. 的类
  5. 在您的LanguageService课程中,实施CreateDropDownHelper功能并让其返回TypeAndMemberDropdownBars
  6. 的实例