Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:21:53+00:00 2026-06-16T02:21:53+00:00

I want to get the SyntaxNode of a line given the location(lineNumber). The code

  • 0

I want to get the SyntaxNode of a line given the location(lineNumber). The code below should be self-explanatory, but let me know of any questions.

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??
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:21:54+00:00Added an answer on June 16, 2026 at 2:21 am

    First, to get TextSpan based on a line number, you can use the indexer of Lines of the SourceText returned by GetText() (but careful, it counts lines from 0).

    Then, to get all nodes that intersect that span, you can use an overload of DescendantNodes().

    Finally, you filter that list to get the first node that is contained fully in that line.

    In code:

    static SyntaxNode GetNode(SyntaxTree tree, int lineNumber)
    {
        var lineSpan = tree.GetText().Lines[lineNumber - 1].Span;
        return tree.GetRoot().DescendantNodes(lineSpan)
            .First(n => lineSpan.Contains(n.Span));
    }
    

    If there is no node on that line, this will throw an exception. If there is more than one, it will return the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code get all img tags preg_match_all('/<img[^>]+>/i',$a,$page); but I want get tags that their
I want get exeption code from wcf method but i always get NotFound error.
What I want: get a xml from the AppData to use What I code
I want to get list of tables in a given database and a given
I want get parameters from json, so I have a code: @RenderMode(name = VIEW)
actually i was opened question previously, but can't get answer what i exactly want,
i want get the last item of an array with class names. My code
I have the following code and I want get all minutes from time type.
I use cordova-2.0.0 and want define coordinaties of user location. Besides, I want get
I want get the number in this image, using python and PIL: my code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.