Show tooltip on hover over text

后端 未结 1 541
再見小時候
再見小時候 2021-01-15 06:54

I want to create extension that allows to show custom message when I hover over a text.

E.g. \"test-text\" should give tooltip \"OK\" instead of current \"ITrackin..

相关标签:
1条回答
  • 2021-01-15 07:14

    The sample send by Lance Li-MSFT was really helpful, but in order to get this working I had to spend some time.

    Important steps:

    • Import LineAsyncQuickInfoSourceProvider.cs and LineAsyncQuickInfoSource.cs
    • Add reference to System.ComponentModel.Composition by add reference dialog (right click on the project name)
    • Get missing references by installing them using NuGet Package Manager
    • To initialize MEF components, you’ll need to add a new Asset to 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.
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题