实时模板中的当前文件路径
Current file path in Live Template
是否可以在 IntelliJ 的实时模板中获取当前文件的完整路径?我试过使用 groovyScript("new File('.').absolutePath")
函数,但是 returns /Applications/IntelliJ IDEA.app/Contents/bin/.
而不是我希望的文件路径。
谢谢!
根据 docs(强调我的):
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.
_editor
是 EditorImpl
which holds a reference to the VirtualFile
的实例,表示当前打开的文件。
因此,以下脚本获取当前打开文件的完整路径。
groovyScript("_editor.getVirtualFile().getPath()")
或者如果你想获取相对于项目根目录的路径:
groovyScript("_editor.getVirtualFile().getPath().replace(_editor.getProject().getBaseDir().getPath(), \"\")")
是否可以在 IntelliJ 的实时模板中获取当前文件的完整路径?我试过使用 groovyScript("new File('.').absolutePath")
函数,但是 returns /Applications/IntelliJ IDEA.app/Contents/bin/.
而不是我希望的文件路径。
谢谢!
根据 docs(强调我的):
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.
_editor
是 EditorImpl
which holds a reference to the VirtualFile
的实例,表示当前打开的文件。
因此,以下脚本获取当前打开文件的完整路径。
groovyScript("_editor.getVirtualFile().getPath()")
或者如果你想获取相对于项目根目录的路径:
groovyScript("_editor.getVirtualFile().getPath().replace(_editor.getProject().getBaseDir().getPath(), \"\")")