在将鼠标悬停在文本上时显示工具提示
我想创建一个扩展名,当我将鼠标悬停在文本上时,该扩展名可以显示自定义消息.
I want to create extension that allows to show custom message when I hover over a text.
例如测试文本"应提供工具提示确定",而不是当前的"ITrackin ...".
E.g. "test-text" should give tooltip "OK" instead of current "ITrackin..."
我尝试遵循 https://docs.microsoft.com/zh-cn/visualstudio/extensibility/walkthrough-displaying-quickinfo-tooltips?view=vs-2019 但是人们在说它不起作用,而且这样做还有很长的路要走.
I tried to follow https://docs.microsoft.com/en-us/visualstudio/extensibility/walkthrough-displaying-quickinfo-tooltips?view=vs-2019 but people are stating that it is not working and it's quite long way of doing this.
我找不到关于此的更多文档.我知道如何在单击窗口中显示它/获取当前选定的文本.
I cannot find any more docs on this. I know how to display it in on-click window/get currently selected text.
Lance Li-MSFT发送的示例确实很有帮助,但是为了使此工作有效,我不得不花一些时间.
The sample send by Lance Li-MSFT was really helpful, but in order to get this working I had to spend some time.
重要步骤:
- 导入LineAsyncQuickInfoSourceProvider.cs和LineAsyncQuickInfoSource.cs
- 通过添加引用对话框(右键单击项目名称)添加对System.ComponentModel.Composition的引用
- 使用NuGet软件包管理器安装缺少的参考
- 要初始化MEF组件,您需要在source.extension.vsixmanifest中添加一个新资产.
<Assets>
...
<Asset Type = "Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
</Assets>
LineAsyncQuickInfoSourceProvider.cs
它仅用于显示快速信息/工具提示.
It's just used to display quick info/tooltip.
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Utilities;
using System.ComponentModel.Composition;
namespace JSONExtension
{
[Export(typeof(IAsyncQuickInfoSourceProvider))]
[Name("Line Async Quick Info Provider")]
[ContentType("any")]
[Order]
internal sealed class LineAsyncQuickInfoSourceProvider : IAsyncQuickInfoSourceProvider
{
public IAsyncQuickInfoSource TryCreateQuickInfoSource(ITextBuffer textBuffer) //creates instance of LineAsyncQuickInfoSource for displaying Quick Info
{
return textBuffer.Properties.GetOrCreateSingletonProperty(() => new LineAsyncQuickInfoSource(textBuffer)); //this ensures only one instance per textbuffer is created
}
}
}
LineAsyncQuickInfoSource.cs
在这里您可以自定义要显示的内容.
Here you can customize what you want to display.
using Microsoft.VisualStudio.Language.Intellisense;
using Microsoft.VisualStudio.Language.StandardClassification;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Adornments;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace JSONExtension
{
internal sealed class LineAsyncQuickInfoSource : IAsyncQuickInfoSource
{
private ITextBuffer _textBuffer;
public LineAsyncQuickInfoSource(ITextBuffer textBuffer)
{
_textBuffer = textBuffer;
}
// This is called on a background thread.
public Task<QuickInfoItem> GetQuickInfoItemAsync(IAsyncQuickInfoSession session, CancellationToken cancellationToken)
{
var triggerPoint = session.GetTriggerPoint(_textBuffer.CurrentSnapshot);
if (triggerPoint != null)
{
var line = triggerPoint.Value.GetContainingLine();
var lineSpan = _textBuffer.CurrentSnapshot.CreateTrackingSpan(line.Extent, SpanTrackingMode.EdgeInclusive);
var text = triggerPoint.Value.GetContainingLine().GetText(); //get whole line of current cursor pos
ContainerElement dataElm = new ContainerElement(
ContainerElementStyle.Stacked,
new ClassifiedTextElement(
new ClassifiedTextRun(PredefinedClassificationTypeNames.Keyword, "MESSAGE TO EDIT: " + text.ToString())
));
return Task.FromResult(new QuickInfoItem(lineSpan, dataElm)); //add custom text from above to Quick Info
}
return Task.FromResult<QuickInfoItem>(null); //do not add anything to Quick Info
}
public void Dispose()
{
// This provider does not perform any cleanup.
}
}
}