Skip to content

Support field names #54

Closed
Closed

Description

@jonhue

Hey,

I'm currently on a project that would greatly benefit from using field names to extract information on class/method names. I saw that you were already working on adding support to tree-sitter bindings for other languages. How are your plans regarding node? If you think it's feasible I'd also love to have a look at it.

/cc @maxbrunsfeld

Activity

  1. issue-label-bot commented on Nov 24, 2019

    @issue-label-bot

    Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.87. Please mark this comment with 👍 or 👎 to give our bot feedback!

    Links: app homepage, dashboard and code for this bot.

  2. maxbrunsfeld commented on Nov 24, 2019

    @maxbrunsfeld
    Contributor

    Yeah, they are already supported in node. There are auto-generated methods like ‘getNameNode’ and ‘getDecoratorNodes’ based on the field names. See #39 for more info.

  3. jonhue commented on Nov 26, 2019

    @jonhue
    Author

    @maxbrunsfeld Hmm... I tried

    node.getNameNode().text
    node.getNameNode.text
    node.nameNode.text

    but neither of them are working. Also, I didn't find any type annotation in index.d.ts so TypeScript is giving me errors. How would I get the name field from a node (say a class_declaration node in Java)?

  4. jonhue commented on Dec 15, 2019

    @jonhue
    Author

    Any updates on this @maxbrunsfeld? 🙂

  5. paked commented on Dec 23, 2019

    @paked

    @jonhue

    So maybe I've discovered something different than what @maxbrunsfeld is talking about here, but by looking at: https://github.com/tree-sitter/node-tree-sitter/pull/39/files#diff-168726dbe96b3ce427e7fedce31bb0bcR454-R478

    I was able to figure out you can access named fields like the following:

    (in the case where a node has two fields: 'body' and 'condition')

    node.fields // list of fields for a node (eg, ['bodyNode', 'conditionNode'])
    node.bodyNode // get the named field node
    

    IMO this is a bit of a weird API choice, wouldn't it have made more sense to have a dict of the fields accessible like: node.fields['body'] -- seems like it would be a lot more friendly to people writing tools using this lib.

  6. jonhue commented on Jan 3, 2020

    @jonhue
    Author

    @paked thanks! your solution works.

    I agree with you though, the current implementation is not very intuitive. Also, it lacks type hints for TypeScript. They would also be a lot easier to add if named nodes were to be accessible through a fields object.

  7. jmbockhorst commented on Sep 5, 2020

    @jmbockhorst

    Here is a new, promising library that can generate types for a given tree-sitter syntax. Then you would be able to access fields using node.bodyNode or whatever, which in my opinion is better than having to use a dict which wouldn't have the correct types. https://github.com/asgerf/dts-tree-sitter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions