Author Topic: C/C++ Beautifier Question about Launguage Specific Aliases  (Read 7655 times)

sdonepudi

  • Community Member
  • Posts: 23
  • Hero Points: 0
C/C++ Beautifier Question about Launguage Specific Aliases
« on: August 27, 2008, 03:39:39 PM »
It seems C/C++ Beautifier does not look at user defined Language Specific Aliases. Is there anyway I can make the beautifier honor user defined Language Specific Aliases ?.

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: C/C++ Beautifier Question about Launguage Specific Aliases
« Reply #1 on: September 05, 2008, 10:19:46 AM »
It seems C/C++ Beautifier does not look at user defined Language Specific Aliases. Is there anyway I can make the beautifier honor user defined Language Specific Aliases ?.

Can you give an example of what you mean?

How would you expect the beautifier to recognize code that was generated from an alias and why would you want it formatted differently to the rest of your code.

Graeme

sdonepudi

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: C/C++ Beautifier Question about Launguage Specific Aliases
« Reply #2 on: September 05, 2008, 02:15:59 PM »
Example I like my cae statement to be like this.
switch()
{
    case 1:
    {
        break;
    }
}

When we use Slick edit beatifier it beautifies like this.

switch()
{
    case 1:
       {
           break;
       }
}

Other beautfiers like uncrustify has option to beautify the way I want not Slickedit.

chrisant

  • Senior Community Member
  • Posts: 1410
  • Hero Points: 131
Re: C/C++ Beautifier Question about Launguage Specific Aliases
« Reply #3 on: September 05, 2008, 04:19:03 PM »
I don't understand what that has to do with aliases.

The SlickEdit beautifier isn't very flexible, but maybe you could make a macro that runs uncrustify and reloads the file?

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: C/C++ Beautifier Question about Launguage Specific Aliases
« Reply #4 on: September 06, 2008, 01:57:05 AM »
I guess you realise the braces surrounding the statements following the case option are redundant unless you're declaring variables and want to limit their scope ??

Here's a macro that un-indents the case blocks for you.  unindent_case_block does one, the _all version does the whole of the current buffer.  To use it, save the code to a file and load it using the load module command on the macro menu.  It should be fairly obvious what the code is doing so you can tweak it if there are special cases it doesn't handle.

Graeme


Code: [Select]
#include "slick.sh"

#pragma option(strictsemicolons,on)
#pragma option(strict,on)
#pragma option(autodecl,off)
#pragma option(strictparens,on)


static _str do_unindent_case_block()
{
   typeless p, p2;
   _deselect();
   // search for keywords 'case' or 'default'
   if (search('case|default','RIH?CK,,'))
      return 'Done';
   _save_pos2(p);
   int pl = p_line;
   int pc = p_col;

   // find a colon
   if (search(':','IH?C3,,'))
      return 'Missing colon. Line ' :+ p_line;

   if (p_line != pl)
      return 'Misplaced colon.  Line ' :+ p_line;
   right();

   // search for first non whitespace, not in comment, not forward slash
   if (search('[ \n\r\t]*\c[^ \n\r\t\/]','IRH?,XC,'))
      return 'Syntax error at line ' :+ p_line;

   int pc2 = p_col;

   // if first non whitespace is not { or not indented then skip
   if (get_text() != '{' || p_line == pl || p_col <= pc)
      return 'Skip';

   // remember the col/line of the opening brace
   int plb = p_line;
   int pcb = p_col;
   _save_pos2(p);
   if (find_matching_paren(true))
      return 'Error - no matching paren. Line ' :+ p_line;
   _deselect();
   _save_pos2(p2);
 
   if (p_col != pc2) {
      pl = p_line;
      _restore_pos2(p);
      return 'Mis-aligned brace at line ' :+ pl;
   }
   // guard against missing brace, search backwards for opening brace
   if (search('{','IH-?C1,XC,')) {
      _restore_pos2(p);
      return 'Lost opening brace!!';
   }

   if (p_line != plb || p_col != pcb) {
      _restore_pos2(p);
      return 'Brace matching failed';
   }

   _restore_pos2(p2);
   _select_line();
   _restore_pos2(p);
   _select_line();
   unindent_selection();
   _deselect();
   _restore_pos2(p2);
   _end_line();
   return '';
}


_command void unindent_case_block(boolean all = false, boolean from_cursor = false) name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   typeless p;
   int k = 0;
   if (!all) {
      message(do_unindent_case_block());
      return;
   }

   if (!from_cursor)
      top();

   while (++k < 60000) {
      _save_pos2(p);
      _str res = do_unindent_case_block();
      switch (res) {
         case '' :
            // un_indenting occurred so repeat in case it was doubly indented
            _restore_pos2(p);
            continue;
         case 'Skip' :
            continue;
         case 'Done' :
            return;
      }
      _message_box(res);
      return;
   }
   _message_box('Done 60000 ??????????????????????');
}


_command void unindent_case_block_all(boolean from_cursor = false) name_info(','VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   typeless p;
   _save_pos2(p);
   unindent_case_block(true, from_cursor);
   _restore_pos2(p);
}



sdonepudi

  • Community Member
  • Posts: 23
  • Hero Points: 0
Re: C/C++ Beautifier Question about Launguage Specific Aliases
« Reply #5 on: September 08, 2008, 07:36:16 PM »
Hi Graeme,
              I appreciate your help.
Thanks,
sdonepudi