“This is the 23rd day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

The Compose code template will not be in the Compose AS in 2022.

File Templates

For Windows: File -> Settings -> Editor -> File and Code Templates.For Mac, go to File -> New -> Edit File Templates -> Preference -> Editor -> File and Code Templates.

There is little difference between Mac and Win, so I won’t repeat it here.

Note that Scheme allows you to choose whether the template applies to all projects by default or to this project specifically.

Click the + sign to create the template.

The corresponding input template name. The file name extension is on the right. So this is Java and let’s change it to KT.

Fill in the following template:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} ! = "")package ${PACKAGE_NAME} #end #parse("File Header.java") import androidx.compose.runtime.Composable import androidx.compose.ui.tooling.preview.Preview #if (${Function_Name} == "" ) @Composable fun ${NAME}() { } @Preview @Composable fun ${NAME}Preview() { ${NAME}() } #end #if (${Function_Name} ! = "" ) @Composable fun ${Function_Name}() { } @Preview @Composable fun ${Function_Name}Preview() { ${Function_Name}() } #endCopy the code

Click OK to set up.

In the project directory, right-click New to see Jack. The template we set up

Click to start setting up a new file:

Live Templates

Same setup directory, Editoer -> Live Templates.

Click the + sign on the right

Choose the first one

So in Abbreviation, you put a hint, and then the description.

Next we introduce the following code into the Template text:

@androidx.compose.runtime.Composable
fun $NAME$() {
$END$
}

@androidx.compose.ui.tooling.preview.Preview
@androidx.compose.runtime.Composable
fun $NAME$Preview() {
$END$
}
Copy the code

There is No Applicable contexts. At this point, I’m going to click on the blue Define next to it.

Kotlin, of course. You can see the drop-down list here, where the range is selected, and everyone can choose as much as they like.

Then click OK and the setup is successful. We tested the final results in the new KT file.

Enter and see:

So far, the setting is successful.