【问题标题】:How to write an extension that displays different text than the underlying file? (like the parameter names)如何编写显示与基础文件不同的文本的扩展名? (如参数名称)
【发布时间】:2019-08-30 21:44:08
【问题描述】:

我想为 Visual Studio 编写一个扩展,将“var”关键字替换为显式类型,但仅在视觉上而不是在代码本身中。

经过一些 MSDN 和网络研究后,我尝试使用 ITextViewModel 的 VisualBuffer(投影缓冲区),但它也更改了文件中的代码。

例如,在这里我通过将“int”替换为“var”来尝试另一种方式

[Export(typeof(ITextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal class TestViewCreationListener : ITextViewCreationListener
{
    private ITextBuffer _buffer;

    public void TextViewCreated(ITextView textView)
    {
        ITextViewModel textVM = textView.TextViewModel;
        _buffer = textVM.VisualBuffer;

        string text = _buffer.CurrentSnapshot.GetText();
        int index = text.IndexOf("int ");
        while (index > 0)
        {
            _buffer.Replace(new Span(index, 3), "var");
            index = text.IndexOf("int ", index + 4);
        }
    }
}

提前致谢:)

【问题讨论】:

  • but it also changes code in the file,所以您不想将 var 更改为 int 或什么,您指的是像 this issue 这样的快速信息吗?
  • 对,我在一个团队中工作,不想更改喜欢“var”的人的代码。但是,我讨厌它并希望在不使用鼠标工具提示的情况下查看显式类型
  • 我了解您想要获取显式类型,但是如果您不想要快速信息格式,您喜欢哪种方式?由于您没有更改代码文件中的 var,因此我不确定您要以哪种方式获取显式类型,将鼠标悬停在其上并获取类型?
  • 不,我想的是 WPF 中的转换器,它会替换你所看到的,而不是它背后的模型。但是,也许这是不可能的。我知道也有投影缓冲区,但据我了解,它们用于在另一个地方显示文本(如大纲窗口)

标签: visual-studio visual-studio-extensions vs-extensibility


【解决方案1】:

啊,我自己找到了解决办法。使用 IntraTextAdornmentTagger 做到了 :)

【讨论】:

  • 嗨 GeluX,感谢您的分享,请将您的回复标记为答案,这将有助于其他社区成员更轻松地搜索此有用信息,这只是一个提醒 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多