How to add custom imports into template?

Answered

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?

 

 

 

0
6 comments

Are you talking about file templates? You can check if there is a corresponding Compose code template exists and modify it if needed.

0

Yes, I'm talking about file templates. There aren't many Compose related templates, and none of them use delegation. 

0

Have you tried File | Save as template action? It will create a file template with the content you need.

0

Even without delegation, when it's possible to add imports from `androidx.compose.runtime.`

var string = remember { mutableStateOf("") }

Save as template cannot parse `remember` and `mutableStateOf` correctly, and gives me the following result:

var data = <TBD>

With delegation works same:

var data <TBD> <TBD>

 

0

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

0

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

0

Please sign in to leave a comment.