Discussion:
[dart-misc] Analyzer Package - How to edit CompilationUnit / AstNode tree?
Artem Romanko
2017-06-15 22:45:36 UTC
Permalink
I need to find, replace and insert *Tokens* and *AstNode* in the
*CompilationUnit/AstNode* tree, that I've got from calling
parseDartFile("path/to/file.dart")


I found a way to search for certain classes/methods e.t.c and replace them
- https://stackoverflow.com/a/35577287/3768314
But I still can't find a way to create and insert custom *AstNode* before
or after some other node.

We get *CompilationUnit* from parsing. It has *childEntities* property,
which is *Iterable* and it can't be changed.
So only idea right now is to somehow recursively copy each node from one
*CompilationUnit* to the empty one and insert additional nodes when needed.
But I still need help with it.
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
'Brian Wilkerson' via Dart Misc
2017-06-16 03:57:56 UTC
Permalink
Artem,

The AST structure isn't designed to be edited. While it seems like it
should be a good way to edit source code, it turns out in practice to not
be very good for most functionality. As a result, the editing tools
produced by the analyzer team use the AST structure to find the source
locations involved and then perform textual edits on the source code. Not
knowing exactly what you're trying to do it's impossible to say for sure,
but that might be a better approach for you too.

Brian
Post by Artem Romanko
I need to find, replace and insert *Tokens* and *AstNode* in the
*CompilationUnit/AstNode* tree, that I've got from calling
parseDartFile("path/to/file.dart")
I found a way to search for certain classes/methods e.t.c and replace them
- https://stackoverflow.com/a/35577287/3768314
But I still can't find a way to create and insert custom *AstNode* before
or after some other node.
We get *CompilationUnit* from parsing. It has *childEntities* property,
which is *Iterable* and it can't be changed.
So only idea right now is to somehow recursively copy each node from one
*CompilationUnit* to the empty one and insert additional nodes when
needed. But I still need help with it.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an
--
For other discussions, see https://groups.google.com/a/dartlang.org/

For HOWTO questions, visit http://stackoverflow.com/tags/dart

To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...