Search code examples
idecode-generationui-automationclioncode-completion

How can I get CLion to append a comment after the closing namespace brace?


In CLion (e.g. 2021.2), if you type namespace foo, nothing special happens. I would have liked it if this would get me:

namespace foo {



} // namespace foo

with the cursor on the middle line. How close can I get to making that happen?


Solution

  • Inspired by your answer, and with full credit to you for getting this far, I have progressed this such that the Live Template behaviour you seek is automatically invoked just by typing namespace<space>.

    To set this up, create the Live Template as per that answer, but make sure:

    1. The name of the Live Template must be namespace, as this is used to select this template once expansion is triggered, and
    2. Set the template text to include the $END$ variable, as that sets the final cursor position to be within the new namespace:
    namespace $NAMESPACE_NAME$ {
    $END$
    } // namespace $NAMESPACE_NAME$
    
    1. Set the drop-down "Expand with Default (Tab)" to "Expand with Space". This will cause the space character you type after the namespace keyword to automatically trigger the selection of the template.

    Settings Dialog > Editor > Live Templates

    Now you can type namespace<space>my_namespace and it will automatically create the following text, as you type, with no need to select from a dropdown or hit Tab, and the cursor will be placed at the start of the blank line within the new namespace:

    namespace my_namespace {
    
    } // namespace my_namespace