Michal Kurz
Michal Kurz

Reputation: 2094

WebStorm live templates: how to create an 'export default from' live template / How to get current folder name?

I am working on React projects, where components have the following file structure:

ComponentName/
├── ComponentName.jsx
├── possiblySomeOtherFiles.js/jsx
└── index.js

and index.js reexports ComponentName.jsx like so:

import ComponentName from './ComponentName'

export default ComponentName

I want to create a live template for this like so:

import $COMPONENT_NAME$ from './$COMPONENT_NAME$'

export default $COMPONENT_NAME$

How do I get the value of $COMPONENT_NAME$? I would like to get it from the name of the current directory, but can't figure out how to do that.

I tried setting the variable to groovyScript("_editor.getVirtualFile().getPath()") and groovyScript("new File('.').absolutePath") as hinted here, but both return:

groovy

/
lang / GroovyShell

Is this even possible as of now?

Upvotes: 3

Views: 667

Answers (1)

lena
lena

Reputation: 93908

The following expression should do the thing:

groovyScript("_editor.getVirtualFile().getParent().getName()")

it works fine for me in Intellij IDEA, but not in Webstorm, because Groovy plugin is not bundled and can't be added to it:( Please follow WEB-28139 for updates

Upvotes: 6

Related Questions