Skip to content

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

Description

@tsantalis

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;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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