question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get the entire AST instead of just the summary?

See original GitHub issue

Hi!

The title says it basically. How do you get the entire AST, with DefaultAstNodes converted to the Klass Data classes, from a kotlinfile. In the example the following function is invoked:

kotlinFile.summary()

This function does not include the body of a function for example. I tried to create my own function:

kotlinFile.complete()

private val completeTreeMap: TreeMap = DefaultTreeMap(
    annotationsMapper,
    declarationsMapper,
    expressionsMapper.flattenNames("functionDeclaration"),
    globalsMapper,
    kotlinDefaultMapper,
    modifierMapper,
    stringMapper,
    typesMapper
)

fun List<Ast>.complete(): AstResult<List<Ast>> {
    return completeTreeMap.treeMap(this)
}

fun Ast.complete(): AstResult<List<Ast>> {
    return listOf(this).complete()
}

I tried to change the Mappers in the DefaultTreeMap, but couldn’t get a configuration to be able to visit the statements in the body of functions.

Hopefully this is enough information to be able to resolve my problem.

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
driekscommented, May 1, 2020

Hi,

I added a new Option: kotlinFile.summary(attachRawAst = true)

attachRawAst: Boolean is now a required argument for summary.

The Version is c35b50fa44

0reactions
driekscommented, Jul 29, 2020

Hi @miniyoung84, the Screenshot was taken from IntelliJ IDEA (https://kotlinlang.org/docs/tutorials/jvm-get-started.html)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python AST library to get all assignment statements
You could use ast.NodeTransformer and the visit_Assign and visit_AugAssign methods. Here's a quick demonstration: import ast import inspect ...
Read more >
ast — Abstract Syntax Trees — Python 3.11.1 documentation
Each concrete class has an attribute _fields which gives the names of all child nodes. Each instance of a concrete class has one...
Read more >
Manipulating AST with JavaScript - Tan Li Hau
Creating a node​​ There are a few ways you can create an AST node. The simplest and crudest way is to manually create...
Read more >
Leveling Up One's Parsing Game With ASTs | by Vaidehi Joshi
Okay, so now we have two trees to keep straight in our heads. We already had a parse tree, and how there's yet...
Read more >
Introduction to Abstract Syntax Trees - Twilio
AST stands for Abstract Syntax Tree and they power a lot of parts of your development flow. Some people might have heard about...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found