Author Topic: Style 2 nit with open brace  (Read 2586 times)

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Style 2 nit with open brace
« on: April 05, 2008, 07:38:48 PM »
@SlickTeam:  I have a growing number of small patches I would like to submit soon.  What is the preferred way to report bugs/fixes, should I send to support@slickedit.com, or post in the forums, or both?

Here is a patch for a Style 2 nit.  Given a line like the following, with the cursor at the [\c] spot:
Code: [Select]
    if (xyz) [\c] { foo(); }

If I type Enter, it becomes the following, with the second line indented even though Style 2 is in effect:
Code: [Select]
    if (xyz)
        { foo(); }

Typing Enter at the obvious points to finish converting the one-line statement to a multi-line statement results in the following:
Code: [Select]
    if (xyz)
        {
        foo();
    }

With the patch I propose further below, the initial Enter results in the following, to conform to Style 2:
Code: [Select]
    if (xyz)
    { foo(); }
So the full expansion ends up correctly conforming to Style 2 without need for any manual fixup:
Code: [Select]
    if (xyz)
    {
        foo();
    }


Edited:  Updated with a better patch than I had initially suggested.  This is smaller and more targetted, but it takes a lot of testing to ensure no other scenarios got broken, and I haven't exhaustively tested for that.

Part 1 -- c.e, at the end of _c_expand_enter:
Code: [Select]
   if ( status ) {  /* try some more? Indenting only. */
      status=0;
      col=c_indent_col2(0,0);
      indent_on_enter(0,col);
      // chrisant: When splitting a line in whitespace preceding an open
      // brace, strip whitespace up to the brace so it is indented properly.
      if ( substr(strip(get_text(100),'L'),1,1) == '{') {
         while (pos(get_text(-1)," \t")) {
            _delete_char();
         }
      }
   }
   return(status);
}

Part 2 -- c.e, at the end of c_indent_col2:
Code: [Select]
      default:
         if (cfg==CFG_KEYWORD) {
            /*
               Cases
                 if ()
                    if () <ENTER>
                 for <ENTER>

            */
            first_non_blank();
            col=p_col+syntax_indent;
            restore_pos(p);
            // chrisant: Style 2 (STYLE1) should not indent braces.
            if (style1 && substr(strip(get_text(100),'L'),1,1) == '{') {
               col-=syntax_indent;
            }
            return(col);
         }
      }

      status=repeat_search();
   }

}
« Last Edit: April 06, 2008, 09:12:58 AM by chrisant »