[英]Get the current filename from a Visual Studio text adornment extension
I'm new to VS extension development. 我是VS扩展开发的新手。 I'm currently working with the text adornment sample in VS 2015 and have been able to get coloured boxes showing correctly.
我目前正在使用VS 2015中的文本装饰示例,并且能够正确显示彩色框。 Now I want to extend the sample so the adornment only appears on certain file names.
现在,我想扩展示例,以便装饰仅出现在某些文件名上。
Googling has said I can use ITextDocumentFactoryService.TryGetTextDocument
interface with the IWpfTextView.TextBuffer
property to get a filename. Googling表示我可以将
ITextDocumentFactoryService.TryGetTextDocument
接口与IWpfTextView.TextBuffer
属性一起使用以获取文件名。 This sounds great. 听起来不错。 But I can't seem to actually get the interface.
但是我似乎无法真正获得接口。
In my class I have: 在我的课堂上,我有:
[Import]
public ITextDocumentFactoryService TextDocumentFactoryService = null;
But it is always NULL. 但它始终为NULL。
How can I get ITextDocumentFactoryService
? 如何获得
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
}
}
}
You got it via dependency injection. 您是通过依赖注入获得的。
As you only submitted 2 lines of code I suppose your context is set up, either explicitly by you, either implicitly by some environment who calls your code. 因为您只提交了两行代码,所以我想您的上下文是由您显式设置的,或者由某些调用您代码的环境隐式设置的。
Then automagically big brother will set it for you before you first access to it. 然后,在您首次使用它之前,老大哥会自动为您设置它。
...or... ...要么...
You can use constructor injection instead. 您可以改用构造函数注入。 Note: It is not you who will create your class.
注意:创建课程的不是您。
private readonly ITextDocumentFactoryService _textDocumentFactoryService;
[ImportingConstructor]
internal YourClass(ITextDocumentFactoryService textDocumentFactoryService)
{
_textDocumentFactoryService = textDocumentFactoryService;
}
So in my case I needed to put the import statement into the AdornmentTextViewCreationListener. 因此,就我而言,我需要将import语句放入AdornmentTextViewCreationListener中。 This implements IWpfTextViewCreationListener and is the one with the following decorating the class.
这实现了IWpfTextViewCreationionListener,并且具有以下修饰类。
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
Then I can add 然后我可以添加
private readonly ITextDocumentFactoryService _textDocumentFactoryService;
[ImportingConstructor]
to my class 到我班上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.