Skip to content

Add Native Code formatter #129

@notJoon

Description

@notJoon

Description

Implements native code formatting functionality similar to gofmt/gofumpt as a built-in feature.

Commands

Basic Format Command

tlin fmt [flags] [path...]

Formats the code in the specified paths. If no path is specified, it reads from stdin.

Flags

General Options

  • -l, --list
    • List files that would be reformatted
    • Does not actually format the files
  • -w, --write
    • Write formatted content to source files
    • Without this flag, prints to stdout
  • -d, --diff
    • Display diffs of formatting changes
  • -s, --simplify
    • Simplify code where possible (like gofumpt)

Style Options

  • --indent-style=<tab|space>
    • Indentation style (default: tab)
  • --indent-width=<n>
    • Number of spaces per indentation level when using spaces (default: 4)
  • --line-length=<n>
    • Maximum line length (default: 100)
  • --no-rewrite-imports
    • Disable import block rewriting/reordering

Examples

# Format a single file
tlin fmt file.gno

# Format and overwrite multiple files
tlin fmt -w file1.gno file2.gno

# Format all Go files in current directory and subdirectories
tlin fmt -w .

# Show formatting changes without applying them
tlin fmt -d file.gno

# List files that need formatting
tlin fmt -l .

# Format using spaces instead of tabs
tlin fmt --indent-style=space file.gno

# Format with simplified code style
tlin fmt -s file.gno

Behavior

Code Simplification (-s flag)

Integrate with auto fixer

When the -s flag is used, the formatter will apply additional simplifications similar to gofumpt:

  1. Standardize empty lines

    • Single empty line between functions
    • No empty lines at start/end of block
    • Group related blocks with empty lines
  2. Import blocks

  3. Code patterns

    • Remove redundant type declarations
    • Simplify redundant boolean expressions
    • Use short variable declarations where possible

Exit Codes

  • 0: Success (no formatting changes needed or changes applied successfully)
  • 1: Syntax errors in input
  • 2: Usage error (invalid flags, etc.)

Configuration

The formatter can be configured through .tlin.yaml configuration file:

formatter:
  indent-style: tab
  indent-width: 4
  line-length: 100
  simplify: false
  rewrite-imports: true

Command line flags take precedence over configuration file settings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P-highPriority: HighT-formatType: related with formatter

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions