Visual Studio SDK-如何在调用的命令上添加边距标志符号? [英] Visual Studio SDK - How to add a margin glyph on command invoked?

查看:85
本文介绍了Visual Studio SDK-如何在调用的命令上添加边距标志符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改此示例: https://msdn.microsoft.com/en-us/library/ee361745.aspx 以便在单击添加按钮时将标志符号添加到页边空白?

How can I modify this sample: https://msdn.microsoft.com/en-us/library/ee361745.aspx to have glyphs added to the margin when a button I added is clicked?

我有一个创建特殊断点的按钮.我希望我自己的页边标志符号能够识别这种类型.因此,我在Tagger类中编写了GetTags方法,如下所示:

I have a button which creates a special kind of breakpoint. I would like this kind to be recognized by my own margin glyph. So I wrote the GetTags method in my Tagger class as follows:

    IEnumerable<ITagSpan<MyBreakpointTag>> ITagger<MyBreakpointTag>.GetTags(NormalizedSnapshotSpanCollection spans)
    {
        if (BreakpointManager != null)
        {
            DTE2 ide = ServiceProvider.GlobalProvider.GetService(typeof(DTE)) as DTE2;
            Document document = ide.ActiveDocument;

            foreach (SnapshotSpan span in spans)
            {
                ITextSnapshot textSnapshot = span.Snapshot;
                foreach (ITextSnapshotLine textSnapshotLine in textSnapshot.Lines)
                {
                    if (BreakpointManager.IsMyBreakpointAt(document.FullName, textSnapshotLine.LineNumber + 1))
                    {
                        yield return new TagSpan<MyBreakpointTag>(new SnapshotSpan(textSnapshotLine.Start, 1),
                                new MyBreakpointTag());
                    }
                }
            }
        }
    }

但是,在将光标移动到另一行代码或更改代码后,会添加字形.单击按钮后立即添加字形,我该怎么办?

However, glyphs are added after moving the cursor to a different line of code or making changes to the code. What do I have to do to have glyphs added right after the button is clicked?

推荐答案

只要出现布局,编辑器就会调用GetTags,但是由于任何随机原因,编辑器都不会调用它. (想想:它将如何知道何时打电话给您?)您需要从标记器中引发TagsChanged事件,以说给定跨度的标记已更改,然后它将再次调用GetTags进行刷新.

GetTags is called by the editor whenever a layout happens, but the editor won't call it for any random reason. (Think: how would it know when to call you?) You need to raise the TagsChanged event from your tagger to say the tags for a given span changed, and then it'll call GetTags again to refresh.

作为不相关的建议:出于以下几个原因,您不应该在GetTag中使用DTE.ActiveDocument:

As an unrelated piece of advice: you shouldn't be using DTE.ActiveDocument in your GetTags for a few reasons:

  1. GetTags应该尽可能快...调用DTE方法很少很快.
  2. 想象一下您有两个打开的文件,并且为非活动文件调用了GetTags.这将使两个文件都使用相同的文件名,这可能是不好的.有代码
  1. GetTags should be as fast as possible...calling DTE methods are rarely fast.
  2. Imagine you have two files open, and GetTags is called for the non-active file. That would have both files looking at the same filename which is probably bad. There's code here that shows how to fetch the file name from an ITextBuffer.

这篇关于Visual Studio SDK-如何在调用的命令上添加边距标志符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆