Is there some util functions I can use to know the...
# compiler
j
Is there some util functions I can use to know the expected type at some position in a KtFile ? for instance let's say I have a method
doSomething(s: String)
we know that when calling this function the expected value inside the
()
should be a string. Is there anything in
kotlin-compiler
that already does all of this kind of logic and returns a
KotlinType
?
i
Can you rephrase want you want to accompish?
There are ways to resolve a FunctionDescriptor and all it’s parameter types
j
I am building a Language Server that provides auto completions. For instance, let's say the user is editing a file wit the content
doSomething(|)
(where
|
represents the cursor) then I need to know that in the given context we expect the user to type a string, i.e. I am trying to build a function
fun getExpectedType(file: KtFile, cursorOffset: Int): KotlinType?
r
You can traverse the ktfile with accept and a recursive visitor gathering all source locations and text ranges
For any ktElement
KtTreeVisitor I believe is what it's called in the compiler sources
👆🏽 1
Once you gather your type locations you can inspect the structure of it's parent and descendents if you wish to filter out some elements
The issue here is that depending in which compiler phases you are you may not have access to typechecking since analysis for example happens before resolution, but even there you can typecheck partial blocks
If you wait for arrow meta your language server would be easy because arrow meta generates automatic synth descriptors that you could just feed straight to the endpoints
In IDEA and plugins there is the PackageFragmentProvider, SyntheticResolverExtension and synthetic scopes that plugin authors can implement to automatically satisfy the editor need for synthetic members and anything you have codegen. Meta is gonna automate that so plugin authors never have to worry about those parts which for the most part today means writing your plugin twice
Also the PsiViewer plugin it's a godsend for programs that deal with KtElements since you can have a side representation of your code as a tree
i
If your using the PackageFragmentProvider, you may have to keep in mind, that your Resolution Strategy is getting called multiple times, even more often than you need, Thereby you might want to implement a Caching Rule or something similar. I also recommend the PsiViewer, helps if you want to dig into the Ide. Are you also implementing Ide Extensions ? If yes I can point you to the right Extension if you need it.
m
@raulraja Did you used the
SyntheticScopeProviderExtension
?
r
@MerlinTHS I have in the past a couple of times while experimenting but lately I have been focusing on FIR which as far as I know does not have that or I haven't run into it yet.