Search code examples
autocompletenamespaceseclipse-cdt

How can I make ecplise append a "// namespace my_ns" when starting a namespace?


In Eclipse CDT, if you type in:

namespace my_ns {

it completes the { with a closing branch, } two lines later, and move the cursor to the empty line between.

Is it possible to make Eclipse add

} // namespace my_ns

i.e. also append a trailing comment?


Solution

  • Use content assist to declare the namespace. That means:

    • Type namespace without a trailing space or the name you want to use.
    • Type Ctrl+Space.
    • Type the namespace name.

    Voilà! With Window > Preferences > C/C++ > Code Style > Code Templates > Code > End of namespace declaration you can customize the comment.

    Notes:

    • The namespace code template may not trigger if you type in namespace foo, then press Ctrl+Space. If you type in an opening brace at that point, you will only get a matching closing brace - not the end of a namespace.
    • If instead of choosing the namespace code template, a pop-up list is shown, then typing Ctrl+Space again will limit the choices to code templates only. The code template namespace - namespace declaration is the right one.