Search code examples
typescriptintellij-ideaphpstormwebstormlive-templates

IntelliJ live template. Remove or replace number at the beginning of filename


I have database migration files with timestamp at the beginning of the filename. For example: 1537865596694-create-config-table.migration.ts

There is expression you can use to convert filename to Typescript class name: capitalize(camelCase(fileNameWithoutExtension()))

But I can't find an option to remove or replace number.


Solution

  • You can try groovyScript("groovy code") (see https://www.jetbrains.com/help/idea/2017.2/edit-template-variables-dialog.html#predefined_functions, http://bartololeo.blogspot.ru/2014/01/idea-12-how-to-write-live-template-with.html. Like:

    groovyScript("def res=''; def fname=_editor.getVirtualFile().getNameWithoutExtension(); def index=fname.indexOf('-'); if
    (index!=-1) res=fname.substring(index+1, fname.indexOf('.')).replaceAll('(-)([A-Za-z0-9])', { Object[] it -> it[2].toUpperCase() } ) else res=fname; return res")