我正在写一个VS扩展,它将字符C添加到类名中。
除了C和常规文本之间留有空格外,我想的最多。见图。
我的ClassTagger子类IntraTextAdornmentTagger。 MEF将调用GetAdornmentData()
,在其中找到ClassDeclarationSyntax和范围,然后将调用CreateAdornment()
创建文本块。
我将padding和margin设置为0,但是仍然有空格。
如何删除IntraTextAdornmentTag和常规文本之间的空格?
class ClassTagger : IntraTextAdornmentTagger<string, TextBlock>
{
protected override TextBlock CreateAdornment(string data, SnapshotSpan span)
{
var textBlock = new TextBlock() { Margin = new Thickness(0), Padding = new Thickness(0), FontFamily = new FontFamily("Consolas"), FontSize = 11, FontWeight = FontWeights.Regular };
textBlock.Inlines.Add("C");
return textBlock;
}
protected override bool UpdateAdornment(TextBlock adornment, string data)
{
return true;
}
protected override IEnumerable<Tuple<SnapshotSpan, PositionAffinity?, string>> GetAdornmentData(NormalizedSnapshotSpanCollection spans)
{
foreach (var span in spans)
{
SyntaxNode syntaxRoot = null;
SnapshotPoint snapshotPoint = span.Start;
var document = snapshotPoint.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
//var semanticModel = document.GetSemanticModelAsync().Result;
syntaxRoot = document.GetSyntaxRootAsync().Result;
Queue<SyntaxNode> queue = new Queue<SyntaxNode>();
queue.Enqueue(syntaxRoot);
while (queue.Count > 0)
{
var token = queue.Dequeue();
if (token is ClassDeclarationSyntax classDeclarationToken)
{
var s = new SnapshotSpan(snapshotPoint.Snapshot, classDeclarationToken.Identifier.SpanStart + 1, 0);
yield return Tuple.Create<SnapshotSpan, PositionAffinity?, string>(s, PositionAffinity.Successor, classDeclarationToken.Identifier.Text);
}
else
{
foreach (var t in token.ChildNodes())
queue.Enqueue(t);
}
}
}
}
...
}