在文件和代码模板中大写$ {NAME} [英] Capitalize ${NAME} in File and Code Templates

查看:135
本文介绍了在文件和代码模板中大写$ {NAME}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

首选项->编辑器->文件和代码模板中,我有一个React模板:

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};

有没有一种方法可以将${NAME}大写?

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

我们有一个约定,即以小写字母命名文件,但在React组件中应大写.

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

其他信息

我知道IntelliJ的重构能力,可以使用 Live模板完成此操作,但是我想删除此额外步骤:).

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 :).

这在 Live Templates 中是可能的,您可以在其中定义和引用类似capitalize(fileNameWithoutExtension())的表达式,但是我找不到在任何地方定义 File and Code Templates 中的表达式的地方.

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.

推荐答案

因此,在进行了一些研究之后,我想到了:

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;

这篇关于在文件和代码模板中大写$ {NAME}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆