本文整理汇总了C#中SyntaxNode.GetText方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.GetText方法的具体用法?C# SyntaxNode.GetText怎么用?C# SyntaxNode.GetText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.GetText方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FlattenMethodInvocation
/* Flatten the caller by replacing a invocation of the callee with the code in the callee. */
public static String FlattenMethodInvocation(SyntaxNode caller, SyntaxNode callee, SyntaxNode invocation)
{
// Get the statements in the callee method body except the return statement;
var statements = GetStatementsInNode(GetBlockOfMethod(callee))
.Where(s => !(s is ReturnStatementSyntax));
// Combine the statements into one string;
String replacer = StringUtil.ConcatenateAll("", statements.Select(s => s.GetFullText()).ToArray());
String callerString = caller.GetFullText();
// Replace the invocation with the replacer.
return callerString.Replace(invocation.GetText(), replacer);
}
示例2: AddParenthesesToExpression
/* Add a pair of parentheses to a given expression. */
private SyntaxNode AddParenthesesToExpression(SyntaxNode expression)
{
return Syntax.ParseExpression("(" + expression.GetText() + ")");
}
示例3: ComputeDestinationSpan
private TextSpan ComputeDestinationSpan(SyntaxNode insertionRoot, string insertionText)
{
var targetToken = insertionRoot.GetAnnotatedTokens(_otherAnnotation).FirstOrNullable();
var text = insertionRoot.GetText();
var line = text.Lines.GetLineFromPosition(targetToken.Value.Span.End);
// DevDiv 958235:
//
// void foo()
// {
// }
// override $$
//
// If our text edit includes the trailing trivia of the close brace of foo(),
// that token will be reconstructed. The ensuing tree diff will then count
// the { } as replaced even though we didn't want it to. If the user
// has collapsed the outline for foo, that means we'll edit the outlined
// region and weird stuff will happen. Therefore, we'll start with the first
// token on the line in order to leave the token and its trivia alone.
var firstToken = insertionRoot.FindToken(line.GetFirstNonWhitespacePosition().Value);
return TextSpan.FromBounds(firstToken.SpanStart, line.End);
}
示例4: AreNodesEqual
private bool AreNodesEqual(SyntaxNode one, SyntaxNode two)
{
// Get the code without trivia.
var codeOne = one.GetText().Replace(" ", "");
var codeTwo = two.GetText().Replace(" ", "");
// Get the scaled distance of two code, if the distance is less than 0.2,
// returns true.
var distance = StringUtil.GetScaledDistance(codeOne, codeTwo);
return distance <= 0.2;
}
示例5: PrintPretty
/// <summary>
///algorithm copied from
/// http://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure.
/// </summary>
/// <param name="node"></param>
/// <param name="indent"></param>
/// <param name="last"></param>
/// <returns></returns>
private string PrintPretty(SyntaxNode node,string indent, bool last)
{
var sb = new StringBuilder();
sb.Append(indent);
if (last)
{
sb.Append("\\-");
indent += "\t";
}
else
{
sb.Append("|-");
indent += "|\t";
}
sb.AppendLine(node.Kind.ToString() + ":" + StringUtil.ReplaceNewLine(node.GetText(), ""));
for (int i = 0; i < node.ChildNodes().Count() ; i++)
{
var child = node.ChildNodes().ElementAt(i);
sb.Append(PrintPretty(child, indent, i == node.ChildNodes().Count() - 1));
}
return sb.ToString();
}
示例6: ModifyIdentifierInAfterSource
private SyntaxNode ModifyIdentifierInAfterSource(SyntaxNode node, int idIndex,String newName)
{
var retriever = RetrieverFactory.GetRenamableRetriever();
retriever.SetRoot(node);
var tokens = retriever.GetIdentifierNodes();
Assert.IsTrue(idIndex < tokens.Count());
TextSpan span = tokens.ElementAt(idIndex).Span;
string beforeTokenCode = node.GetText().Substring(0, span.Start);
string afterTokenCode = node.GetText().Substring(span.End);
return ASTUtil.GetSyntaxTreeFromSource(beforeTokenCode + newName + afterTokenCode).GetRoot();
}
示例7: ReplaceMethodTextCSharp
private static void ReplaceMethodTextCSharp(SyntaxNode node, IDocument document, string textToSearch)
{
string methodText = node.GetText().ToString();
bool isMethod = node is MethodDeclarationSyntax;
string methodOrPropertyDefinition = isMethod ? "Method: " : " Invalid - not Method ";
object methodName = ((MethodDeclarationSyntax)node).Identifier.Value;
var body = (node as MethodDeclarationSyntax).Body;
var statements = body.Statements;
var originalTrailingTrivia = body.GetTrailingTrivia();
SyntaxList<StatementSyntax> newStatements = new SyntaxList<StatementSyntax>();
StatementSyntax statementNewTestmap = Syntax.ParseStatement("var testmap = new Dictionary<string, object>();");
newStatements = newStatements.Add(statementNewTestmap);
SyntaxList<StatementSyntax> flattenedStmts = Flatten(statements);
foreach (var statement in flattenedStmts)
{
//string stmtString = RemoveComments(statement).ToString();
string stmtString = statement.ToString();
stmtString = RemoveComments(stmtString);
if (Regex.IsMatch(stmtString, "\\b" + textToSearch + "\\b"))
{
StatementSyntax newStatement = null;
//string[] parameters = stmtString.Split(new char[] { '(', ',', ')' });
//if(parameters.Length == 4)
//{
// if(parameters.Last() == "1")
// {
// Console.WriteLine(stmtString);
// }
//}
//stmtString = TestmapAdd + "(" + parameters.ElementAt(1) + ", " + parameters.ElementAt(2) + ");" ;
var invocation = Syntax.ParseExpression(RemoveComments(stmtString)) as InvocationExpressionSyntax;
var args = invocation.ArgumentList.Arguments;
//var args = GetParams(stmtString);
if (args.Count() > 3)
{
throw new Exception();
}
if (args.Count() == 3)
{
#if DEBUG
if (args.Last().ToString() != "0")
Console.WriteLine(stmtString);
#endif
// Console.WriteLine(stmtString);
//stmtString = TestmapAdd + "(" + args.ElementAt(0) + ", " + args.ElementAt(1) + ");";
try
{
var registerArgs = new List<ArgumentSyntax>
{
args.ElementAt(0),
args.ElementAt(1)
};
var argSeparators = Enumerable.Repeat(Syntax.Token(SyntaxKind.CommaToken), registerArgs.Count - 1).ToList();
ArgumentListSyntax newArgList = Syntax.ArgumentList(
arguments: Syntax.SeparatedList(
registerArgs,
argSeparators));
InvocationExpressionSyntax newInvokationNode = Syntax.InvocationExpression(
Syntax.MemberAccessExpression(
kind: SyntaxKind.MemberAccessExpression,
expression: Syntax.ParseName("testmap"),
name: Syntax.IdentifierName("Add"),
operatorToken: Syntax.Token(SyntaxKind.DotToken)),
newArgList);
stmtString = newInvokationNode.ToString() + ";";
newStatement = Syntax.ParseStatement(stmtString);
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
if(args.Count() == 2)
{
stmtString = stmtString.Replace(textToSearch, TestmapAdd);
}
Console.WriteLine(stmtString);
newStatement = Syntax.ParseStatement(stmtString);
try
{
newStatements = newStatements.Add(newStatement);
}
catch (Exception e)
{
Console.WriteLine(e);
}
//stmtString.Replace(textToSearch, "thisTest.GenericVerify");
}
else
newStatements = newStatements.Add(statement);
}
newStatements = newStatements.Add(Syntax.ParseStatement("thisTest.GenericVerify(testmap);"));
FileRoot = FileRoot.ReplaceNode(body,
//.........这里部分代码省略.........
示例8: IsStatementsSame
private bool IsStatementsSame(SyntaxNode n1, SyntaxNode n2)
{
var s1 = n1.GetText().Replace(" ", "");
var s2 = n2.GetText().Replace(" ", "");
return s1.Equals(s2);
}