Search code examples
intellij-ideapycharmlive-templates

How to apply more than one function to a passed in live template variable?


I'm trying to build a Python Unit Test File Template in PyCharm. The overall result I want to achieve is:

  1. A user creates a new file with my template, say "widget_builder.py"
  2. Inside the template I want to create the class name by taking the file name "widget_builder" and turning it into "WidgetBuilderTests"

It looks like I need to use a Live Template to manipulate the file template variable $FILE_NAME$?

How can I create a Live Template that given a passed in variable (in this case $FILE_NAME$), applies both the underscoresToCamelCase and capitalize functions to it?

If I declare the Template text as:

$CLASS_NAME$

...and then edit variables, how can I reference a passed in variable of '$FILE_NAME$'?

I'd imagine it to look something like this, but I just can't get it to work:

Example

I'm sure there must be a way to do this, but I just can't quite wrap my head round it.

Is this possible? Thanks!

EDIT

I've got a bit further. If I define the template as this:

enter image description here

If I then use it, this happens:

enter image description here

So the end result of $CLASS_NAME$ (WidgetBuilder) on the left is what I want, but I don't want $FILE_NAME$ (widget_builder) to be there once I hit return.


Solution

  • So your problem here is that $FILE_NAME$ is not a native variable in the live templates, merely an arbitrary name. What you actually want to be using is another function: fileNameWithoutExtension().

    So your template would look something like: enter image description here