CodeFormatter`
CodeFormatter`

CodeFormat

CodeFormat["code"]

formats a string of WL code returning a string.

CodeFormat[File[src]]

formats a file of WL code returning a string.

Details and Options

  • The following options can be given:
  • Airiness 0high-level control of amount of whitespace and newlines
    "IndentationString" " "string to use for indentation
    "LineWidth" 78width of lines before line breaking occurs
    "NewlineString" "\n"string to use for newlines
    "NewlinesBetweenCommas"Automaticlow-level control for newlines between commas
    "NewlinesBetweenSemicolons"Automaticlow-level control for newlines between semicolons
    "NewlinesBetweenOperators"Automaticlow-level control for newlines between operators
    "NewlinesInComments"Automaticlow-level control for newlines in comments
    "NewlinesInControl"Automaticlow-level control for newlines in control structures
    "NewlinesInGroups"Automaticlow-level control for newlines in groups
    "NewlinesInScoping"Automaticlow-level control for newlines in scoping constructs
  • Airiness can be a value between -1 and 1.
  • Settings for "IndentationString" must result in equivalent code.
  • Settings for "NewlineString" must result in equivalent code.
  • Possible settings for the low-level newline options are:
  • Automaticuse default behavior that relies on heuristics
    Insertalways insert newlines
    Deletealways delete newlines
  • Values for Airiness map directly to different values for low-level newline options.
  • Airiness of 0 is equivalent to all low-level newline options having value Automatic.
  • Airiness of -1 is equivalent to all low-level newline options having value Delete.
  • Airiness of 1 is equivalent to all low-level newline options having value Insert.

Examples

open allclose all

Basic Examples  (1)

Format WL code:

Files can also be formatted:

Options  (4)

Airiness  (1)

Default airiness:

Minimal airiness:

Maximal airiness:

"IndentationString"  (1)

No indentation:

Settings that do not result in equivalent code are not allowed:

"LineWidth"  (1)

Allow long lines:

Allow short lines:

"NewlineString"  (1)

Allow extra newlines:

Settings that do not result in equivalent code are not allowed:

Possible Issues  (1)

A string representing a file path is treated as code.

File paths must use the File wrapper.