Search code examples
namespaceseclipse-cdtcode-completioneclipse-marsautocommenting

Why isn't Eclipse CDT adding a comment after closing namespace braces?


I'm using Eclipse CDT Mars.1 (4.5.1) to write some C++ code.

When I type in namespace foo {, Eclipse completes this with the closing namespace brace, two lines below. Now, I also want a comment on that closing brace, i.e.

namespace foo {

} /* namespace foo */

I know that Eclipse is supposed to add this comment, since I saw it in the preferences - under Window > Preferences > C/C++ > Code Style > Code Templates > Code > End of namespace declaration. It's just not getting used! Why?

PS - If it matters, I'm running Debian Stretch but I downloaded my Eclipse directly from eclipse.org


Solution

  • CDT does not support automatically adding the closing comment on the namespace just as a side-effect of automatically inserting the closing brace }.

    The best way to get the closing comment automatically is to type only namespace and then auto-complete (with Ctrl+Space).

    More Details

    There are three different features here.

    1. Automatically close of {Braces} settable in Preferences -> C/C++ -> Editor -> Typing

      This feature simply puts a closing brace when you type an open brace and is what you are doing when you type namespace foo {

    2. File template, settable in Preferences > C/C++ > Code Style > Code Templates > Code > End of namespace declaration (and Beginning of namespace declaration)

      This controls the format of new classes in new files that you create with the new Class Wizard. This is the setting you have referred to in your question.

    3. Editor templates, settable in Preferences -> C/C++ -> Editor -> Templates -> namespace

      This feature works when you insert a namespace template. To do that:

      1. type namespace or part of it

      2. press Ctrl+Space. This will bring you the standard completion proposals.

      namespace proposals

      1. Select the namespace entry on the list corresponding to the template version

      2. Type your namespace name and you should see it appearing in the declaration and the comment at the end

    typing with two cursors

    Note: If you type namespace completely and press Ctrl+Space it will probably auto-select the template if there are no other possible matches.