fwcd / kotlin-analysis-server

Language server prototype using the new Kotlin analysis API
MIT License
43 stars 2 forks source link

Migrate to updated analysis API #2

Open fwcd opened 5 months ago

fwcd commented 5 months ago

It's been a while since this prototype was written and we should have a look at what has changed in the analysis API over the last two years. KSP seems to be a good reference client that showcases how the API can be consumed, specifically the incremental compilation machinery looks really interesting: https://github.com/google/ksp/blob/319ddffc791afc9e34e1d0b80d323635a6ef06c2/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/KotlinSymbolProcessing.kt

fwcd commented 5 months ago

Worth keeping an eye on: https://youtrack.jetbrains.com/issue/KT-65215/Analysis-API-Distinguish-APIs-for-Analysis-API-users-and-platforms

nizam-betterapp commented 2 months ago

Thanks for all the great work.

we need ksp support to use android development in cursor ide (vscode). should we use this library or the kotlin-language-server library?

fwcd commented 2 months ago

This analysis server is not really ready for production yet and Android support in Kotlin Language Server is rather experimental too (it's a hobbyist project after all). If you have some resources to spare, contributions are highly appreciated though.

nizam-betterapp commented 2 months ago

I am super motivated to work on this project. where to get started?

fwcd commented 2 months ago

I would recommend taking a look at Kotlin Language Server, it's more or less a functioning language server, but with a couple of rough edges (e.g. dependency resolution could be faster, memory usage could be optimized etc). The structure is pretty straightforward, it's a standard LSP server that contains a KotlinLanguageServer, KotlinTextDocumentService and KotlinWorkspaceService, from where requests are dispatched to individual handlers (e.g. for code completion). The README also contains a small overview of how dependency resolution works. I would recommend just using the language server a bit to get a sense for what you think could be improved.

This project (Kotlin Analysis Server) is an alternative language server for Kotlin, but at the moment a highly experimental prototype. I haven't dug deeply enough into the new analysis API to work out whether it's complete enough to develop a full language server using it, but if you are really adventurous and want to contribute to this, I recommend taking a look at the analysis API docs.

nizam-betterapp commented 1 month ago

Sure. I'll start contributing to the KLS project first. Found this starter video on YouTube. I'll get started on this soon. Thanks!

amgdev9 commented 1 month ago

Hi! I'd like to share here some research I've been doing with the analysis API, I managed to get this working:

Here is the PoC, in case it may help in the development of this project: https://github.com/amgdev9/kotlin-analysis-api-test

gAbelli commented 1 month ago

I'm also interested in contributing to the project! I've been trying to figure out how to use KaSourceModificationService to handle didChange events from the LSP, but so far I haven't had much success since when I reload a file using PsiManager I still see the old, non-updated version. I also experimented with KotlinDeclarationProvider but I got similar results. If anybody has any ideas to share I would be happy to discuss them!

amgdev9 commented 1 month ago

I'm also interested in contributing to the project! I've been trying to figure out how to use KaSourceModificationService to handle didChange events from the LSP, but so far I haven't had much success since when I reload a file using PsiManager I still see the old, non-updated version. I also experimented with KotlinDeclarationProvider but I got similar results. If anybody has any ideas to share I would be happy to discuss them!

Do you have a snippet on how to access the KaSourceModificationService? As well as the library versions you are using, I cant manage to get this class to import, I might be missing something

gAbelli commented 1 month ago

@amgdev9 Sure, check out this branch: https://github.com/gAbelli/kotlin-analysis-server/tree/platform-api

amgdev9 commented 1 month ago

I'm also interested in contributing to the project! I've been trying to figure out how to use KaSourceModificationService to handle didChange events from the LSP, but so far I haven't had much success since when I reload a file using PsiManager I still see the old, non-updated version. I also experimented with KotlinDeclarationProvider but I got similar results. If anybody has any ideas to share I would be happy to discuss them!

@gAbelli How did you manage to reload files using PsiManager? I've been trying using

val manager = PsiManager.getInstance(session.project)
manager.reloadFromDisk(ktFile)

but it complains about a missing extension point Missing extension point: com.intellij.psi.treeChangeListener in container. I may be wrong, but I think I can't register an extension point if I'm not developing an intellij plugin

gAbelli commented 1 month ago

I don't know how to reload files either. PsiManager has an addPsiTreeChangeListener method but I'm not sure what to pass to it. By the way I'm not sure that reloadFromDisk is what we want, because one of the goals would be keep track of changes that haven't been written to disk yet.

amgdev9 commented 1 month ago

I don't know how to reload files either. PsiManager has an addPsiTreeChangeListener method but I'm not sure what to pass to it. By the way I'm not sure that reloadFromDisk is what we want, because one of the goals would be keep track of changes that haven't been written to disk yet.

Yeah, I guess we need to modify the PSI tree incrementally to achieve that, but as far as I have looked up the analysis api does not provide an easy way to commit changes to the PSI based on raw text and offsets which is what the LSP is going to receive... Doing this step crafting the PSI from scratch seems a hell lot of work 😅