How can I get all Kotlin file in a `Project`?

Or more generally, is there an API for getting all `VirtualFile`s which are of the same `FileType`?

 

I have already got a project instance.

0

You can get it via FileTypeIndex.getFiles method. But note that there may be hundreds of thousands files in a project, so this call may cause performance and memory issues. Why do you need this?

 

1

Thanks first for telling me the API.

 

I'm working on a project (a simple editor based on Swing, here https://github.com/ice1000/dev-kt, since IDEA is too heavy for old bad machines and take long time to boot, my emacs/vim friends are not glad to use it. I create this lightweight editor for them, and by the way to get more familiar with Kotlin compiler's API) using Kotlin's compiler, and I want to implement more functionalities.

0

What kind of functionality you're going to implement by enumerating all Kotlin files in the project? Maybe there is a better way to do this.

1

`FileTypeIndex` is not present in Kotlin compiler. Is there any other alternatives?

 

My final goal is to get all the declarations and references in a mixed Java-Kotlin project, but when I create a project according to the test case I saw in the kotlin repo, I can only get java's psi classes/files.

I tried resolving some Java code that calling Kotlin, I get `null` (but I can resolve Java codes calling Java).

0

I know. Tens of minutes of time cost is acceptable, so no worry about time cost.

0

Only limited part of IntelliJ API is available inside Kotlin compiler, and the indices are not available indeed. It's better to ask your question at the Kotlin forum then.

1

Seems that Kotlin forum isn't friendly with kotlin compiler-related questions. I've asked a question about kotlin compiler's script API but get no replies and low views.

 

You said indices are not available, but I can get Java's PsiClass using APIs like `org.jetbrains.kotlin.com.intellij.psi.JavaPsiFacade#findClass`. But I cannot get Kotlin's `org.jetbrains.kotlin.psi.KtClass` by the same API, this is why I'm asking question here...

 

BTW, thanks for your help again.

0

There is no API in the Kotlin compiler to enumerate all Kotlin classes. To get all Kotlin classes in a specific file, you can use `PsiManager.findFile(VirtualFile)`, cast the result to `KtFile`, and call `KtFile.getClasses()`. Enumerating source files is something that you can implement by yourself; you don't need to use any platform APIs for that.

1

Thanks yole. I'll try later.

1

It works! @yole but I'm getting new problems now. I'll create a new post.

0

Hi all, 
could someone sare more info about  get/list files in Kotlin / Android Studio?
the function only returns directories..
Thanks

0

请先登录再写评论。