Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalize ${NAME} in File and Code Templates

Problem

In Preferences -> Editor -> File and Code Templates I have a template for React with:

import React from 'react';

class ${NAME} extends React.Component {

  render() {
    return (
      <div>
        #[[$END$]]#
      </div>
    );
  }
} 

export default ${NAME};

Is there a way to capitalize the ${NAME}?

We have a convention of naming our files starting with a lowercase but in React components are meant to be capitalized.

Additional Information

I'm aware of the IntelliJ's ability to refactor and that you can use a Live Templates to accomplish this but I would like to remove this extra step :).

This is possible in Live Templates where you can define and reference a expression like capitalize(fileNameWithoutExtension()) but I couldn't find anywhere to define expressions in File and Code Templates.

like image 246
ksrb Avatar asked Oct 28 '16 19:10

ksrb


3 Answers

So after doing some research I came up with this:

import React from 'react';

#set($capitalizedFilename = $NAME.substring(0,1).toUpperCase() + $NAME.substring(1))
class $capitalizedFilename extends React.Component {
  
  render() {
    return (
      <div>
        #[[$END$]]#
      </div>
    );
  }
} 

export default $capitalizedFilename;
like image 85
ksrb Avatar answered Oct 21 '22 13:10

ksrb


An IntelliJ IDEA file template is a Velocity template. Velocity allows you to use standard String methods on variables (see this question for some examples).

like image 3
yole Avatar answered Oct 21 '22 13:10

yole


Here is all cases for WebStorm File template:

#set($NAME_CAP = $NAME.substring(0,1).toUpperCase() + $NAME.substring(1))
#set($NAME_LOW = $NAME.substring(0,1).toLowerCase() + $NAME.substring(1))
#set($NAME_UPPER = $NAME.toUpperCase())
like image 2
Anton Dokuchaev Avatar answered Oct 21 '22 13:10

Anton Dokuchaev