本文整理汇总了C#中Microsoft.CodeAnalysis.SyntaxNode.FindToken方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.FindToken方法的具体用法?C# SyntaxNode.FindToken怎么用?C# SyntaxNode.FindToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.FindToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryFix
public override bool TryFix (ConstructFixer fixer, SyntaxNode syntaxTree, Ide.Gui.Document document, int location, ref int newOffset)
{
foreach (var invocationExpression in syntaxTree.FindToken (location).Parent.AncestorsAndSelf ().OfType<InvocationExpressionSyntax> ()) {
if (invocationExpression != null) {
if (!invocationExpression.ArgumentList.OpenParenToken.IsMissing && invocationExpression.ArgumentList.CloseParenToken.IsMissing) {
var insertionOffset = invocationExpression.Span.End - 1;
newOffset = insertionOffset;
var text = ")";
newOffset++;
var expressionStatement = invocationExpression.Parent as ExpressionStatementSyntax;
if (expressionStatement != null) {
if (expressionStatement.SemicolonToken.IsMissing)
text = ");";
newOffset++;
}
document.Editor.InsertText (insertionOffset, text);
return true;
}
}
}
return false;
}
示例2: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
var updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space))));
return Task.FromResult(updatedDocument);
}
示例3: RegisterCodeFixesAsync
protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var paramsToken = root.FindToken(diagnosticSpan.Start);
if (!paramsToken.IsKind(SyntaxKind.ParamsKeyword))
{
return;
}
context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var node = paramsToken.Parent;
var newNode = node.ReplaceToken(
paramsToken,
SyntaxFactory.Token(SyntaxKind.None));
newNode = newNode.WithTriviaFrom(node);
var newRoot = root.ReplaceNode(node, newNode);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
context.Diagnostics);
}
示例4: GetTypeSyntaxToReplace
private static TypeSyntax GetTypeSyntaxToReplace(Diagnostic diagnostic, SyntaxNode root) {
var diagnosticSpan = diagnostic.Location.SourceSpan;
var nodeCandidates = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf();
return nodeCandidates.OfType<VariableDeclarationSyntax>().FirstOrDefault()?.Type
?? nodeCandidates.OfType<ParameterSyntax>().FirstOrDefault()?.Type;
}
示例5: GetApplicableUsings
private static IEnumerable<UsingDirectiveSyntax> GetApplicableUsings(int position, SyntaxNode root)
{
var namespaceUsings = root.FindToken(position).Parent.GetAncestors<NamespaceDeclarationSyntax>().SelectMany(n => n.Usings);
var allUsings = root is CompilationUnitSyntax
? ((CompilationUnitSyntax)root).Usings.Concat(namespaceUsings)
: namespaceUsings;
return allUsings.Where(u => u.Alias != null);
}
示例6: GetCastNode
// public sealed override FixAllProvider GetFixAllProvider()
// {
// return RemoveUnnecessaryCastFixAllProvider.Instance;
// }
//
private static CastExpressionSyntax GetCastNode(SyntaxNode root, SemanticModel model, TextSpan span, CancellationToken cancellationToken)
{
var token = root.FindToken(span.Start);
if (!token.Span.IntersectsWith(span))
{
return null;
}
return token.GetAncestors<CastExpressionSyntax>()
.FirstOrDefault(c => c.Span.IntersectsWith(span) && c.IsUnnecessaryCast(model, cancellationToken));
}
示例7: TryGetNode
private bool TryGetNode(SyntaxNode root, Microsoft.CodeAnalysis.Text.TextSpan span, out SyntaxNode node)
{
node = null;
var ancestors = root.FindToken(span.Start).GetAncestors<SyntaxNode>();
if (!ancestors.Any())
{
return false;
}
node = ancestors.FirstOrDefault(n => n.Span.Contains(span) && n != root);
return node != null;
}
示例8: GetActions
public IEnumerable<CodeAction> GetActions(Document document, SemanticModel model, SyntaxNode root, TextSpan span)
{
var token = root.FindToken(span.Start);
var lambdaExpression = token.GetAncestor<LambdaExpressionSyntax>();
if ((lambdaExpression != null) && (lambdaExpression.ArrowToken.FullSpan.Contains(span)))
{
if (ContainsLocalReferences(model, lambdaExpression, lambdaExpression.Body))
yield break;
var lambdaSymbol = model.GetSymbolInfo(lambdaExpression).Symbol as IMethodSymbol;
if (lambdaSymbol == null)
yield break;
bool noReturn = false;
BlockSyntax body;
if (lambdaExpression.Body is BlockSyntax)
{
body = SyntaxFactory.Block(((BlockSyntax)lambdaExpression.Body).Statements);
}
else
{
if (!(lambdaExpression.Body is ExpressionSyntax))
yield break;
body = SyntaxFactory.Block();
if ((lambdaSymbol.ReturnType == null) || lambdaSymbol.ReturnsVoid)
{
body = body.AddStatements(SyntaxFactory.ExpressionStatement((ExpressionSyntax)lambdaExpression.Body));
}
else
{
body = body.AddStatements(SyntaxFactory.ReturnStatement((ExpressionSyntax)lambdaExpression.Body));
}
}
var method = GetMethod(model, span, lambdaSymbol, body);
yield return GetAction(document, model, root, lambdaExpression, method);
}
// anonymous method
var anonymousMethod = token.GetAncestor<AnonymousMethodExpressionSyntax>();
if ((anonymousMethod != null) && anonymousMethod.DelegateKeyword.FullSpan.Contains(span))
{
if (ContainsLocalReferences(model, anonymousMethod, anonymousMethod.Body))
yield break;
var lambdaSymbol = model.GetSymbolInfo(anonymousMethod).Symbol as IMethodSymbol;
if (lambdaSymbol == null)
yield break;
var method = GetMethod(model, span, lambdaSymbol, SyntaxFactory.Block(((BlockSyntax)anonymousMethod.Body).Statements));
yield return GetAction(document, model, root, anonymousMethod, method);
}
}
开发者ID:ceddlyburge,项目名称:RefactoringEssentials,代码行数:52,代码来源:ExtractAnonymousMethodCodeRefactoringProvider.cs
示例9: GetTextChange
private static TextChange GetTextChange(SyntaxNode root, SourceText sourceText, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
switch (token.Kind())
{
case SyntaxKind.XmlTextLiteralToken:
int spaceCount = token.ValueText.Length - token.ValueText.TrimStart(' ').Length;
return new TextChange(new TextSpan(token.SpanStart, spaceCount), " ");
default:
return new TextChange(new TextSpan(token.SpanStart, 0), " ");
}
}
示例10: RegisterCodeFixesAsync
protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
context.RegisterCodeFix(
CodeAction.Create(
Title,
c =>
{
var newRoot = root.ReplaceToken(token, SyntaxFactory.Token(SyntaxKind.None));
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
context.Diagnostics);
}
示例11: GetTransformedDocumentAsync
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
SyntaxToken updatedToken;
switch (token.Kind())
{
case SyntaxKind.XmlTextLiteralToken:
updatedToken = XmlSyntaxFactory.TextLiteral(" " + token.Text.TrimStart(' ')).WithTriviaFrom(token);
break;
default:
updatedToken = token.WithLeadingTrivia(token.LeadingTrivia.Add(SyntaxFactory.Space));
break;
}
Document updatedDocument = document.WithSyntaxRoot(root.ReplaceToken(token, updatedToken));
return Task.FromResult(updatedDocument);
}
示例12: GetNodeToSimplify
// public sealed override FixAllProvider GetFixAllProvider()
// {
// return SimplifyTypeNamesFixAllProvider.Instance;
// }
internal static SyntaxNode GetNodeToSimplify(SyntaxNode root, SemanticModel model, TextSpan span, OptionSet optionSet, out string diagnosticId, CancellationToken cancellationToken)
{
diagnosticId = null;
var token = root.FindToken(span.Start, findInsideTrivia: true);
if (!token.Span.IntersectsWith(span))
{
return null;
}
foreach (var n in token.GetAncestors<SyntaxNode>())
{
if (n.Span.IntersectsWith(span) && CanSimplifyTypeNameExpression(model, n, optionSet, span, out diagnosticId, cancellationToken))
{
return n;
}
}
return null;
}
示例13: ProcessAsync
public async Task<SyntaxNode> ProcessAsync(Document document, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
var model = await document.GetSemanticModelAsync(cancellationToken);
var diagnostics = model.GetDiagnostics().Where(s => s.Id == "CS8019");
var usingDirectives = new List<SyntaxNode>();
var newDocument = document;
foreach (var diagnostic in diagnostics)
{
var text = diagnostic.Location.SourceSpan;
var token = syntaxNode.FindToken(text.Start); // do I need to get the root from the document?
usingDirectives.Add(token.Parent);
}
var newNode = syntaxNode.RemoveNodes(usingDirectives, SyntaxRemoveOptions.KeepNoTrivia);
if (newNode != syntaxNode)
{
newDocument = document.WithSyntaxRoot(newNode);
}
return await newDocument.GetSyntaxRootAsync(cancellationToken);
}
示例14: RegisterCodeFixesAsync
protected sealed override async Task RegisterCodeFixesAsync(SyntaxNode root, CodeFixContext context)
{
var diagnostic = context.Diagnostics.First();
var diagnosticSpan = diagnostic.Location.SourceSpan;
var token = root.FindToken(diagnosticSpan.Start);
if (token.IsKind(SyntaxKind.UnsafeKeyword))
{
context.RegisterCodeFix(
CodeAction.Create(
TitleUnsafe,
c =>
{
var newRoot = RemoveRedundantUnsafe(root, token);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
context.Diagnostics);
return;
}
if (SimpleTokenKinds.Contains(token.Kind()))
{
var title = token.IsKind(SyntaxKind.PartialKeyword) ? TitlePartial : TitleSealed;
context.RegisterCodeFix(
CodeAction.Create(
title,
c =>
{
var newRoot = RemoveRedundantToken(root, token);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
}),
context.Diagnostics);
return;
}
RegisterCodeFixForChecked(token.Parent, root, context);
}
示例15: GetAdditionalIndentation
private int GetAdditionalIndentation(SyntaxNode root, SourceText text, TextSpan span)
{
if (_hostType == HostType.HTML)
{
return _optionService.GetOption(FormattingOptions.IndentationSize, this.Project.Language);
}
if (_hostType == HostType.Razor)
{
var type = GetRazorCodeBlockType(span.Start);
// razor block
if (type == RazorCodeBlockType.Block)
{
// more workaround for csharp razor case. when } for csharp razor code block is just typed, "}" exist
// in both subject and surface buffer and there is no easy way to figure out who owns } just typed.
// in this case, we let razor owns it. later razor will remove } from subject buffer if it is something
// razor owns.
if (this.Project.Language == LanguageNames.CSharp)
{
var textSpan = GetVisibleTextSpan(text, span);
var end = textSpan.End - 1;
if (end >= 0 && text[end] == '}')
{
var token = root.FindToken(end);
var syntaxFact = _workspace.Services.GetLanguageServices(Project.Language).GetService<ISyntaxFactsService>();
if (token.Span.Start == end && syntaxFact != null)
{
SyntaxToken openBrace;
if (syntaxFact.TryGetCorrespondingOpenBrace(token, out openBrace) && !textSpan.Contains(openBrace.Span))
{
return 0;
}
}
}
}
// same as C#, but different text is in the buffer
if (this.Project.Language == LanguageNames.VisualBasic)
{
var textSpan = GetVisibleTextSpan(text, span);
var subjectSnapshot = _containedLanguage.SubjectBuffer.CurrentSnapshot;
var end = textSpan.End - 1;
if (end >= 0)
{
var ch = subjectSnapshot[end];
if (CheckCode(subjectSnapshot, textSpan.End, ch, VBRazorBlock, checkAt: false) ||
CheckCode(subjectSnapshot, textSpan.End, ch, FunctionsRazor, checkAt: false))
{
var token = root.FindToken(end, findInsideTrivia: true);
var syntaxFact = _workspace.Services.GetLanguageServices(Project.Language).GetService<ISyntaxFactsService>();
if (token.Span.End == textSpan.End && syntaxFact != null)
{
if (syntaxFact.IsSkippedTokensTrivia(token.Parent))
{
return 0;
}
}
}
}
}
return _optionService.GetOption(FormattingOptions.IndentationSize, this.Project.Language);
}
}
return 0;
}