Best way to add comments at the top of a bal file by modifying syntax tree? #40365
Unanswered
lochana-chathura
asked this question in
Q&A
Replies: 1 comment
-
looping @sameerajayasoma @rdulmina @IMS94 @malinthar @hasithaa |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Playing around with modifying the syntax tree after a while. :) Today I encountered the scenario mentioned in the title. (Raised by @kalaiyarasiganeshalingam) As I couldn't find an already written similar code, thought of writing one myself.
One way of modifying ST is by knowing the internal structure. e.g. If we know the first line is an import declaration.
We could simply modify it by,
However, when the structure is unknown (generic scenario), I had to write one new method (
Token firstToken(Node node)
) and aTokenReplacer
by extending theTreeModifier
.My code is as follows (can run it by copying it to
SyntaxTreeModifierTest.java
class),firstToken()
) to get the first token of a givenSTNode
. However, no API to achieve the same in the external tree. So had to writeToken firstToken(Node node)
method. Should we add such API?syntaxTree.replaceNode()
returns a new syntax with the root being that token. I'm not sure why. So I ended up writing aTokenReplacer
to achieve the token replacement in the syntax tree.Want to make sure if the above is the best way to achieve it?
Beta Was this translation helpful? Give feedback.
All reactions