Chcę uzyskać węzeł składniowy wiersza z podaniem położenia (numer wiersza). Poniższy kod powinien być oczywisty, ale daj mi znać na wszelkie pytania.Pobierz węzeł składni podając tekst wierszy w syntaxTree
static void Main()
{
string codeSnippet = @"using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello, World!"");
}
}";
SyntaxTree tree = SyntaxTree.ParseCompilationUnit(codeSnippet);
string[] lines = codeSnippet.Split('\n');
SyntaxNode node = GetNode(tree, 6); //How??
}
static SyntaxNode GetNode(SyntaxTree tree,int lineNumber)
{
throw new NotImplementedException();
// *** What I did ***
//Calculted length from using System... to Main(string[] args) and named it (totalSpan)
//Calculated length of the line(lineNumber) Console.Writeline("Helllo...."); and named it (lineSpan)
//Created a textspan : TextSpan span = new TextSpan(totalSpan, lineSpan);
//Was able to get back the text of the line : tree.GetLocation(span);
//But how to get the SyntaxNode corresponding to that line??
}
Co jeśli w tym wierszu nie ma węzła? Co jeśli jest więcej niż jeden? – svick
@svick Zakładam idealny przypadek. –