Author Topic: Prevent placing closing brace } on the next line (initializeing a variable)  (Read 2861 times)

patrickkox

  • Senior Community Member
  • Posts: 136
  • Hero Points: 6
  • Debian GNU/Linux user
I'm still learning to program in C++ and I would like to follow the more modern brace initializer instead of the older parentheses method.

so:
int variable {0};
intead of:
int variable (0);

But when I type the opening brace the closing one it put on the next like like this:
int variable {
};

Is it possible to prevent this?
This behaviour is great for functions and classes and such, but not for initializing a new variable.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6864
  • Hero Points: 528
Tools>Options>Languages>Application Languages>C/C++>Auto-Close

Change "Brace {}" option from "On next line" to "On same line".

patrickkox

  • Senior Community Member
  • Posts: 136
  • Hero Points: 6
  • Debian GNU/Linux user
Thanks, but this is a global option isn't it ?

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6864
  • Hero Points: 528
It does apply to other C++ brace situations.

Graeme

  • Senior Community Member
  • Posts: 2796
  • Hero Points: 347
If you check the keybinding for { in the keybindings dialog (or use the list-keydefs command), you'll see it's bound to a function called c_begin for "c" languages. 
Type this on the slick command line
fp c_begin
and you'll get to the c_begin function.  All the hard work is done by the call to c_expand_begin (a 400 line function), after which there's beautify while typing stuff.

If you type int abc{   - no space before the brace, slick produces {}.

If you want you could bind a key like Ctrl-shift-{ to the macro below - add it to your vusrmacs.e file and load it with the load module command in the macro menu.

For CUA emulation you'll see that shift+enter inserts a new line without doing any syntax expansion and shift+space inserts a space without doing syntax expansion.



Code: [Select]
_command void my_c_begin() name_info(','VSARG2_MULTI_CURSOR|VSARG2_REQUIRES_EDITORCTL|VSARG2_CMDLINE)
{
   _insert_text("{ }");
   cursor_left(2);
}


Graeme

  • Senior Community Member
  • Posts: 2796
  • Hero Points: 347
BTW - have you seen these articles
http://mikelui.io/2019/01/03/seriously-bonkers.html
https://blog.tartanllama.xyz/initialization-is-bonkers/

C++ initialization has always been confusing and sometimes unpredictable.  I think using braces is a good idea.  Embedded programmers like me are glad to have a guarantee of no initialization when it's not needed.

patrickkox

  • Senior Community Member
  • Posts: 136
  • Hero Points: 6
  • Debian GNU/Linux user
If you type int abc{   - no space before the brace, slick produces {}.
That looks like a valid solution, I have the habit of typing a space between the variable and the brace initializers. So I guess I'll need to unlearn that  ;D
Thanks for the tip!

patrickkox

  • Senior Community Member
  • Posts: 136
  • Hero Points: 6
  • Debian GNU/Linux user
BTW - have you seen these articles
http://mikelui.io/2019/01/03/seriously-bonkers.html
https://blog.tartanllama.xyz/initialization-is-bonkers/

C++ initialization has always been confusing and sometimes unpredictable.  I think using braces is a good idea.  Embedded programmers like me are glad to have a guarantee of no initialization when it's not needed.
I will have a look at those articles tomorrow when I have some more time.Tnx