Script Editor: Basic Code Formatter Plugin – Arma Reforger

From Bohemia Interactive Community
Jump to navigation Jump to search
(Add spellcheck warning)
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Infobox/WorkbenchPlugin
|name= Basic Code Formatter
|editor= script
|descr= A plugin to help formatting code and following {{Link|Arma Reforger:Scripting: Conventions|conventions}} and {{Link|Arma Reforger:Scripting: Best Practices|good practices}}
|shortcut= <nowiki/>
* {{Controls|Ctrl|Shift|K}}
* {{Controls|Ctrl|Alt|Shift|K}} (forced)
|file= {{Link|enfusion://ScriptEditor/scripts/WorkbenchGame/ScriptEditor/SCR_BasicCodeFormatterPlugin.c}}
}}
'''Basic Code Formatter''' is a plugin that helps formatting code to {{Name|bi}} standards as well as warns for bad practice.
'''Basic Code Formatter''' is a plugin that helps formatting code to {{Name|bi}} standards as well as warns for bad practice.


Line 21: Line 30:


{{Feature|informative|{{Controls|Ctrl|Alt|Shift|K}} can be used to force formatting of all the current file's lines.}}
{{Feature|informative|{{Controls|Ctrl|Alt|Shift|K}} can be used to force formatting of all the current file's lines.}}
== Features ==
=== Modification ===
It can:
* trim line ends (trailing spaces and tabs)
* fix indentation (turn four spaces into a tab, remove extra spaces)
* auto-format method separators - all it takes is <enforce inline>//---</enforce> three dashes
* do general formatting:
** remove semicolons {{hl|;}} after a class-closing bracket
** {{hl|if(}} / {{hl|while(}} / {{hl|foreach(}}, etc
** double spaces, double semicolons, capital NULL, etc<br>{{Feature|informative|See <enforce inline>SCR_BasicCodeFormatterPlugin.GeneralFormatting()</enforce>.}}
* add a final line return to the file
{{Feature|informative|The plugin does not format comment or string content.}}
=== Warnings ===
It warns about:
* wrong syntax (e.g {{hl|new ref}})
* improvable syntax (e.g {{hl|array<string> strings {{=}} new array<string>();}} can be reduced to {{hl|array<string> strings {{=}} {};}})
* multiple empty lines
* usage of the {{hl|{{Link|Arma Reforger:Scripting: Keywords#auto|auto}}}} and {{hl|{{Link|Arma Reforger:Scripting: Keywords#autoptr|autoptr}}}} keywords
* divisions that could be multiplications (e.g {{hl|value / 2}} is {{hl|value * 0.5}}), as multiplications are usually cheaper in term of CPU
* unbracketed loops ({{hl|if}}, {{hl|for}}, {{hl|foreach}}, {{hl|while}})
* {{hl|if}} one-liners - always put the "then" part on a new line
* badly named variables and constants (e.g {{hl|string m_iValue}} where the prefix for string is s - see {{Link|Arma Reforger:Scripting: Values#String|Values - String}})
* {{Link/Enfusion|armaR|ScriptInvoker}} direct usage (see {{Link|Arma Reforger:ScriptInvoker Usage}})
* a forgotten {{hl|Print()}} call (a Print/PrintFormat without log level is considered a temporary debug one)
* non-tag-prefixed classes/enums (e.g {{hl|EMyEnum}} vs {{hl|TAG_EMyEnum}})
* ''some'' spelling mistakes in comments (e.g "solider", "overriden", "dammage"...)




Line 29: Line 71:
! After
! After
|-
|-
| <enforce noGuess>
| <enforce noGuess noTrim>
class abc:Managed // must warn for non-prefixed class + must space around ':'
class abc:Managed // must warn for non-prefixed class + must space around ':'
{
{
Line 46: Line 88:


// must remove trailing empty space (below)
// must remove trailing empty space (below)
}  
} 
// must remove the final semicolon below
// must remove the final semicolon below
};
};
Line 89: Line 131:




{{GameCategory|armaR|Modding|Tutorials|Official Tools|Script Editor Plugins}}
{{GameCategory|armaR|Modding|Official Tools|Script Editor Plugins}}

Latest revision as of 12:36, 16 September 2024

Basic Code Formatter

Script Editor plugin

  • Ctrl + ⇧ Shift + K
  • Ctrl + Alt + ⇧ Shift + K (forced)

A plugin to help formatting code and following conventions and good practices

File: SCR_BasicCodeFormatterPlugin.c

Basic Code Formatter is a plugin that helps formatting code to Bohemia Interactive standards as well as warns for bad practice.

It features:

  • General space formatting
  • Line end trimming
  • Indentation fix from spaces to tabs
  • Method separator fix
  • Auto line end at the end of the file
  • Scripting prefix check
  • Batch processing (all addon scripts at once)
  • The option to only formats modified lines (if using SVN/Git)
  • Bad practice warnings
  • A demo mode to practice in read-only.


Usage

It is triggered by Ctrl + ⇧ Shift + K; depending on the selected options, it will either process the current file or the selected addon's files.

Ctrl + Alt + ⇧ Shift + K can be used to force formatting of all the current file's lines.


Features

Modification

It can:

  • trim line ends (trailing spaces and tabs)
  • fix indentation (turn four spaces into a tab, remove extra spaces)
  • auto-format method separators - all it takes is //--- three dashes
  • do general formatting:
    • remove semicolons ; after a class-closing bracket
    • if( / while( / foreach(, etc
    • double spaces, double semicolons, capital NULL, etc
      See SCR_BasicCodeFormatterPlugin.GeneralFormatting().
  • add a final line return to the file
The plugin does not format comment or string content.

Warnings

It warns about:

  • wrong syntax (e.g new ref)
  • improvable syntax (e.g array<string> strings = new array<string>(); can be reduced to array<string> strings = {};)
  • multiple empty lines
  • usage of the auto and autoptr keywords
  • divisions that could be multiplications (e.g value / 2 is value * 0.5), as multiplications are usually cheaper in term of CPU
  • unbracketed loops (if, for, foreach, while)
  • if one-liners - always put the "then" part on a new line
  • badly named variables and constants (e.g string m_iValue where the prefix for string is s - see Values - String)
  • ScriptInvoker direct usage (see ScriptInvoker Usage)
  • a forgotten Print() call (a Print/PrintFormat without log level is considered a temporary debug one)
  • non-tag-prefixed classes/enums (e.g EMyEnum vs TAG_EMyEnum)
  • some spelling mistakes in comments (e.g "solider", "overriden", "dammage"...)


Example

Before After
class abc:Managed // must warn for non-prefixed class + must space around ':' { [Attribute()]; // must remove the semicolon protected ScriptInvokerVoid Event_OnSomething; protected static string m_sValue3; // must fix the separator (below) //--- protected void Method() { // must replace 5 spaces by one tab if(true ) return; // must reformat the if and warn about one-liner // must warn about two empty lines (below) // must remove trailing empty space (below) }  // must remove the final semicolon below }; // must add a line return (below)
class abc : Managed // must warn for non-prefixed class + must space around ':' { [Attribute()] // must remove the semicolon protected ScriptInvokerVoid Event_OnSomething; protected static string m_sValue; // must fix the separator (below) //------------------------------------------------------------------------------------------------ protected void Method() { // must replace 5 spaces by one tab if (true) return; // must reformat the if and warn about one-liner // must warn about two empty lines (below) // must remove trailing empty space (below) } // must remove the final semicolon below } // must add a line return (below)
Changelog
SCRIPT       : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SCRIPT       : SCR_BasicCodeFormatterPluginExample.c - 7 lines changed, 1× line trimming, 1× indent fix(es) at line(s) 11, 5 formattings, 1 end spaces trimming, 1 4-spaces indent -> tabs replaced, 1 space(s) in indentation removed, 1 method separators fixed, added final newline (read: 3 ms, format: 83 ms, diff: 81 ms - total: 167 ms)
SCRIPT       : Checking all 20 lines (100% of the file)
SCRIPT       : 1× multiple consecutive empty lines found at line(s) 14 - leave only one
SCRIPT       : 2× badly-named variables found at line(s) 5-6 - use proper prefixes: m_s for ResourceName/string, m_v for vectors, NO m_p, CASED_CONSTS, etc
SCRIPT       : 1× non-prefixed class/enum found at line(s) 1 - classes and enums should be prefixed; see the settings to setup accepted prefixes (current SCR_)