Current file path in Live Template

泪湿孤枕 提交于 2019-12-02 00:21:44

问题


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!


回答1:


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(), \"\")")


来源:https://stackoverflow.com/questions/38488564/current-file-path-in-live-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!