Skip to content

lmichaudel/c3fmt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo banner

A customizable code formatter for the C3 language, written in C3.

Usage

Usage:

c3fmt [<options>] <files>

Options:

-h, --help       - Show this help.
-v, --version    - Show current version.
--in-place       - Format files in place.
--stdin          - Read input from stdin.
--stdout         - Output result to stdout.
--config=<path>  - Specify a config file.
--default        - Force default config.

Configuration

c3fmt will try to find a .c3fmt format inside the working directory. You can also pass your own path to c3fmt, or force the default configuration.

You can look at .c3fmt for the default configuration.

Building

Run c3c build to build c3fmt.

The only dependency is the tree-sitter sdk.

Planned features / wishlist

  • Code wrapping (current breaks with nested constructions).
  • Align consecutive assignments / declaration / comments.
  • Wrapping indent option. (ContinuationIndentWidth)
  • Align wrapped items.
  • Space before <...> options.
  • Import sorting
  • Space before trailing comment
  • Pointer alignment ? (kind of a pain and I think right alignment is heretic)

About the parser

c3fmt use tree-sitter to generate a tree that is then walked down.

For the moment it's very experimental and doesn't even support the whole language. If c3fmt doesn't know how to format a bit of code, it will not touch it. Please report any of these incidents, I tested the formatted against the whole std but maybe missed some grammar points.

c3fmt won't change any non whitespace character in your code, except :

  • Line comments between a statement and its scope :
if (a >= 0) // Check if a >= 0
{ ... }

would become

if (a >= 0) /* Check if a >= 0 */
{ ... }

or

if (a >= 0) /* Check if a >= 0 */ {
    ...
}

depending on brace style.

Tests

There are two tests for the moment :

  • Corpus takes a .c3 source file, format it and compare it the same file with a _f.c3 format corresponding to the expected output.
  • Stdlib formats every file from the C3 standard library, then verifiy it still compiles with the same semantic. (For the moment it won't automatically try to compile it, but you can go to test/stdlib and run c3c build)

Each test checks that the syntax tree of the formatted code is the same as the original one.

About

A code formatter for the C3 language. Written in C3.

Topics

Resources

License

Stars

Watchers

Forks

Languages