nimfmt icon indicating copy to clipboard operation
nimfmt copied to clipboard

Nim code formatter / linter / style checker

=== Nim code formatter / style checker

.Features

  • [x] Detect inconsistent variable and proc naming
  • [x] Fix inconsistent variable and proc naming
  • [x] Add/remove whitespaces and commas in function calls
  • [ ] Sort imports
  • [ ] Enforce variable/function naming style
  • Enforce whitelines (in fixed number or a range): ** [ ] before functions ** [ ] on closing blocks ** [ ] end of file ** [ ] around imports
  • [ ] Optional # to close blocks

One of the main features is to detect and correct inconsistent variable and function naming.

Using proc_naming_style = most_popular, if the same identifier appears within a file as myName more frequently and my_name and myname less frequently, nimfmt will pick the first one. This is the recommended setting to ensure consistency without having to explicitly configure nimfmt with a preferred style.

Using proc_naming_style = snake_case, if the same identifier appears as myName, my_name and myname, nimfmt will pick the second one.

==== Configuration

.nimfmt looks for configuration files is the following order:

  • current directory
  • ~/.config/nimfmt.cfg
  • ~/.nimfmt.cfg
  • /etc/nimfmt.cfg

Configuration example: [source,nim]

Fix inconsistent naming

auto - fix automatically

no - print warning only

fix_naming_style = "auto"

Choose the correct naming

most_popular - pick the most frequently used naming style for a given identifier

snake_case - prefer snake_case naming if possible

proc_naming_style = most_popular

==== Examples

Input: [source,nim]

proc my_foo( a: string, b:string,c:int, ): string = raise newException ( Exception , "foo" ) foo ( a , b , c ) d [ a ] = 3 discard "string to discard"

break return "string to return"


Output: [source,nim]

proc my_foo(a: string; b: string; c: int): string = raise newException(Exception, "foo") foo(a, b, c) d[a] = 3 discard "string to discard" break return "string to return"

==== Usage

[source,bash]

nimfmt <filename.nim> [<filename.nim> ... ] [ -p ] output file prefix [ -s ] output file suffix [ -c , ] configuration file location(s) (default: ./.nimfmt.cfg ~/.nimfmt.cfg) [ -i ] update files in-place (dangerous!) [ -w ] overwrite existing files (automatically enabled when using -i) [-v] version [-h] this help

If any of -p ..., -s ... or -i are specified the output will be written to disk, otherwise to stdout

==== Installation

[source,bash]

nimble install https://github.com/FedericoCeratto/nimfmt