Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current file path in Live Template

Is it possible to get the full path of the current file within a live template in IntelliJ? I've tried using groovyScript("new File('.').absolutePath") function, but that returns /Applications/IntelliJ IDEA.app/Contents/bin/. and not the file path as I was hoping for.

Thanks!

like image 685
Adunahay Avatar asked Dec 15 '22 04:12

Adunahay


1 Answers

According to the docs (emphasis mine):

You can use groovyScript macro with multiple arguments. The first argument is a script text that is executed or a path to the file that contains a script. The next arguments are bound to _1, _2, _3, ..._n variables that are available inside your script. Also, _editor variable is available inside the script. This variable is bound to the current editor.

The _editor is an instance of EditorImpl which holds a reference to the VirtualFile that represents the currently opened file.

Therefore, the following script gets the full path of currently opened file.

groovyScript("_editor.getVirtualFile().getPath()")

Or if you want to get the path relative to the project's root:

groovyScript("_editor.getVirtualFile().getPath().replace(_editor.getProject().getBaseDir().getPath(), \"\")")
like image 188
fracz Avatar answered Jan 30 '23 21:01

fracz