Rocket Uniface User Forum

 View Only

Code Formatting in the IDE

By Kathy Larson posted 06-05-2024 10:49

  

by Jonke Korzelius

We have enhanced the latest versions of Uniface with the option to have Uniface format your ProcScript. With ProcScript code formatting, you can correct or improve your code indentation to increase readability and understanding. You can apply formatting on a selected portion of code using the Format Selection option or format the entire document using the Format Document option, both available from the Code Editor’s popup menu. You can change the shortcut keys for these menu options to your liking in the [USERKEYS] section of the usys.ini file. The default is Alt+F (Selection) and Alt+Shift+F (Document).

Formatting rules

  1. Indent all code blocks, such as if, endif, while, endwhile, params, variables, etc.
  2. It does not indent block data, line continuation in strings, and JavaScript inside proc-script.
  3. The Undo (Ctrl+Z) action restores the code to the original state of the code before the formatting action.
  4. Code indentation is not currently applicable to the HTML editor.

#tofp

Indentation rules

Uniface has zero-based keywords (where zero means the starting point in your line of code). The following keywords are marked as zero-based, and thus indentation does not apply to these keywords:

  • trigger
  • operation
  • params
  • endparams
  • variables
  • endvariables
  • weboperation
  • throws
  • returns
  • end
  • defines
  • Pre-compiler definitions, such as #comment, #ifdef, etc.

Single indentation parts

  • Code in general
  • Variables declaration block
  • Parameter declaration block

Indentation increase

The following keywords increase the indentation by the number of spaces defined in the settings.

  • if - The next line(s) will be indented with the indentation definition.
  • else - Uses indentation level of ‘if’ for the lines to follow. ‘else’ comes on the same level as the connected ‘if’ statement.
  • elseif - Uses indentation level of 'if'.
  • while
  • repeat
  • selectcase
  • elsecase
  • case - Any subsequent case does not increase the indentation.
  • for

Indentation decrease

The following keywords decrease the indentation by the number of spaces defined in the settings.

  • endif
  • endwhile
  • endselectcase
  • until
  • endfor

Behavior and limitations

The formatting feature relies on ProcScript keywords, such as if, endif, while, and endwhile to determine indentation. However, limitations arise when the selection lacks any keyword. If the selection comprises only a comment line or a single if line, the formatter might produce unexpected results. To prevent this, ensure the selection provides ample context. For example, select a complete operation or begin the selection on a formatting keyword. Initiating the selection with an if serves as an anchor point, ensuring the rest of the selection is formatted relative to this keyword. This approach to selection helps avoid unexpected outcomes. In cases where the result is not as expected, you can easily undo the change with CTRL+Z or re-select a larger portion and apply formatting again. Expanding the selection provides the formatter with more context, resulting in a more accurate outcome.

Pre-compiler directives

When we have #define or #comment pre-compiler directives as part of the regular code, the directives will follow the indentation of the code.

For example:

#startdefine 1
operation MY_OPER
  #define DEF1 = value1  ;- is indented
  #ifdefined CLASSIC
    #comment Classic code
    if (something = <DEF1>)
      putmess "Classic something”
    endif
   #endif
#enddefine
end

If the #define, #include, and #comment lines are located outside of the operation in the code, the formatting will place these lines at position zero.

Comment lines

When you select only a comment line in an operation, trigger or entry, the comment line moves to position zero. The editor does not know where you are at this moment and assumes you are selecting outside the operation. As soon as there is a formatting keyword in the selection, such as if or while, the editor assumes you are in an operation. These proc-script keywords trigger the code formatting to let the comment lines follow the standard code indentation rules.

Block data and line continuation

Lines in block data are not formatted when the Format option is applied, the same is true for code which is part of a line continuation.

Line continuation example:

operation check_dogs
  while ($status == 0)
    if ($dogs$ == "T”)
      $message_string$ = "The quick brown fox%\
jumps over the lazy dog"
    endif
  endwhile
end

The Uniface formatter does not touch the line after the line continuation.

Block data example:

example:blockdata+
This is
  some
  block
    data
+


The Uniface formatter skips the text between the '+' signs.

Configurable Formatting rules

As different organizations may use different code formatting conventions, Uniface provides settings so you can configure your formatting rules.

The [FORMATTING] section of the assignment file allows you to assign rules and customize the code formatting tool in the ProcScript Editor. If a rule is not defined, Uniface uses the default value.

Here’s an overview of the rules available:

  • AlignVariables - Align the contents of 'variables' blocks into perfect columns. The columns may contain (in the following order): Datatype and type-specific attributes, Parameter name, Direction, and Comment (optional). Default is True.
  • AlignParams - Align the contents of 'params' blocks into perfect columns. The columns may contain (in the following order): Datatype and type-specific attributes, Variable name, and Comment (optional). Default is True.
  • IndentVariables - Add extra indentation to the 'variables' block. By default, the block's indentation starts from zero. This is only effective when the block is placed into a trigger, operation, function, etc. Component variables are not affected. Default is False.
  • IndentParams - Add extra indentation to the 'params' block. By default, the block's indentation starts from zero. Default is False.
  • IndentScope - Add extra indentation to the 'scope' block. By default, the block's indentation starts from zero. Default is False.
  • IndentTabs - When true, indentation is performed with tabs. When false, indentation is performed with spaces. Default is True.
  • IndentSize - When using tabs, IndentSize determines the width of a tab. When using spaces, IndentSize determines the number of spaces that represent a single tab. Default is 4.

Example of the formatting section in your assignment file:

[formatting]

AlignVariables=true

AlignParams=true

IndentVariables=false

IndentParams=false

IndentScope=false
IndentTabs=false
IndentSize=2

Enforcing ProcScript Code Conventions

Here we explain how you can enforce the usage of the same rules across your organization so that all developers use them.

As explained, to set custom formatting rules, you must define a new subsection in your ASN file named [FORMATTING]. The rules are best placed in an ASN file that is used for your Project, that is, the default ide.asn file, or your own ASN file. 

You can also use a separate file that is included from your ASN file. For this option, the file name and extension are not important. The file should contain the [FORMATTING] header and the rules. The file can then be included from any ASN file, using #file, for example - #file Z:\rules\rules.asn, or #file Z:\.policy. Ideally, the formatting rules should reside in a read-only location, so you can enforce its usage across your organization.

0 comments
23 views

Permalink