Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 324
Michal Kurz Avatar asked Jan 22 '26 20:01

Michal Kurz


1 Answers

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

like image 64
lena Avatar answered Jan 25 '26 08:01

lena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!