Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Generate trees for Kotlin using the kotlin-compiler-embeddable parser #421

Copy link
Copy link
@tsantalis

Description

@tsantalis
Issue body actions

The tree-sitter parser for Kotlin is quite unreliable.

In many cases, it generates ERROR nodes (more details in the issue below)
bonede/tree-sitter-ng#108
and it also parses functional interfaces as functions.

I would strongly recommend to use kotlin-compiler-embeddable
https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-compiler-embeddable/versions?repo=central
for generating the AST.

It provides a org.jetbrains.kotlin.psi.KtVisitor

Parsing is easy as follows:

String filePath = ""; // the path of a kt file
String fileContent = "" // the string content of a kt file
KotlinCoreEnvironment environment = KotlinCoreEnvironment.createForProduction(
  	Disposer.newDisposable(),
  	new CompilerConfiguration(),
  	EnvironmentConfigFiles.JVM_CONFIG_FILES
);
PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(environment.getProject());
PsiFile psiFile = factory.createFileFromText(filePath, KotlinLanguage.INSTANCE, fileContent);
KtFile ktFile = (KtFile)psiFile;
Reactions are currently unavailable

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.