MetaEditor Help

Styler

This function is intended for formatting the source code in accordance with the recommended standard. This allows making the source code readable and look like a professional one. Correctly formatted code is much easier for further analysis both by its author and other users. In order to start the styler one should execute the "Styler Styler" command in the "Tools" menu or use the "Ctrl+," key combinations.

The changes of the source code formatting made by the styler can be rolled back using the "Undo Undo" command in the "Edit"  menu, in the "Standard" toolbar or by pressing the "Ctrl+Z" key combination.

Common Description

The following types of actions with the source code are implemented in the styler for the correct formatting:

  • Replacing specified lines by the other specified ones;
  • Alignment of operators of the language;
  • Insertion of comments to the file header;
  • Insertion of comments before functions, structures, classes and their methods;
  • Insertion of comments at the end of file.

The styler performs the following corrections based on the action types mentioned above:

Spaces and Empty Lines

The deletion of unnecessary space symbols allows to make the source code compact and improve its perception. Also the styler replaces each tab symbol with three space symbols, it is necessary to avoid the distortion of the source code formatting when it is opened in a third-party software.

Styler Removes Spaces:

  • At the end of lines;
  • Before ";" symbol;
  • Between "if" and "(", "while" and "(", "for" and "(", "switch" and "(", "return" and "(".
  • After ";" inside "for()";
  • After "!";
  • Near "(" and ")";
  • Near "<" and ">";
  • Near "|" and "^";
  • Near "+", "-", "/" and "%";
  • Near "[" and "]";
  • Near "==" and "==";
  • Near "!=" and "!=";
  • Near "<=" and ">=";
  • Near "+=" and "+=";
  • Near "-=" and "-=";
  • Near "*=" and "*=";
  • Near "/=" and "/=";
  • Near "<<" and ">>";
  • One space at a time to the right and left of "," and "=".

Styler Places:

  • One space at a time to the right and left of  "||" and "&&" (result is " || " and " && ").

Other Substitutions:

  • In the constructions like "int* a" the "*" and "&" symbols refer to the variable but not to the type, so this constructions is replaced by "int *a";
  • Double empty lines are replaced with one.

Before Formatting

After Formatting

void myFunction() 
  {
   if   (I  <  10)
  {
   printf (“Hello\n”) ;
   if (< 5 )
     {
             printf (“i<5 !\n”) ;
             if (< 3)
               {
                printf (“i<3 !\n”) ;
                if (< 2)
                  {
                  }
               }
            }
  }
     }

void myFunction()
  {
   if(I<10)
     {
      printf(“Hello\n”);
      if(I<5)
        {
         printf(“i<5 !\n”);
         if(i<3)
           {
            printf(“i<3 !\n”);
            if(i<2)
              {
              }
           }
        }
     }
  }

The tab size is always equal to three spaces regardless of settings of the MetaEditor.

Exceptions of Formatting Substitution with a Glance to Interline Alignment

The rules of substitution performed by the styler are not applied in some cases. It is implemented to keep an author's alignment by adjacent lines. The styler recognizes the interline alignment by adjacent line for the following keywords and symbols:

  • "//" — symbols of a "C" comment;
  • "{" — left curly bracket;
  • "}" — right curly bracket;
  • "return" — the "return" operator;
  • "delete" — the "delete" operator;
  • "=" — the equality symbol;
  • ":" — colon;
  • "." — dot.

If the styler during the analysis finds one of the above mentioned words or symbols at the same positions as it is at the previous line then the substitutions are not performed.

Formatting of Functions and Operators

  • Two spaces from the declarations of the type of the function/operator are set before the "{" and "}" symbols that open and close the function/operator.
  • Three spaces from the beginning of the operator are set before each line located between the "{" and "}" symbols of the function/operator.
  • In case of unclosed "{" or "}" symbols in an operator with some text within them, those symbols are placed at different lines.
  • The alignment is also performed with taking the previous line into account. If the previous line contains an unclosed bracket or an unfinished arithmetical expression then the current line will start from the first position after "(" or "=" of the previous line.
  • The "else" condition is always placed at the same positions as "if";

Before Formatting

After Formatting

void myFunction()  {
int k[10],t=0;
for(int i=0;i<10;i++){
   k[i]=i;
   t+=i;}   
if  (button==name) {
bool state=ObjectGetInteger(0,name,OBJPROP_STATE);
if(!state)
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
else {
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
}

void myFunction() 
  {
   int k[10],t=0;
   for(int i=0;i<10;i++)
     {
      k[i]=i;
      t+=i;
     }
   if(button==name) 
     {
      bool state=ObjectGetInteger(0,name,OBJPROP_STATE);
      if(!state)
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
     }
   else 
     {
      ObjectSetInteger(0,name,OBJPROP_STATE,false);
     }
  }

Alignment of Declaration of Structures, Classes and Enumerators

In such kinds of expression the so-called double level tabulation is used:

  • Three spaces are placed before the indication of the type of the described member/method;
  • The names of members or methods of the class or structure are specified starting from the 22 position in the line;
  • The specificators of access to the members and methods ("private", "protected", and "virtual") are aligned by "class" word.

Before Formatting

After Formatting

class CTradePad  {
 private:
 int m_rows;
 int m_columns;
 int m_button_width;
 int m_button_height;
 int m_top;
 int m_left;
 int m_left_previous_header;
  };

class CTradePad  
  {
private:
   int               m_rows;
   int               m_columns;
   int               m_button_width;
   int               m_button_height;
   int               m_top;
   int               m_left;
   int               m_left_previous_header;
  };

Alignment of Comments

There is a difference between the alignment of comments in declarations and definitions:

  • Comments of the first level are aligned to the left (without an indent) in the definitions of functions and methods;
  • Comments of the first level in declaration are aligned by the third position in the line;
  • Comments of the following levels are aligned by the corresponding levels of operators in which they are specified.

Before Formatting

After Formatting

void myFunction()  {
//--- 1-st level comment
int k[10],t=0;
for(int i=0;i<10;i++){
   k[i]=i;
   t+=i;}   
if  (button==name) {
bool state=ObjectGetInteger(0,name,OBJPROP_STATE);
//--- 2-nd level comment
if(!state)
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
else {
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
}

void myFunction() 
  {
//--- 1-st level comment
   int k[10],t=0;
   for(int i=0;i<10;i++)
     {
      k[i]=i;
      t+=i;
     }
   if(button==name) 
     {
      bool state=ObjectGetInteger(0,name,OBJPROP_STATE);
      //--- 2-nd level comment
      if(!state)
         ObjectSetInteger(0,name,OBJPROP_STATE,false);
     }
   else 
     {
      ObjectSetInteger(0,name,OBJPROP_STATE,false);
     }
  }

class CTradePad
  {
private:
   int m_rows;
 //--- number of rows
   int m_columns;   
              //--- number of columns
   int m_button_width; 
//--- cell width
 
  };

class CTradePad
  {
private:
   int               m_rows;
   //--- number of rows
   int               m_columns;
   //--- number of columns
   int               m_button_width;
   //--- cell width
 
  };

Insertion of Comments

The comments of the following type are inserted to the file header if it is absent:

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                    Copyright © 2009, CompanyName.|
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+

The comments of the following type are inserted before functions, classes, structures, etc. if there is none:

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+

The following intercepting comment is inserted at the end of the file:

//+------------------------------------------------------------------+