Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Roslyn API确定当前范围是否为属性删除_C#_Roslyn_Visual Studio Extensions_Roslyn Code Analysis - Fatal编程技术网

C# 使用Roslyn API确定当前范围是否为属性删除

C# 使用Roslyn API确定当前范围是否为属性删除,c#,roslyn,visual-studio-extensions,roslyn-code-analysis,C#,Roslyn,Visual Studio Extensions,Roslyn Code Analysis,我正在构建一个visual studio扩展,用于使用Roslyn API为各种语言结构着色,我想更改属性声明的颜色,例如Asp.Net MVC[Require]属性。我可以访问SyntaxNode和ISymbol,我当前的检查是为了确定当前节点是否是属性声明: public static bool IsCSharpAttributeSyntaxKind(this SyntaxNode node) { return node.Kind() == SyntaxKind.Attrib

我正在构建一个visual studio扩展,用于使用Roslyn API为各种语言结构着色,我想更改属性声明的颜色,例如Asp.Net MVC
[Require]
属性。我可以访问
SyntaxNode
ISymbol
,我当前的检查是为了确定当前节点是否是属性声明:

public static bool IsCSharpAttributeSyntaxKind(this SyntaxNode node)
{
        return node.Kind() == SyntaxKind.Attribute;
}
然后像这样使用它:

if (node.IsCSharpAttributeSyntaxKind())
  {
     classificationTypeDictionary.TryGetValue(ColorCoderClassificationName.Attribute, out IClassificationType classificationValue);

     return new TagSpan<IClassificationTag>(new SnapshotSpan(snapshot, span.TextSpan.Start, span.TextSpan.Length), new ClassificationTag(classificationValue));
   }

我试过很多其他的方法,但都不管用,我想我错过了其他的方法。我知道我的问题不够好,对此我很抱歉,因为我缺乏提出好问题的术语,而且visual studio的可扩展性框架通常都没有文档,如果您需要更多详细信息,请告诉我。

您没有提到如何在扩展中获取roslyn信息,但是,由于您在代码示例中提到了
SyntaxNode
工作区
语义模型
,因此我假设您可以获得当前的
文档
。我将采取的方法是获取您试图分类的文档的根节点。一个好的起点应该是下面的代码和VSSDK示例

public static async Task<IEnumerable<ClassificationSpan>> ClassifyAttributes(Document currentDocument, CancellationToken token)
{
    // Get all attribute nodes in the current document
    var rootNode = await currentDocument.GetSyntaxRootAsync(token).ConfigureAwait(false);
    var attributesInDocument = from descendantNode in rootNode.DescendantNodesAndSelf()
                               where descendantNode.IsKind(SyntaxKind.Attribute)
                               select (AttributeSyntax)descendantNode;

    // Check to see if the attribute binds to a type (I assume you do not want to classify attributes with errors)
    var model = await currentDocument.GetSemanticModelAsync(token).ConfigureAwait(false);
    var attributeSpans = from attributeNode in attributesInDocument
                         let typeInfo = model.GetTypeInfo(attributeNode, token)
                         where typeInfo.Type.Kind != SymbolKind.ErrorType
                         select new ClassificationSpan(attributeNode.Span, _classificationType);

    // returns a set of ClassifiedSpans that your extensions classifer will colorize
    return attributeSpans;
}
公共静态异步任务ClassifyAttributes(Document currentDocument、CancellationToken令牌)
{
//获取当前文档中的所有属性节点
var rootNode=await currentDocument.GetSyntaxRootAsync(令牌).ConfigureAwait(false);
var attributesInDocument=来自根节点中的后代节点。后代节点和自身()
其中degenantNode.IsKind(SyntaxKind.Attribute)
选择(AttributeSyntax)子节点;
//检查属性是否绑定到类型(我假设您不想对有错误的属性进行分类)
var model=await currentDocument.GetSemanticModelAsync(令牌).ConfigureAwait(false);
var attributeSpans=来自attributesInDocument中的attributeNode
让typeInfo=model.GetTypeInfo(attributeNode,token)
其中typeInfo.Type.Kind!=SymbolKind.ErrorType
选择新分类Span(attributeNode.Span,\u classificationType);
//返回扩展Classifier将着色的一组ClassifiedSpans
返回属性页;
}

我不明白,问题是什么?@svick你说得对,问题不清楚,我已经更新了问题。谢谢你的回答,很抱歉没有充分解释我的目的,我添加了一些更多的信息,我想确定当前节点是否是属性声明,如海关属性,我使用的机制没有问题,而且检查
rootNode.genderantnodesandself(),其中genderantnode.IsKind(SyntaxKind.Attribute)
也没有解决问题。
internal IEnumerable<ClassifiedSpan> GetIdentifiersInSpans(Workspace workspace, SemanticModel model, NormalizedSnapshotSpanCollection spans)
 {
    var comparer = StringComparer.InvariantCultureIgnoreCase;

    var classifiedSpans = spans.SelectMany(span =>
    {
       var textSpan = TextSpan.FromBounds(span.Start, span.End);
        return Classifier.GetClassifiedSpans(model, textSpan, workspace);
    });

    return classifiedSpans.Where(c => comparer.Compare(c.ClassificationType, "identifier") == 0);
}
public class ProviderCache
{
    public Workspace Workspace { get; private set; }
    public Document Document { get; private set; }
    public SemanticModel SemanticModel { get; private set; }
    public SyntaxNode SyntaxRoot { get; private set; }
    public ITextSnapshot Snapshot { get; private set; }

    public static async Task<ProviderCache> Resolve(ITextBuffer buffer, ITextSnapshot snapshot)
    {
        var workspace = buffer.GetWorkspace();
        var document = snapshot.GetOpenDocumentInCurrentContextWithChanges();
        if (document == null)
        {
            return null;
        }

        var semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);
        var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);
        return new ProviderCache
        {
            Workspace = workspace,
            Document = document,
            SemanticModel = semanticModel,
            SyntaxRoot = syntaxRoot,
            Snapshot = snapshot
        };
    }
}
public static async Task<IEnumerable<ClassificationSpan>> ClassifyAttributes(Document currentDocument, CancellationToken token)
{
    // Get all attribute nodes in the current document
    var rootNode = await currentDocument.GetSyntaxRootAsync(token).ConfigureAwait(false);
    var attributesInDocument = from descendantNode in rootNode.DescendantNodesAndSelf()
                               where descendantNode.IsKind(SyntaxKind.Attribute)
                               select (AttributeSyntax)descendantNode;

    // Check to see if the attribute binds to a type (I assume you do not want to classify attributes with errors)
    var model = await currentDocument.GetSemanticModelAsync(token).ConfigureAwait(false);
    var attributeSpans = from attributeNode in attributesInDocument
                         let typeInfo = model.GetTypeInfo(attributeNode, token)
                         where typeInfo.Type.Kind != SymbolKind.ErrorType
                         select new ClassificationSpan(attributeNode.Span, _classificationType);

    // returns a set of ClassifiedSpans that your extensions classifer will colorize
    return attributeSpans;
}