How to add custom imports into template?
已回答
I'm trying to create the following template for Compose:
var string by remember { mutableStateOf("") }
I can add remember and mutableStateOf imports manually like this:
var string by androidx.compose.runtime.remember { androidx.compose.runtime.mutableStateOf("") }
But there're also two more imports needed to make delegation work:
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
Is there a way to make template import these too?
请先登录再写评论。
Are you talking about file templates? You can check if there is a corresponding Compose code template exists and modify it if needed.
Yes, I'm talking about file templates. There aren't many Compose related templates, and none of them use delegation.
Have you tried File | Save as template action? It will create a file template with the content you need.
Even without delegation, when it's possible to add imports from `androidx.compose.runtime.`
Save as template cannot parse `remember` and `mutableStateOf` correctly, and gives me the following result:
With delegation works same:
Sounds like the fix for these issues would help for this case:
KTIJ-20402 Kotlin plugin doesn't suggest importing a `getValue` operator when the delegate type is inferred from a lambda
KTIJ-16966 NI: Kotlin plugin doesn't suggest importing `getValue` operator if there is no explicit type parameter for delegate
I see, but these issues seems to be related to importing, which should theoretically fix Save as template action, but the fact wether templates support such syntax seems like a separate question, I have created an issue, thank you