Optimize your development workflow

IntelliJ File templates. Create multiple folders and files in one operation!

A developer’s perspective on utilizing IntelliJ file templates for faster code creation

Marc Picone
by MWM
Published in
5 min readDec 16, 2022

--

[Update 22/03/2023] -> Crafting Custom Path Formatting in IntelliJ File Templates for Android Development

Hi, as a developer, we are always looking for ways to streamline our processes and make our work more efficient. One tool that I have found to be incredibly helpful in achieving this is IntelliJ file templates. In this article, I’ll be sharing my experience with using file templates and how they have helped me improve my development workflow.

  • Level: Medior to senior developers.
  • Goal: Create folders with file templates.
  • Requirement: Android Studio IDE or any IntelliJ based IDE.

1-Why this article?

Before Android Studio 4.1, we had the possibility to create our own template with multiple files and folders, but unfortunately, this feature is no longer available.

However, with the help of IntelliJ file templates and Velocity Template language, we can still achieve a similar outcome.

External resources:

  • Here is the solution that existed before Android Studio 4.1.
  • Here the actual IntelliJ solution to create multiple files
  • Here is the Velocity Template Language documentation, that will help us to manipulate variables.

2-Creating Folder with File Templates

File templates in IntelliJ IDEs can be a powerful tool for automating repetitive and tedious tasks. One of the most useful features is the ability to create multiple files and folders in one operation.

To use file templates, you’ll first need to create a new template. In the file name, you can use variables such as ${NAME} to represent the name of the file or folder you want to create.

For example, you can use the variable ${PACKAGE} to represent the name of the package you want to create. The template file name could look like this:

`${PACKAGE}/${NAME}`

When you create a new file using this template, the IDE will prompt you to fill in the values for NAME and PACKAGE.

You can also use the Velocity Template Language (VTL) to format variables. For example, if we want to name the package in snake case “feature_name_view” and the associated file in camel case “FeatureNameView”, we don’t want to have to enter the same name in snake case and camel case every time we create a new file template, even if they are the same.

So, we could assume that now the ${NAME} is no more the file name, but the feature name, and this is where VTL comes in.

We can use VTL’s StringUtils.removeAndHump() method to format the variables. This method will take the ${NAME} variable (which represents the feature name) and remove the underscores, effectively converting it to camel case. This way, we don’t have to manually enter the same name in different cases, the IDE will do it for us. The resulting path would look like this: feature_name_view/${StringUtils.removeAndHump(feature_name_view, ”_”)}FileType.

And that’s the way it could be wrote in our file template file name:

This will allow you to only enter the feature name in snake case and have the IDE automatically create the file name in camel case. If the folder already exists, only the files will be created in the existing folder, otherwise the folder and attached files will be created.

3-Create a secondary folder with a child template.

You can also use the Velocity Template Language (VTL) to create a secondary folder with its own files at the same time. It’s important to note that to create multiple files or folders you will need to create a child template on your file template.

For example, to create a folder in the same root folder, you can simply copy-paste the previous example and change the variable ${NAME} to a new custom variable in your child template file name, like:

${ATTACHED_FEATURE_NAME}/${StringUtils.removeAndHump(${ATTACHED_FEATURE_NAME}, “_”)}

The IDE will ask for the two feature names and will determine the two attached files names, creating them in their respective folder.

To create a folder in another root folder, you can use the ${PACKAGE_NAME} (given by the IDE) variable from the parent file to retrieve the root folder.

One way to navigate to another root folder is to split the ${PACKAGE_NAME} variable from the parent file on the dot, then create a loop on the resulting array. On each iteration, we can add “../” to a path variable. This way, we can go up one folder level at a time, until we reach the desired root folder. Once we have the root folder, we can create a new folder or file in any location within that folder tree.

It’s possible to write this script in the file name of the file template, but it could be a long line and hard to understand. A better solution is to use the “Includes” tab to store this script and use the #parse(“FileName.extention”) directive in your file templates to include it where you need it. This way you can easily reuse this script and keep your templates clean and readable.

For example, you can use a script like this to retrieve the “res” folder:

It’s important to note that there should be no spaces at the beginning or end of every line as this can cause issues with the creation of the package.

  • The script begins by initializing all variables that will be used to create the path. This is necessary so that the Intellij IDE will not prompt the user to fill in the variable when creating the file.
  • Next, the script enters a foreach loop on the package name, which is split on the dot. The #set directive is used to set the value of the variable and avoid printing it. In the loop, on each iteration, “../” is added to the “$resRelativePath” variable, allowing the script to navigate up one folder in the root folder.
  • The script then checks if it is on the last iteration of the loop. If it is, it concatenates $endResPath to access the “res” folder.
  • Finally, the script calls the $resRelativePath variable, which is the desired result and can be used to create a secondary package or navigate to other desired folders within the package.

Pretty cool, isn’t it? With this path we could now create a secondary package, and more if needed, with simply filling the package name.

In conclusion, file templates are a powerful tool in a developer’s toolbox. With a little bit of creativity and understanding of VTL, you can streamline your workflow and save time on repetitive tasks.

I hope you’ve seen the potential of the tips discussed in this article and how they can be applied to your own projects.

If you’re interested in seeing how I’ve used these techniques in my own work, check out my article “The power of file templates.”

Enjoy, explore, and keep coding! 💪

--

--

Marc Picone
by MWM
Writer for

Hello! I'm Marc, Android developer at MWM. I've been creating innovative music, drawing, and creative apps with MWM since 2021. Let's connect and exchange ideas