Auto-indent of function blocks for C/C++

Syntax highlighting, code folding, brace matching, code indenting, and function list

Auto-indent of function blocks for C/C++

Postby complexmath » Wed Dec 22, 2010 3:42 pm

I don't have UE installed on Win/Linux or I'd test this myself, but with UE for Mac if I do "void MyObj::fn()" and hit ENTER the cursor ends up indented instead of lined up under the 'v'. Looking at the wordfile for C/C++ I have:

Code: Select all
/Indent Strings = "{" "if" "else" ":"

So this indent shouldn't be occurring, correct?
complexmath
Newbie
 
Posts: 6
Joined: Mon Nov 01, 2010 4:08 pm

Re: Auto-indent of function blocks for C/C++

Postby Mofi » Thu Dec 23, 2010 1:13 am

Indentation is caused by the defined indent strings which you can modify to your needs. The standard wordfile for C/C++ contains /Indent Strings = "{" "if" "else" ":" as you already posted. But most users just need:

/Indent Strings = "{"
/Unindent Strings = "}"


"if" "else" are for those users writing if conditions with a single code line like

Code: Select all
if( bTest == true )
   iVar = 50;
else
   iVar = *piInterface;

If you write such conditions like

Code: Select all
if( bTest == true ) iVar = 50;
else iVar = *piInterface;

or like
Code: Select all
if( bTest == true )
{
   iVar = 50;
}
else
{
   iVar = *piInterface;
}

you should remove from "if" "else" the indent strings line.

":" is mainly for

Code: Select all
switch( eType )
{
   case Standard:
      printf("Standard type\n");
      break;
   case Extended:
      printf("Extended type\n");
      break;
}


If you code your switch conditions like

Code: Select all
switch( eType )
{
   case Standard:   printf("Standard type\n");
                    break;
   case Extended:   printf("Extended type\n");
                    break;
}

or like

Code: Select all
switch( eType )
{
   case Standard:
                  printf("Standard type\n");
                  break;
   case Extended:
                  printf("Extended type\n");
                  break;
}

or if you code in C++, you should remove ":" from the indent strings (or replace it with "case"). ":" is also present for goto target labels.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Auto-indent of function blocks for C/C++

Postby complexmath » Thu Dec 23, 2010 10:46 am

But according to DaveS, UE behaves differently on Windows, which is consistent with my memory of UE on Windows. Also, I think the rule should apply based on the last character, not the presence of a character in a line at all. Otherwise, I'd think UE for Mac would incorrectly indent this as well:

Code: Select all
int x = 1 > 2 ? 1 : 2;
// is this line indented because of the colon above?
complexmath
Newbie
 
Posts: 6
Joined: Mon Nov 01, 2010 4:08 pm

Re: Auto-indent of function blocks for C/C++

Postby Mofi » Thu Dec 23, 2010 11:48 am

complexmath wrote:But according to DaveS, UE behaves differently on Windows, which is consistent with my memory of UE on Windows.

Yes, that is right for the user interface and file locations, but surely not for the core functions as auto-indent is one.

complexmath wrote:Also, I think the rule should apply based on the last character, not the presence of a character in a line at all.

There is no other rule than if an indent string is found on a line not inside a comment or string, the next line will be indented. So your code line results in indenting the next line.

Please take into consideration that UltraEdit (for any platform) is a general text editor not written for certain programming languages. I use auto-indent for text files which do not contain a programming language. So there are no special rules for some programming languages included. The IDE solution of IDM - UEStudio - is more specialized for some programming languages and offers additional features for faster writing C/C++ code, but indenting works the same way.

Best is you simply remove ":" from indent strings definition in the wordfile for C/C++.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4058
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Auto-indent of function blocks for C/C++

Postby complexmath » Thu Dec 23, 2010 2:42 pm

I just tested windows and it exhibits the same behavior, including wrongly indenting below a "a<b?c:d" expression, so you're right that this is expected behavior rather than a bug in the Mac implementation. I'll change my wordfile. Thanks :-)
complexmath
Newbie
 
Posts: 6
Joined: Mon Nov 01, 2010 4:08 pm


Return to Syntax Highlighting