提交 a740f664 编写于 作者: A Ankita Khera

Initial logic for tagging

上级 5c5d58d0
using System.Windows;
using System.Windows.Controls;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
namespace Microsoft.CodeAnalysis.Editor.CSharp.InlineParamNameHints
{
class InlineParamHintsTag : IntraTextAdornmentTag
{
public readonly int TagLength;
public InlineParamHintsTag(string text, int tagLength, TextFormattingRunProperties format)
: base(CreateElement(text, format), null, (tagLength == 0) ? ((PositionAffinity?)PositionAffinity.Predecessor) : null)
{
TagLength = tagLength;
}
private static UIElement CreateElement(string text, TextFormattingRunProperties format)
{
var block = new TextBox
{
Text = text,
Foreground = format.ForegroundBrush,
FontFamily = format.Typeface.FontFamily,
FontSize = format.FontRenderingEmSize,
FontStyle = FontStyles.Normal
};
block.FontSize = format.FontRenderingEmSize;
block.FontStyle = FontStyles.Italic;//.Oblique;
block.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
return block;
}
}
}

using System.ComponentModel.Composition;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Tagging;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Editor.Tagging;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Formatting;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.Utilities;
namespace Microsoft.CodeAnalysis.Editor.CSharp.InlineParamNameHints
{
[Export(typeof(IViewTaggerProvider))]
[ContentType("csharp")]
[TagType(typeof(IntraTextAdornmentTag))]
[Name("InlineParamNameHintsTaggerProvider")]
internal class InlineParamNameHintsTaggerProvider : AsynchronousTaggerProvider<InlineParamHintsTag>
{
private TextFormattingRunProperties _format;
[ImportingConstructor]
public InlineParamNameHintsTaggerProvider(
IThreadingContext threadingContext,
IAsynchronousOperationListenerProvider listenerProvider,
IForegroundNotificationService notificationService)
: base(threadingContext, listenerProvider.GetListener(FeatureAttribute.InlineParamNameHints), notificationService)
{
}
protected override ITaggerEventSource CreateEventSource(ITextView textViewOpt, ITextBuffer subjectBuffer)
{
return TaggerEventSources.OnTextChanged(subjectBuffer, TaggerDelay.NearImmediate);
}
protected override async Task ProduceTagsAsync(TaggerContext<InlineParamHintsTag> context, DocumentSnapshotSpan documentSnapshotSpan, int? caretPosition)
{
var cancellationToken = context.CancellationToken;
var document = documentSnapshotSpan.Document;
var snapshotSpan = documentSnapshotSpan.SnapshotSpan;
var paramNameHintsService = document.GetLanguageService<InlineParameterNameHintsService.IInlineParamNameHintsService>();
var paramNameHintSpans = await paramNameHintsService.GetInlineParameterNameHintsAsync(document, snapshotSpan.Span.ToTextSpan(), cancellationToken).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
foreach (var span in paramNameHintSpans)
{
context.AddTag(new TagSpan<InlineParamHintsTag>(span.Item2.ToSnapshotSpan(snapshotSpan.Snapshot), new InlineParamHintsTag(span.Item1, span.Item1.Length + 1, _format)));
}
}
}
}
using System.Collections.Generic;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.InlineParameterNameHintsService;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.CSharp.InlineParameterNameHints
{
[ExportLanguageService(typeof(IInlineParamNameHintsService), LanguageNames.CSharp), Shared]
internal class InlineParamNameHintsService : IInlineParamNameHintsService
{
[ImportingConstructor]
public InlineParamNameHintsService()
{
}
public async Task<IEnumerable<(string, TextSpan)>> GetInlineParameterNameHintsAsync(
Document document,
TextSpan textSpan,
CancellationToken cancellationToken)
{
var tree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var node = await tree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var spans = new List<(string name, TextSpan span)>();
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var invocations = node.Traverse<SyntaxNode>(textSpan, IsInvocationExpression);
// var invocation = node.DescendantNodes()
foreach (var invocationNode in invocations)
{
var invo = (InvocationExpressionSyntax)invocationNode;
foreach (var argument in invo.ArgumentList.Arguments)
{
if (argument.NameColon == null && IsLiteralOrNoNamedExpression(argument))
{
var param = argument.DetermineParameter(semanticModel, cancellationToken);
spans.Add(param.Name, argument.Span);
}
}
}
return spans;
}
private static bool IsLiteralOrNoNamedExpression(ArgumentSyntax arg)
{
if (arg.Expression is LiteralExpressionSyntax)
{
return true;
}
if (arg.Expression is ObjectCreationExpressionSyntax)
{
return true;
}
if (arg.Expression is CastExpressionSyntax)
{
var cast = (CastExpressionSyntax)arg.Expression;
if (cast.Expression is LiteralExpressionSyntax)
{
return true;
}
return false;
}
if (arg.Expression is PrefixUnaryExpressionSyntax)
{
var negation = (PrefixUnaryExpressionSyntax)arg.Expression;
if (negation.Operand is LiteralExpressionSyntax)
{
return true;
}
return false;
}
return false;
}
private static bool IsInvocationExpression(SyntaxNode node)
{
return node is InvocationExpressionSyntax;
}
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.InlineParameterNameHintsService
{
internal interface IInlineParamNameHintsService : ILanguageService
{
Task<IEnumerable<(string, TextSpan)>> GetInlineParameterNameHintsAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken = default);
}
}
......@@ -23,6 +23,7 @@ internal partial class FeatureAttribute
public const string GoToImplementation = nameof(GoToImplementation);
public const string GraphProvider = nameof(GraphProvider);
public const string InfoBar = nameof(InfoBar);
public const string InlineParamNameHints = nameof(InlineParamNameHints);
public const string KeywordHighlighting = nameof(KeywordHighlighting);
public const string LightBulb = nameof(LightBulb);
public const string LineSeparators = nameof(LineSeparators);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册