本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.GetLocation方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetLocation方法的具体用法?C# SyntaxNode.GetLocation怎么用?C# SyntaxNode.GetLocation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetLocation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRootNodeOfLine
private static SyntaxNode GetRootNodeOfLine(SyntaxNode node) {
int nodeLocationLine = node.GetLocation().GetLineSpan().StartLinePosition.Line;
var ancestors = node.Ancestors();
var nodeLocationCharacter = ancestors.Where(ancestor => GetSpanLocation(ancestor).Start.Line == nodeLocationLine)
.Min(ancestor => GetSpanLocation(ancestor).Start.Character);
return ancestors.Last(ancestor => GetSpanLocation(ancestor).Start == new LinePosition(nodeLocationLine, nodeLocationCharacter));
}
开发者ID:vuder,项目名称:CodingStandardCodeAnalyzers,代码行数:7,代码来源:TimeMeasurementCodeAnalyzerCodeFixProvider.cs
示例2: GetTypeSymbols
public static IEnumerable<NamedSymbol> GetTypeSymbols(this SemanticModel semanticModel, SyntaxNode node)
{
var typesymbols = semanticModel.LookupSymbols(node.SpanStart)
.OfType<ILocalSymbol>()
.Where(x => x.Locations.Any() && x.Locations.First().GetLineSpan().StartLinePosition < node.GetLocation().GetLineSpan().StartLinePosition)
.Select(x => new NamedSymbol( x.Name, x.Type))
.Concat(
semanticModel.LookupSymbols(node.SpanStart)
.OfType<IParameterSymbol>()
.Select(x => new NamedSymbol( x.Name, x.Type))
).Concat(
semanticModel.LookupSymbols(node.SpanStart)
.OfType<IPropertySymbol>()
.Select(x => new NamedSymbol( x.Name, x.Type))
).Concat(
semanticModel.LookupSymbols(node.SpanStart)
.OfType<IFieldSymbol>()
.Select(x => new NamedSymbol( x.Name, x.Type))
);
if (!semanticModel.GetEnclosingSymbol(node.SpanStart).IsStatic)
{
var classDeclaration = node.Ancestors().OfType<ClassDeclarationSyntax>().FirstOrDefault();
if(classDeclaration!=null)
{
typesymbols = typesymbols.Concat(new NamedSymbol[]
{
new NamedSymbol( "this",(ITypeSymbol)semanticModel.GetDeclaredSymbol(classDeclaration))
});
}
}
return typesymbols;
}
示例3: Evaluate
public EvaluationResult Evaluate(SyntaxNode node)
{
var result = EvaluateImpl(node);
if (result != null)
{
var sourceTree = node.GetLocation().SourceTree;
var filePath = sourceTree.FilePath;
var typeDefinition = GetNodeType(node);
var unitNamespace = GetNamespace(node);
if (result.ErrorCount == 0)
{
result.ErrorCount = 1;
}
if (result.LinesOfCodeAffected <= 0)
{
result.LinesOfCodeAffected = GetLinesOfCode(node);
}
result.Namespace = unitNamespace;
result.TypeKind = typeDefinition.Item1;
result.TypeName = typeDefinition.Item2;
result.Title = Title;
result.Suggestion = Suggestion;
result.Quality = Quality;
result.QualityAttribute = QualityAttribute;
result.ImpactLevel = ImpactLevel;
result.FilePath = filePath;
}
return result;
}
示例4: TryReportDiagnostic
private static void TryReportDiagnostic(SyntaxNodeAnalysisContext context, SyntaxNode parent, SyntaxNode node)
{
var type = context.SemanticModel.GetTypeInfo(node).Type;
if (type.ContainingNamespace.ToString() == "System.Threading.Tasks" && type.Name == "Task")
{
context.ReportDiagnostic(Diagnostic.Create(Rule, parent.GetLocation()));
}
}
示例5: Check
void Check(SyntaxNode n)
{
var info = nodeContext.SemanticModel.GetSymbolInfo(n);
var symbol = info.Symbol;
if (symbol == null || symbol.ContainingType.Locations.Where(loc => loc.IsInSource && loc.SourceTree.FilePath == type.SyntaxTree.FilePath).All(loc => !type.Span.Contains(loc.SourceSpan)))
return;
if (symbol.IsVirtual || symbol.IsAbstract || symbol.IsOverride)
{
Diagnostics.Add(Diagnostic.Create(descriptor, n.GetLocation()));
}
}
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:11,代码来源:DoNotCallOverridableMethodsInConstructorAnalyzer.cs
示例6: CheckNonOverridenMethodOnStruct
private static void CheckNonOverridenMethodOnStruct(IMethodSymbol methodInfo, Action<Diagnostic> reportDiagnostic, SyntaxNode node, string filePath)
{
if (methodInfo.ContainingType != null)
{
// hack? Hmmm.
var containingType = methodInfo.ContainingType.ToString();
if (string.Equals(containingType, "System.ValueType", StringComparison.OrdinalIgnoreCase) || string.Equals(containingType, "System.Enum", StringComparison.OrdinalIgnoreCase))
{
reportDiagnostic(Diagnostic.Create(ValueTypeNonOverridenCallRule, node.GetLocation(), EmptyMessageArgs));
HeapAllocationAnalyzerEventSource.Logger.NonOverridenVirtualMethodCallOnValueType(filePath);
}
}
}
开发者ID:Maxwe11,项目名称:RoslynClrHeapAllocationAnalyzer,代码行数:13,代码来源:CallSiteImplicitAllocationAnalyzer.cs
示例7: AddAccessor
private async Task<Document> AddAccessor(Document document, SyntaxNode parameter, CancellationToken cancellationToken)
{
SymbolEditor symbolEditor = SymbolEditor.Create(document);
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var parameterSymbol = model.GetDeclaredSymbol(parameter, cancellationToken) as IParameterSymbol;
if (parameterSymbol == null)
{
return document;
}
// Make the first character uppercase since we are generating a property.
var propName = char.ToUpper(parameterSymbol.Name[0]).ToString() + parameterSymbol.Name.Substring(1);
var typeSymbol = parameterSymbol.ContainingType;
var propertySymbol = typeSymbol.GetMembers(propName).Where(m => m.Kind == SymbolKind.Property).FirstOrDefault();
// Add a new property
if (propertySymbol == null)
{
await symbolEditor.EditOneDeclarationAsync(typeSymbol,
parameter.GetLocation(), // edit the partial declaration that has this parameter symbol.
(editor, typeDeclaration) =>
{
var newProperty = editor.Generator.PropertyDeclaration(propName,
editor.Generator.TypeExpression(parameterSymbol.Type),
Accessibility.Public,
DeclarationModifiers.ReadOnly);
newProperty = editor.Generator.WithGetAccessorStatements(newProperty, null);
editor.AddMember(typeDeclaration, newProperty);
},
cancellationToken).ConfigureAwait(false);
}
else
{
await symbolEditor.EditOneDeclarationAsync(propertySymbol,
(editor, propertyDeclaration) =>
{
editor.SetGetAccessorStatements(propertyDeclaration, null);
editor.SetModifiers(propertyDeclaration, editor.Generator.GetModifiers(propertyDeclaration) - DeclarationModifiers.WriteOnly);
},
cancellationToken).ConfigureAwait(false);
}
return symbolEditor.GetChangedDocuments().First();
}
示例8: Evaluate
public async Task<EvaluationResult> Evaluate(SyntaxNode node, SemanticModel semanticModel, Solution solution)
{
if (semanticModel == null || solution == null)
{
return null;
}
var result = await EvaluateImpl(node, semanticModel, solution).ConfigureAwait(false);
if (result == null)
{
return null;
}
var sourceTree = node.GetLocation().SourceTree;
var filePath = sourceTree.FilePath;
var typeDefinition = GetNodeType(node);
var unitNamespace = GetNamespace(node);
if (result.ErrorCount == 0)
{
result.ErrorCount = 1;
}
if (result.LinesOfCodeAffected <= 0)
{
result.LinesOfCodeAffected = GetLinesOfCode(node);
}
result.Namespace = unitNamespace;
result.TypeKind = typeDefinition.Item1;
result.TypeName = typeDefinition.Item2;
result.Title = Title;
result.Suggestion = Suggestion;
result.Quality = Quality;
result.QualityAttribute = QualityAttribute;
result.ImpactLevel = ImpactLevel;
result.FilePath = filePath;
return result;
}
示例9: GenerateConstructor
private async Task<Document> GenerateConstructor(Document document, SyntaxNode node, ISymbol symbol, INamedTypeSymbol notImplementedExceptionType, CancellationToken cancellationToken)
{
SymbolEditor editor = SymbolEditor.Create(document);
var typeSymbol = symbol as INamedTypeSymbol;
await editor.EditOneDeclarationAsync(typeSymbol, node.GetLocation(), (docEditor, declaration) =>
{
SyntaxGenerator generator = docEditor.Generator;
SyntaxNode throwStatement = generator.ThrowStatement(generator.ObjectCreationExpression(generator.TypeExpression(notImplementedExceptionType)));
SyntaxNode ctorDecl = generator.ConstructorDeclaration(
typeSymbol.Name,
new[]
{
generator.ParameterDeclaration("serializationInfo", generator.TypeExpression(WellKnownTypes.SerializationInfo(docEditor.SemanticModel.Compilation))),
generator.ParameterDeclaration("streamingContext", generator.TypeExpression(WellKnownTypes.StreamingContext(docEditor.SemanticModel.Compilation)))
},
typeSymbol.IsSealed ? Accessibility.Private : Accessibility.Protected,
statements: new[] { throwStatement });
docEditor.AddMember(declaration, ctorDecl);
}, cancellationToken).ConfigureAwait(false);
return editor.GetChangedDocuments().First();
}
示例10: CheckConstant
static bool CheckConstant(SyntaxNodeAnalysisContext nodeContext, SyntaxNode expr, ref Diagnostic diagnostic)
{
var rr = nodeContext.SemanticModel.GetConstantValue(expr);
if (rr.HasValue && rr.Value is bool)
{
var result = (bool)rr.Value;
diagnostic = Diagnostic.Create(
descriptor,
expr.GetLocation(),
result ? "true" : "false"
);
return true;
}
return false;
}
示例11: AddConstants
public void AddConstants(SyntaxNode attribute, SemanticModel semanticModel, EventSourceTypeInfo eventSourceTypeInfo)
{
if (attribute == null)
throw new ArgumentNullException("attribute", "attribute is null.");
AttributeSyntax attributeSyntax;
AttributeListSyntax attributeListSyntax = attribute as AttributeListSyntax;
if (attributeListSyntax != null)
{
if (attributeListSyntax.Attributes.Count != 1)
throw new GenerationException(attribute.GetLocation(), "Expected a single attribute in attribute list, but either none or more than one were found.");
attributeSyntax = attributeListSyntax.Attributes.Single();
}
else
{
attributeSyntax = attribute as AttributeSyntax;
if (attributeSyntax == null)
throw new GenerationException($"SyntaxNode was not of expected type {typeof(AttributeSyntax).FullName}");
}
AddToDictionary(attributeSyntax, "Keywords", m_keywords, semanticModel, eventSourceTypeInfo.EventKeywordsType);
AddToDictionary(attributeSyntax, "Opcode", m_opcodes, semanticModel, eventSourceTypeInfo.EventOpcodeType);
AddToDictionary(attributeSyntax, "Task", m_tasks, semanticModel, eventSourceTypeInfo.EventTaskType);
}
开发者ID:modulexcite,项目名称:EventSourceGenerator,代码行数:25,代码来源:EventSourceGenerator.CollectedGenerationInfo.cs
示例12: AnalyzeArrayLength
protected static void AnalyzeArrayLength(int length, SyntaxNode arrayCreationExpression, Action<Diagnostic> addDiagnostic)
{
if (length == 0)
{
addDiagnostic(Diagnostic.Create(UseEmptyEnumerableRule, arrayCreationExpression.GetLocation()));
}
else if (length == 1)
{
addDiagnostic(Diagnostic.Create(UseSingletonEnumerableRule, arrayCreationExpression.GetLocation()));
}
}
示例13: GetUpdatedDocumentAsync
public virtual async Task<Document> GetUpdatedDocumentAsync(
Document document,
IList<Tuple<INamedTypeSymbol, IList<ISymbol>>> unimplementedMembers,
INamedTypeSymbol classOrStructType,
SyntaxNode classOrStructDecl,
CancellationToken cancellationToken)
{
var result = document;
var compilation = await result.Project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);
var memberDefinitions = GenerateMembers(
compilation,
unimplementedMembers,
cancellationToken);
result = await CodeGenerator.AddMemberDeclarationsAsync(
result.Project.Solution, classOrStructType, memberDefinitions,
new CodeGenerationOptions(contextLocation: classOrStructDecl.GetLocation()),
cancellationToken).ConfigureAwait(false);
return result;
}
示例14: CheckCall
private static void CheckCall(SyntaxNode node, ArgumentListSyntax argumentList, SyntaxNodeAnalysisContext context)
{
var invokedMethodSymbol = context.SemanticModel.GetSymbolInfo(node).Symbol as IMethodSymbol;
if (invokedMethodSymbol == null ||
!invokedMethodSymbol.Parameters.Any() ||
!invokedMethodSymbol.Parameters.Last().IsParams ||
argumentList == null)
{
return;
}
if (IsInvocationWithExplicitArray(argumentList, invokedMethodSymbol, context.SemanticModel))
{
return;
}
var argumentTypes = argumentList.Arguments
.Select(arg => context.SemanticModel.GetTypeInfo(arg.Expression).Type)
.ToList();
if (argumentTypes.Any(type => type is IErrorTypeSymbol))
{
return;
}
var possibleOtherMethods = invokedMethodSymbol.ContainingType.GetMembers(invokedMethodSymbol.Name)
.OfType<IMethodSymbol>()
.Where(m => !m.IsVararg)
.Where(m => m.MethodKind == invokedMethodSymbol.MethodKind)
.Where(m => !invokedMethodSymbol.Equals(m))
.Where(m => m.Parameters.Any() && !m.Parameters.Last().IsParams);
var otherMethod = possibleOtherMethods.FirstOrDefault(possibleOtherMethod =>
ArgumentsMatchParameters(
argumentList,
argumentTypes.Select(t => t as INamedTypeSymbol).ToList(),
possibleOtherMethod,
context.SemanticModel));
if (otherMethod != null)
{
context.ReportDiagnostic(Diagnostic.Create(
Rule,
node.GetLocation(),
otherMethod.ToMinimalDisplayString(context.SemanticModel, node.SpanStart)));
}
}
示例15: HasNonTrailingExitSelects
internal bool HasNonTrailingExitSelects(SyntaxNode node, ExitStatementSyntax trailing)
{
var exit = node as ExitStatementSyntax;
if ((exit != null && exit.BlockKeyword.IsKind(SyntaxKind.SelectKeyword)) && (trailing == null || !node.GetLocation().Equals(trailing.GetLocation())))
return true;
return node.DescendantNodes().Any(n => HasNonTrailingExitSelects(n, trailing));
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:7,代码来源:ConvertSelectCaseToIfCodeRefactoringProvider.cs