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.

11 comments
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

`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
Comment actions Permalink

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

0
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

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
Comment actions Permalink

Thanks yole. I'll try later.

1
Comment actions Permalink

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

0
Comment actions Permalink

OK. I've found a post that says exactly the same I want to say:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000795610-What-s-the-methed-call-to-Resolve-a-Kotlin-reference?page=1

Could you please take a look at it? @yole

0

Please sign in to leave a comment.