Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help: How to edit a single AST? #77

Closed
Himujjal opened this issue Feb 24, 2021 · 2 comments
Closed

Help: How to edit a single AST? #77

Himujjal opened this issue Feb 24, 2021 · 2 comments

Comments

@Himujjal
Copy link

In a certain use case, I need to edit the AST to enclose a for-loop in a statment_block for tree-sitter-javascript:

Before:

// ...preceding blocks
for(let i = 0; i < 10; i++) {
  // ...for body
}
// ...succeeding blocks

After:

// ...preceding blocks
{ for(let i = 0; i < 10; i++) {
  // ...for body
} }
// ...succeeding blocks

Is it possible to edit the AST this way using tree-sitter-node? If yes. How do I do it? Currently, the only way I see is to create a new StatementBlockNode object and then attach it to the tree. In the after case, I don't care about the row and column numbers.

@Himujjal Himujjal changed the title Help: How to a single AST? Help: How to edit a single AST? Feb 24, 2021
@ahlinc
Copy link
Contributor

ahlinc commented Mar 7, 2021

@Himujjal It's not possible to edit tree-sitter's AST cause it runs counter to tree-sitter abilities like cheap syntax tree edits, recovering after errors and precise nodes position tracking. There only one way is to edit grammar.js file so the tree-sitter would build a different syntax tree by itself.

@Himujjal
Copy link
Author

Himujjal commented Mar 7, 2021

Cool no problem. I anyways got the problem solved. Thanks for your help. We can close this issue.

@Himujjal Himujjal closed this as completed Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants