从Visual Studio文本修饰扩展名获取当前文件名 [英] Get the current filename from a Visual Studio text adornment extension

查看:103
本文介绍了从Visual Studio文本修饰扩展名获取当前文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VS扩展开发的新手.我目前正在使用VS 2015中的文本装饰示例,并且能够正确显示彩色框.现在,我想扩展示例,使装饰仅出现在某些文件名上.

I'm new to VS extension development. I'm currently working with the text adornment sample in VS 2015 and have been able to get coloured boxes showing correctly. Now I want to extend the sample so the adornment only appears on certain file names.

Google搜索说我可以使用ITextDocumentFactoryService.TryGetTextDocument接口和IWpfTextView.TextBuffer属性来获取文件名.听起来不错.但是我似乎无法真正获得该界面.

Googling has said I can use ITextDocumentFactoryService.TryGetTextDocument interface with the IWpfTextView.TextBuffer property to get a filename. This sounds great. But I can't seem to actually get the interface.

在我的课堂上,我有:

    [Import]
    public ITextDocumentFactoryService TextDocumentFactoryService = null;

但是它始终为NULL.

But it is always NULL.

如何获取ITextDocumentFactoryService?

namespace Test
{
    internal sealed class TestAdornment
    {
        [Import]
        public ITextDocumentFactoryService TextDocumentFactoryService = null;

        public TestAdornment(IWpfTextView view)
        {
        }

        /// <summary>
        /// Adds the scarlet box behind the 'a' characters within the given line
        /// </summary>
        /// <param name="line">Line to add the adornments</param>
        private void CreateVisuals(ITextViewLine line)
        {
            // TextDocumentFactoryService is NULL
        }
    }
}

推荐答案

TextAdornmentTextViewCreationListener.cs

TextAdornmentTextViewCreationListener.cs

[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal sealed class TextAdornmentTextViewCreationListener : IWpfTextViewCreationListener
{
    [Import]
    public ITextDocumentFactoryService textDocumentFactory { get; set; }

    //...

    public void TextViewCreated(IWpfTextView textView)
    {
        new TextAdornment(textView, textDocumentFactory);
    }
}

TextAdornment.cs

TextAdornment.cs

internal sealed class TextAdornment
    {
        private readonly ITextDocumentFactoryService textDocumentFactory;
        private ITextDocument TextDocument;

        //...    

        public TextAdornment(IWpfTextView view, ITextDocumentFactoryService textDocumentFactory)
        {
            //...

            this.textDocumentFactory = textDocumentFactory;

            //...
        }

     internal void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
        {
            var res = this.textDocumentFactory.TryGetTextDocument(this.view.TextBuffer, out this.TextDocument);
            if (res)
            {
                //this.TextDocument.FilePath;
            }
            else
            {
                //ERROR
            }
        }
    }

这篇关于从Visual Studio文本修饰扩展名获取当前文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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