Current file path in Live Template

后端 未结 1 1657
半阙折子戏
半阙折子戏 2021-01-19 07:09

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\") fun

相关标签:
1条回答
  • 2021-01-19 07:45

    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(), \"\")")
    
    0 讨论(0)
提交回复
热议问题