本文整理汇总了C#中SyntaxToken.GetAncestor方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxToken.GetAncestor方法的具体用法?C# SyntaxToken.GetAncestor怎么用?C# SyntaxToken.GetAncestor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxToken
的用法示例。
在下文中一共展示了SyntaxToken.GetAncestor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSyntax
protected override SyntaxNode GetSyntax(SyntaxToken token)
{
return token.GetAncestor<EventFieldDeclarationSyntax>()
?? token.GetAncestor<EventDeclarationSyntax>()
?? token.GetAncestor<PropertyDeclarationSyntax>()
?? token.GetAncestor<IndexerDeclarationSyntax>()
?? (SyntaxNode)token.GetAncestor<MethodDeclarationSyntax>();
}
示例2: TryGetInterpolatedStringExpression
private static InterpolatedStringExpressionSyntax TryGetInterpolatedStringExpression(
SyntaxToken token, int position)
{
if (token.IsKind(SyntaxKind.InterpolatedStringTextToken) ||
token.IsKind(SyntaxKind.InterpolatedStringEndToken) ||
IsInterpolationOpenBrace(token, position))
{
return token.GetAncestor<InterpolatedStringExpressionSyntax>();
}
return null;
}
示例3: Do
internal IList<string> Do(CancellationToken cancellationToken)
{
// First, find the containing statement. We'll want to add the expressions in this
// statement to the result.
_token = _syntaxTree.GetRoot(cancellationToken).FindToken(_position);
_parentStatement = _token.GetAncestor<StatementSyntax>();
if (_parentStatement == null)
{
return null;
}
AddRelevantExpressions(_parentStatement, _expressions, includeDeclarations: false);
AddPrecedingRelevantExpressions();
AddFollowingRelevantExpressions(cancellationToken);
AddCurrentDeclaration();
AddMethodParameters();
AddIndexerParameters();
AddCatchParameters();
AddThisExpression();
AddValueExpression();
var result = _expressions.Distinct().Where(e => e.Length > 0).ToList();
return result.Count == 0 ? null : result;
}
示例4: GetExpansionTarget
private static SyntaxNode GetExpansionTarget(SyntaxToken token)
{
// get the directly enclosing statement
var enclosingStatement = token.GetAncestors(n => n is StatementSyntax).FirstOrDefault();
// System.Func<int, int> myFunc = arg => X;
SyntaxNode possibleLambdaExpression = enclosingStatement == null
? token.GetAncestors(n => n is SimpleLambdaExpressionSyntax || n is ParenthesizedLambdaExpressionSyntax).FirstOrDefault()
: null;
if (possibleLambdaExpression != null)
{
var lambdaExpression = ((LambdaExpressionSyntax)possibleLambdaExpression);
if (lambdaExpression.Body is ExpressionSyntax)
{
return lambdaExpression.Body;
}
}
// int M() => X;
var possibleArrowExpressionClause = enclosingStatement == null
? token.GetAncestors<ArrowExpressionClauseSyntax>().FirstOrDefault()
: null;
if (possibleArrowExpressionClause != null)
{
return possibleArrowExpressionClause.Expression;
}
var enclosingNameMemberCrefOrnull = token.GetAncestors(n => n is NameMemberCrefSyntax).LastOrDefault();
if (enclosingNameMemberCrefOrnull != null)
{
if (token.Parent is TypeSyntax && token.Parent.Parent is TypeSyntax)
{
enclosingNameMemberCrefOrnull = null;
}
}
var enclosingXmlNameAttr = token.GetAncestors(n => n is XmlNameAttributeSyntax).FirstOrDefault();
if (enclosingXmlNameAttr != null)
{
return null;
}
var enclosingInitializer = token.GetAncestors<EqualsValueClauseSyntax>().FirstOrDefault();
if (enclosingStatement == null && enclosingInitializer != null && enclosingInitializer.Parent is VariableDeclaratorSyntax)
{
return enclosingInitializer.Value;
}
var attributeSyntax = token.GetAncestor<AttributeSyntax>();
if (attributeSyntax != null)
{
return attributeSyntax;
}
// there seems to be no statement above this one. Let's see if we can at least get an SimpleNameSyntax
return enclosingStatement ?? enclosingNameMemberCrefOrnull ?? token.GetAncestors(n => n is SimpleNameSyntax).FirstOrDefault();
}
示例5: GetTagsForMethod
private IEnumerable<CompletionItem> GetTagsForMethod(IMethodSymbol symbol, TextSpan itemSpan, DocumentationCommentTriviaSyntax trivia, SyntaxToken token)
{
var items = new List<CompletionItem>();
var parameters = symbol.GetParameters().Select(p => p.Name).ToSet();
var typeParameters = symbol.TypeParameters.Select(t => t.Name).ToSet();
// User is trying to write a name, try to suggest only names.
if (token.Parent.IsKind(SyntaxKind.XmlNameAttribute) ||
(token.Parent.IsKind(SyntaxKind.IdentifierName) && token.Parent.IsParentKind(SyntaxKind.XmlNameAttribute)))
{
string parentElementName = null;
var emptyElement = token.GetAncestor<XmlEmptyElementSyntax>();
if (emptyElement != null)
{
parentElementName = emptyElement.Name.LocalName.Text;
}
// We're writing the name of a paramref or typeparamref
if (parentElementName == ParamRefTagName)
{
items.AddRange(parameters.Select(p => CreateCompletionItem(itemSpan, p)));
}
else if (parentElementName == TypeParamRefTagName)
{
items.AddRange(typeParameters.Select(t => CreateCompletionItem(itemSpan, t)));
}
return items;
}
RemoveExistingTags(trivia, parameters, x => AttributeSelector(x, ParamTagName));
RemoveExistingTags(trivia, typeParameters, x => AttributeSelector(x, TypeParamTagName));
items.AddRange(parameters.Select(p => CreateCompletionItem(itemSpan, FormatParameter(ParamTagName, p))));
items.AddRange(typeParameters.Select(t => CreateCompletionItem(itemSpan, FormatParameter(TypeParamTagName, t))));
// Provide a return completion item in case the function returns something
var returns = true;
foreach (var node in trivia.Content)
{
var element = node as XmlElementSyntax;
if (element != null && !element.StartTag.IsMissing && !element.EndTag.IsMissing)
{
var startTag = element.StartTag;
if (startTag.Name.LocalName.ValueText == ReturnsTagName)
{
returns = false;
break;
}
}
}
if (returns && !symbol.ReturnsVoid)
{
items.Add(CreateCompletionItem(itemSpan, ReturnsTagName));
}
return items;
}
示例6: GetTagsForProperty
private IEnumerable<CompletionItem> GetTagsForProperty(IPropertySymbol symbol, TextSpan itemSpan, DocumentationCommentTriviaSyntax trivia, SyntaxToken token)
{
var items = new List<CompletionItem>();
if (symbol.IsIndexer)
{
var parameters = symbol.GetParameters().Select(p => p.Name).ToSet();
// User is trying to write a name, try to suggest only names.
if (token.Parent.IsKind(SyntaxKind.XmlNameAttribute) ||
(token.Parent.IsKind(SyntaxKind.IdentifierName) && token.Parent.IsParentKind(SyntaxKind.XmlNameAttribute)))
{
string parentElementName = null;
var emptyElement = token.GetAncestor<XmlEmptyElementSyntax>();
if (emptyElement != null)
{
parentElementName = emptyElement.Name.LocalName.Text;
}
// We're writing the name of a paramref
if (parentElementName == ParamRefTagName)
{
items.AddRange(parameters.Select(p => CreateCompletionItem(itemSpan, p)));
}
return items;
}
RemoveExistingTags(trivia, parameters, x => AttributeSelector(x, ParamTagName));
items.AddRange(parameters.Select(p => CreateCompletionItem(itemSpan, FormatParameter(ParamTagName, p))));
}
var typeParameters = symbol.GetTypeArguments().Select(p => p.Name).ToSet();
items.AddRange(typeParameters.Select(t => CreateCompletionItem(itemSpan, TypeParamTagName, NameAttributeName, t)));
items.Add(CreateCompletionItem(itemSpan, "value"));
return items;
}
示例7: GetExpansionTarget
private static SyntaxNode GetExpansionTarget(SyntaxToken token)
{
// get the directly enclosing statement
var enclosingStatement = token.GetAncestors(n => n is StatementSyntax).FirstOrDefault();
// see if there's an enclosing lambda expression
SyntaxNode possibleLambdaExpression = enclosingStatement == null
? token.GetAncestors(n => n is SimpleLambdaExpressionSyntax || n is ParenthesizedLambdaExpressionSyntax).FirstOrDefault()
: null;
var enclosingNameMemberCrefOrnull = token.GetAncestors(n => n is NameMemberCrefSyntax).LastOrDefault();
if (enclosingNameMemberCrefOrnull != null)
{
if (token.Parent is TypeSyntax && token.Parent.Parent is TypeSyntax)
{
enclosingNameMemberCrefOrnull = null;
}
}
var enclosingXmlNameAttr = token.GetAncestors(n => n is XmlNameAttributeSyntax).FirstOrDefault();
if (enclosingXmlNameAttr != null)
{
return null;
}
var enclosingInitializer = token.GetAncestors<EqualsValueClauseSyntax>().FirstOrDefault();
if (enclosingStatement == null && enclosingInitializer != null && enclosingInitializer.Parent is VariableDeclaratorSyntax)
{
return enclosingInitializer.Value;
}
var attributeSyntax = token.GetAncestor<AttributeSyntax>();
if (attributeSyntax != null)
{
return attributeSyntax;
}
// there seems to be no statement above this one. Let's see if we can at least get an SimpleNameSyntax
return enclosingStatement ?? enclosingNameMemberCrefOrnull ?? token.GetAncestors(n => n is SimpleNameSyntax).FirstOrDefault();
}
示例8: GetAdjustNewLinesOperation
public override AdjustNewLinesOperation GetAdjustNewLinesOperation(SyntaxToken previousToken, SyntaxToken currentToken, OptionSet optionSet, NextOperation<AdjustNewLinesOperation> nextOperation)
{
if (previousToken.IsNestedQueryExpression())
{
return CreateAdjustNewLinesOperation(0, AdjustNewLinesOption.PreserveLines);
}
// skip the very first from keyword
if (currentToken.IsFirstFromKeywordInExpression())
{
return nextOperation.Invoke();
}
switch (currentToken.Kind())
{
case SyntaxKind.FromKeyword:
case SyntaxKind.WhereKeyword:
case SyntaxKind.LetKeyword:
case SyntaxKind.JoinKeyword:
case SyntaxKind.OrderByKeyword:
case SyntaxKind.GroupKeyword:
case SyntaxKind.SelectKeyword:
if (currentToken.GetAncestor<QueryExpressionSyntax>() != null)
{
if (optionSet.GetOption(CSharpFormattingOptions.NewLineForClausesInQuery))
{
return CreateAdjustNewLinesOperation(1, AdjustNewLinesOption.PreserveLines);
}
else
{
return CreateAdjustNewLinesOperation(0, AdjustNewLinesOption.PreserveLines);
}
}
break;
}
return nextOperation.Invoke();
}
示例9: IsLeftSideOfUsingAliasDirective
private static bool IsLeftSideOfUsingAliasDirective(SyntaxToken leftToken, CancellationToken cancellationToken)
{
var usingDirective = leftToken.GetAncestor<UsingDirectiveSyntax>();
if (usingDirective != null)
{
// No = token:
if (usingDirective.Alias == null || usingDirective.Alias.EqualsToken.IsMissing)
{
return true;
}
return leftToken.SpanStart < usingDirective.Alias.EqualsToken.SpanStart;
}
return false;
}
示例10: LineBreaksAfter
// copied from compiler formatter to have same base forced format
private int LineBreaksAfter(SyntaxToken previousToken, SyntaxToken currentToken)
{
if (currentToken.Kind() == SyntaxKind.None)
{
return 0;
}
switch (previousToken.Kind())
{
case SyntaxKind.None:
return 0;
case SyntaxKind.OpenBraceToken:
case SyntaxKind.FinallyKeyword:
return 1;
case SyntaxKind.CloseBraceToken:
return LineBreaksAfterCloseBrace(currentToken);
case SyntaxKind.CloseParenToken:
return (((previousToken.Parent is StatementSyntax) && currentToken.Parent != previousToken.Parent)
|| currentToken.Kind() == SyntaxKind.OpenBraceToken) ? 1 : 0;
case SyntaxKind.CloseBracketToken:
if (previousToken.Parent is AttributeListSyntax)
{
// Assembly and module-level attributes followed by non-attributes should have
// a blank line after them.
var parent = (AttributeListSyntax)previousToken.Parent;
if (parent.Target != null &&
(parent.Target.Identifier.IsKindOrHasMatchingText(SyntaxKind.AssemblyKeyword) ||
parent.Target.Identifier.IsKindOrHasMatchingText(SyntaxKind.ModuleKeyword)))
{
if (!(currentToken.Parent is AttributeListSyntax))
{
return 2;
}
}
if (previousToken.GetAncestor<ParameterSyntax>() == null)
{
return 1;
}
}
break;
case SyntaxKind.SemicolonToken:
return LineBreaksAfterSemicolon(previousToken, currentToken);
case SyntaxKind.CommaToken:
return previousToken.Parent is EnumDeclarationSyntax ? 1 : 0;
case SyntaxKind.ElseKeyword:
return currentToken.Kind() != SyntaxKind.IfKeyword ? 1 : 0;
case SyntaxKind.ColonToken:
if (previousToken.Parent is LabeledStatementSyntax || previousToken.Parent is SwitchLabelSyntax)
{
return 1;
}
break;
}
if ((currentToken.Kind() == SyntaxKind.FromKeyword && currentToken.Parent.Kind() == SyntaxKind.FromClause) ||
(currentToken.Kind() == SyntaxKind.LetKeyword && currentToken.Parent.Kind() == SyntaxKind.LetClause) ||
(currentToken.Kind() == SyntaxKind.WhereKeyword && currentToken.Parent.Kind() == SyntaxKind.WhereClause) ||
(currentToken.Kind() == SyntaxKind.JoinKeyword && currentToken.Parent.Kind() == SyntaxKind.JoinClause) ||
(currentToken.Kind() == SyntaxKind.JoinKeyword && currentToken.Parent.Kind() == SyntaxKind.JoinIntoClause) ||
(currentToken.Kind() == SyntaxKind.OrderByKeyword && currentToken.Parent.Kind() == SyntaxKind.OrderByClause) ||
(currentToken.Kind() == SyntaxKind.SelectKeyword && currentToken.Parent.Kind() == SyntaxKind.SelectClause) ||
(currentToken.Kind() == SyntaxKind.GroupKeyword && currentToken.Parent.Kind() == SyntaxKind.GroupClause))
{
return 1;
}
switch (currentToken.Kind())
{
case SyntaxKind.OpenBraceToken:
case SyntaxKind.CloseBraceToken:
case SyntaxKind.ElseKeyword:
case SyntaxKind.FinallyKeyword:
return 1;
case SyntaxKind.OpenBracketToken:
if (currentToken.Parent is AttributeListSyntax)
{
// Assembly and module-level attributes preceded by non-attributes should have
// a blank line separating them.
var parent = (AttributeListSyntax)currentToken.Parent;
if (parent.Target != null)
{
if (parent.Target.Identifier == SyntaxFactory.Token(SyntaxKind.AssemblyKeyword) ||
parent.Target.Identifier == SyntaxFactory.Token(SyntaxKind.ModuleKeyword))
{
if (!(previousToken.Parent is AttributeListSyntax))
{
return 2;
//.........这里部分代码省略.........
示例11: TryGetTextForKeyword
private bool TryGetTextForKeyword(SyntaxToken token, Document document, ISyntaxFactsService syntaxFacts, out string text)
{
if (token.Kind() == SyntaxKind.InKeyword)
{
if (token.GetAncestor<FromClauseSyntax>() != null)
{
text = "from_CSharpKeyword";
return true;
}
if (token.GetAncestor<JoinClauseSyntax>() != null)
{
text = "join_CSharpKeyword";
return true;
}
}
if (token.IsKeyword())
{
text = Keyword(token.Text);
return true;
}
if (token.ValueText == "var" && token.IsKind(SyntaxKind.IdentifierToken) &&
token.Parent.Parent is VariableDeclarationSyntax && token.Parent == ((VariableDeclarationSyntax)token.Parent.Parent).Type)
{
text = "var_CSharpKeyword";
return true;
}
if (syntaxFacts.IsTypeNamedDynamic(token, token.Parent))
{
text = "dynamic_CSharpKeyword";
return true;
}
text = null;
return false;
}
示例12: TryGetTextForPreProcessor
private bool TryGetTextForPreProcessor(SyntaxToken token, Document document, ISyntaxFactsService syntaxFacts, out string text)
{
if (syntaxFacts.IsPreprocessorKeyword(token))
{
text = "#" + token.Text;
return true;
}
if (token.IsKind(SyntaxKind.EndOfDirectiveToken) && token.GetAncestor<RegionDirectiveTriviaSyntax>() != null)
{
text = "#region";
return true;
}
text = null;
return false;
}
示例13: TryGetTextForSymbol
private bool TryGetTextForSymbol(SyntaxToken token, SemanticModel semanticModel, Document document, CancellationToken cancellationToken, out string text)
{
ISymbol symbol;
if (token.Parent is TypeArgumentListSyntax)
{
var genericName = token.GetAncestor<GenericNameSyntax>();
symbol = semanticModel.GetSymbolInfo(genericName, cancellationToken).Symbol ?? semanticModel.GetTypeInfo(genericName, cancellationToken).Type;
}
else if (token.Parent is NullableTypeSyntax && token.IsKind(SyntaxKind.QuestionToken))
{
text = "System.Nullable`1";
return true;
}
else
{
var symbols = semanticModel.GetSymbols(token, document.Project.Solution.Workspace, bindLiteralsToUnderlyingType: true, cancellationToken: cancellationToken);
symbol = symbols.FirstOrDefault();
if (symbol == null)
{
var bindableParent = document.GetLanguageService<ISyntaxFactsService>().GetBindableParent(token);
var overloads = semanticModel.GetMemberGroup(bindableParent);
symbol = overloads.FirstOrDefault();
}
}
// Local: return the name if it's the declaration, otherwise the type
if (symbol is ILocalSymbol && !symbol.DeclaringSyntaxReferences.Any(d => d.GetSyntax().DescendantTokens().Contains(token)))
{
symbol = ((ILocalSymbol)symbol).Type;
}
// Range variable: use the type
if (symbol is IRangeVariableSymbol)
{
var info = semanticModel.GetTypeInfo(token.Parent, cancellationToken);
symbol = info.Type;
}
// Just use syntaxfacts for operators
if (symbol is IMethodSymbol && ((IMethodSymbol)symbol).MethodKind == MethodKind.BuiltinOperator)
{
text = null;
return false;
}
text = symbol != null ? Format(symbol) : null;
return symbol != null;
}
示例14: GetTagsForMethod
private IEnumerable<CompletionItem> GetTagsForMethod(IMethodSymbol symbol, TextSpan filterSpan, DocumentationCommentTriviaSyntax trivia, SyntaxToken token)
{
var items = new List<CompletionItem>();
var parameters = symbol.GetParameters().Select(p => p.Name).ToSet();
var typeParameters = symbol.TypeParameters.Select(t => t.Name).ToSet();
// User is trying to write a name, try to suggest only names.
if (token.Parent.IsKind(SyntaxKind.XmlNameAttribute) ||
(token.Parent.IsKind(SyntaxKind.IdentifierName) && token.Parent.IsParentKind(SyntaxKind.XmlNameAttribute)))
{
string parentElementName = null;
var emptyElement = token.GetAncestor<XmlEmptyElementSyntax>();
if (emptyElement != null)
{
parentElementName = emptyElement.Name.LocalName.Text;
}
// We're writing the name of a paramref or typeparamref
if (parentElementName == "paramref")
{
items.AddRange(parameters.Select(p => new XmlDocCommentCompletionItem(this, filterSpan, p, GetCompletionItemRules())));
}
else if (parentElementName == "typeparamref")
{
items.AddRange(typeParameters.Select(t => new XmlDocCommentCompletionItem(this, filterSpan, t, GetCompletionItemRules())));
}
return items;
}
var returns = true;
RemoveExistingTags(trivia, parameters, x => AttributeSelector(x, "param"));
RemoveExistingTags(trivia, typeParameters, x => AttributeSelector(x, "typeparam"));
foreach (var node in trivia.Content)
{
var element = node as XmlElementSyntax;
if (element != null && !element.StartTag.IsMissing && !element.EndTag.IsMissing)
{
var startTag = element.StartTag;
if (startTag.Name.LocalName.ValueText == "returns")
{
returns = false;
break;
}
}
}
items.AddRange(parameters.Select(p => new XmlDocCommentCompletionItem(this, filterSpan, FormatParameter("param", p), GetCompletionItemRules())));
items.AddRange(typeParameters.Select(t => new XmlDocCommentCompletionItem(this, filterSpan, FormatParameter("typeparam", t), GetCompletionItemRules())));
if (returns && !symbol.ReturnsVoid)
{
items.Add(new XmlDocCommentCompletionItem(this, filterSpan, "returns", GetCompletionItemRules()));
}
return items;
}
示例15: IsValidElseKeywordContext
private static bool IsValidElseKeywordContext(SyntaxToken token)
{
var statement = token.GetAncestor<StatementSyntax>();
var ifStatement = statement.GetAncestorOrThis<IfStatementSyntax>();
if (statement == null || ifStatement == null)
return false;
// cases:
// if (foo)
// Console.WriteLine();
// |
// if (foo)
// Console.WriteLine();
// e|
if (token.IsKind(SyntaxKind.SemiToken) && ifStatement.Statement.GetLastToken(includeSkippedTokens: true) == token)
return true;
// if (foo) {
// Console.WriteLine();
// } |
// if (foo) {
// Console.WriteLine();
// } e|
if (token.IsKind(SyntaxKind.CloseBraceToken) && ifStatement.Statement is BlockSyntax && token == ((BlockSyntax) ifStatement.Statement).CloseBraceToken)
return true;
return false;
}