Visual Studio 2010 SDK - 如何在XML注释组旁边放置一个装饰?

mic*_*ael 7 c# visual-studio-2010 xml-comments adornment visual-studio-sdk

我无法找到如何执行此操作,Visual Studio SDK参考也不是很有帮助.

我正在试图弄清楚如何获得NormalizedSnapshotSpanCollectionXML评论.我想在它们旁边放一个图标...我不希望每行旁边有一个图标,但只是在每个组的第一行旁边...

///<summary>SomeXML Comment</summary>   [ICON]
///<remarks>some remarks</remarks>
public void Foo()
{
    ///Some false XML comment line that does not get an icon.
}
Run Code Online (Sandbox Code Playgroud)

Max*_*kin 11

这就是我能得到的,我认为它与你需要的非常相似.如果您有任何疑问,我将更新详细信息.

VS 2010图标装饰

我从VS 2010 SDK网站开始使用此示例.它已经非常接近您的需求,但需要更多步骤.


下载C#版本,解压缩到一个文件夹,编译.要运行它并测试,您需要转到Project> Properties> Debug

例如,您需要选择"启动外部程序"选项并设置VS 2010应用程序的路径 C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe

在命令行参数集中: /rootsuffix Exp

现在你应该能够运行它,在打开的VS中创建一些示例项目,如果你在任何地方键入一个六位数的数字00AA00,它将显示为具有相应颜色的矩形.关闭调试VS实例.


现在让我们编辑一些代码.在ColorAdornmentTagger.cs评论中定义#define HIDING_TEXT.这将显示文本旁边的装饰,而不是它.

在同一个文件中,您需要找到SnapshotSpan adornmentSpan初始化的位置并将行更改为:

SnapshotSpan adornmentSpan = new SnapshotSpan(colorTagSpans[0].End, 0);
Run Code Online (Sandbox Code Playgroud)

这将在文本跨度之后而不是之前放置装饰.


ColorTagger.cs.在构造函数中更改正则表达式,因此构造函数现在看起来像

    internal ColorTagger(ITextBuffer buffer)
        : base(
        buffer, 
        new[] { new Regex(@"/// <summary>.*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase) }
        )
    {
    }
Run Code Online (Sandbox Code Playgroud)

这将设置正则表达式以识别方法注释行.

我们不会使用此类中的其他方法,您可以对它们进行注释或返回一些随机颜色.


在'ColorAdornment.cs'中.这是装饰WPF控件本身.首先将基类更改ButtonContentControl.将类的构造函数更改为

    internal ColorAdornment(ColorTag colorTag)
    {
        BitmapImage image = new BitmapImage(); 
        using (FileStream stream = File.OpenRead("c:\\temp\\sologo.png")) 
        { 
            image.BeginInit(); 
            image.StreamSource = stream; 
            image.CacheOption = BitmapCacheOption.OnLoad; 
            image.EndInit(); 
        }

        this.Content = new Image() { Margin = new Thickness(20,0,0,0), Width = 100, Height = 30, Source = image };
    }
Run Code Online (Sandbox Code Playgroud)

您可以将图像路径更改为所需的图像路径.我刚从维基百科下载了SO徽标并放入了我的临时文件夹.

编译并运行.您应该能够在调试VS实例中的注释旁边看到SO徽标.


一些额外的评论.

首先,通过这种方式,您只需要开始使用工作原型,您应该重命名类并根据需要清理代码.

其次,当我调试它时,我的调试VS不时冻结.我认为这可能与锁定有关IntraTextAdornmentTagger.cs

如果您还看到冻结,请尝试以这种方式更新以下方法:

    protected void InvalidateSpans(IList<SnapshotSpan> spans)
    {
        if (spans.Count == 0)
            return;
        bool wasEmpty = false;
        lock (this.invalidatedSpans)
        {
            wasEmpty = this.invalidatedSpans.Count == 0;
            this.invalidatedSpans.AddRange(spans);
        }

        if (wasEmpty)
            this.view.VisualElement.Dispatcher.BeginInvoke(new Action(AsyncUpdate));
    }
Run Code Online (Sandbox Code Playgroud)

和AsyncUpdate这样:

    private void AsyncUpdate()
    {
        // Store the snapshot that we're now current with and send an event
        // for the text that has changed.
        if (this.snapshot != this.view.TextBuffer.CurrentSnapshot)
        {
            this.snapshot = this.view.TextBuffer.CurrentSnapshot;

            Dictionary<SnapshotSpan, TAdornment> translatedAdornmentCache = new Dictionary<SnapshotSpan, TAdornment>();

            foreach (var keyValuePair in this.adornmentCache)
                translatedAdornmentCache.Add(keyValuePair.Key.TranslateTo(this.snapshot, SpanTrackingMode.EdgeExclusive), keyValuePair.Value);

            this.adornmentCache = translatedAdornmentCache;
        }

        List<SnapshotSpan> spansCopy;
        lock (this.invalidatedSpans)
        {
            spansCopy = this.invalidatedSpans.ToList();
            this.invalidatedSpans.Clear();
        }

        List<SnapshotSpan> translatedSpans = spansCopy.Select(s => s.TranslateTo(this.snapshot, SpanTrackingMode.EdgeInclusive)).ToList();

        if (translatedSpans.Count == 0)
            return;

        var start = translatedSpans.Select(span => span.Start).Min();
        var end = translatedSpans.Select(span => span.End).Max();

        RaiseTagsChanged(new SnapshotSpan(start, end));
    }
Run Code Online (Sandbox Code Playgroud)