MetaEditor HelpDeveloping programsStyler

Styler

The styler quickly brings a source code design in line with the recommended standard. This makes the code look professional and easy to read. A well-designed code is much easier to analyze in the future both for its author and for other users. To launch Styler, click Styler Styler in the Tools menu or press Ctrl+,.

  • Changes in code design made by Styler can be undone. Click Undo Undo in the Edit menu or press Ctrl+Z.
  • The styler supports a variety of coding standards. The style can be changed in the editor settings. The design rules for the recommended MetaQuotes style are described in detail below.

General

The following types of action are provided to properly format the code in Styler:

  • Replacing certain lines with specified ones;
  • Aligning language operators;
  • Inserting comments to a file header;
  • Inserting comments before functions, structures, classes and their methods;
  • Inserting comments at the end of a file.

Based on the above types of actions, Styler makes the following adjustment:

Spaces and blank lines

Removing unnecessary space characters allows you to make a code more compact improving its perception. Also, Styler replaces each tab character with three spaces to avoid possible distortion of its formatting when opened in third-party programs.

Styler removes spaces:

  • At the end of lines;
  • Before ";";
  • 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 to the right and left of "," and "=".

Styler sets:

  • One space to the right and left for "||" and "&&" (result: " || " and " && ").

Other replacements:

  • In the constructions of "int* a" type, the characters "*" and "&" refer to a variable rather than type, so this design is replaced with "int *a";
  • Double blank lines are replaced by 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 Styler tab is always equal to three spaces regardless of MetaEditor settings.

Excluding replacements when formatting considering interline alignment

The Styler replacement rules specified above are not applied in some cases. This is provided in order to not violate copyright alignment on adjacent lines. Styler recognizes the interline alignment by the next line for the following keywords and symbols:

  • "//" – comment symbols;
  • "{" – opening curly bracket;
  • "}" – closing curly bracket;
  • "return" – "return" operator;
  • "delete" – "delete" operator;
  • "=" – equality symbol;
  • ":" – colon;
  • "." – full stop.

If Styler finds one of the above words or symbols at the same position as in the previous or subsequent line, then no replacements are made in it.

Formatting functions and operators

  • Two spaces from a function/operator type declaration are set before "{" and "}" symbols that open and close a function/operator.
  • Three spaces from the start of the operator are placed before each line located between "{" and "}" symbols of a function/operator.
  • In case there are unclosed "{" or "}" symbols in the operator with a text, these symbols and text are set in different lines.
  • Also aligning is performed considering the contents of the previous line. If a parenthesis is not closed or an arithmetic expression is not completed in the previous line, then the current line starts with the first position after "(" or "=" of the previous line.
  • The "else" condition is always set on a par with "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 when declaring structures, classes and enumerations

Such expressions also apply the so-called two-level tabulation:

  • Three spaces are set before specifying a type of a described member/method;
  • Names of described structure or class members or methods are indicated from the 22nd position in a line;
  • Specifiers of accessing members and methods ("private", "protected", "private" 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;
  };

Aligning comments

Comments are aligned differently in declarations and definitions:

  • First level comments in a function or method definition are aligned to the left (without indentation);
  • First level comments in a declaration are aligned by the third position in a line;
  • Comments of the following levels are aligned equally by the corresponding level of the operators they are indicated in.

Before formatting

After formatting

void myFunction()  {
//--- 1st 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);
//--- 2nd level comment
if(!state)
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
else {
ObjectSetInteger(0,name,OBJPROP_STATE,false);}
}

void myFunction() 
  {
//--- 1st 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);
      //--- 2nd 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 lines
   int m_columns;   
              //--- number of columns
   int m_button_width; 
//--- cell width
 
  };

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

Inserting comments

A comment of the following form is inserted in a file header if it is missing:

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

Comments of the following form are inserted before functions, classes, structures, etc. when it is missing:

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

A cut-off comment of the following type is inserted at the end of a file:

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