Skip to main content

A code formatter for OpenSCAD files, similar to clang-format

Project description

scad-format

A code formatter for OpenSCAD files, similar to clang-format.

Features

  • Format OpenSCAD source code with configurable style options
  • Supports .scad-format configuration files (clang-format style syntax)
  • Command-line interface compatible with clang-format workflows
  • Can be used as an executable or a Python module
  • Supports piping, in-place editing, and batch processing

Installation on Windows

Download Windows executable / installer from:

https://github.com/ashleyharris-maptek-com-au/scad-format/releases

Installation on Linux

git clone https://github.com/ashleyharris-maptek-com-au/scad-format
cd scad-format
sudo source install_linux.sh

Installation as a python module

pip install scad-format

Usage

Command Line

# Format a file and print to stdout
scad-format myfile.scad

# Format in place
scad-format -i myfile.scad

# Format multiple files in place
scad-format -i *.scad

# Read from stdin
cat myfile.scad | scad-format

# Use specific style
scad-format --style="{IndentWidth: 2}" myfile.scad

# Use config file from directory tree
scad-format --style=file myfile.scad

# Dry run (show what would change)
scad-format -n myfile.scad

# Dump current config
scad-format --dump-config

As a Python Module

import scad_format

# Simple formatting
code = "module foo(){cube(1);}"
formatted = scad_format.format(code)
print(formatted)

# With custom configuration
from scad_format import FormatConfig, format_code

config = FormatConfig(IndentWidth=2, UseTab="Never")
formatted = format_code(code, config)

# Format a file
from scad_format import format_file

formatted = format_file("myfile.scad")

# Format a file in place
format_file("myfile.scad", in_place=True)

As a Standalone Script

python scad-format.py myfile.scad
python scad-format.py -i myfile.scad
cat myfile.scad | python scad-format.py

Configuration

Create a .scad-format file in your project directory (searches parent directories like clang-format):

---
# Indentation
IndentWidth: 4
TabWidth: 4
ContinuationIndentWidth: 4
UseTab: Never

# Brace style: Attach, Allman, Linux, Mozilla, Stroustrup, WebKit, GNU, Whitesmiths
BreakBeforeBraces: Attach

# Line endings: LF, CRLF, DeriveLF, DeriveCRLF
LineEnding: DeriveLF

# Optional settings
ColumnLimit: 0
SpaceAfterComma: true
SpaceBeforeParens: false
SpaceInsideParens: false
SpaceInsideBrackets: false
SpaceInsideBraces: true
SpaceAroundOperators: true
...

Configuration Options

Option Type Default Description
IndentWidth int 4 Number of spaces per indentation level
TabWidth int 4 Width of a tab character
ContinuationIndentWidth int 4 Indent for continuation lines
UseTab enum Never Tab usage: Never, ForIndentation, Always
BreakBeforeBraces enum Attach Brace breaking style
LineEnding enum DeriveLF Line ending style
ColumnLimit int 0 Max line length (0 = no limit)
SpaceAfterComma bool true Add space after commas
SpaceBeforeParens bool false Add space before parentheses
SpaceInsideParens bool false Add space inside parentheses
SpaceInsideBrackets bool false Add space inside brackets
SpaceInsideBraces bool true Add space inside braces
SpaceAroundOperators bool true Add space around operators

Brace Breaking Styles

  • Attach: module foo() { - Brace on same line
  • Allman: Brace on new line, aligned with statement
  • Linux: Like Attach, but break before function definitions
  • Stroustrup: Like Attach, but break before function definitions and catch
  • GNU: Always break, braces are indented

Development

# Install dev dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=scad_format

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scad_format-1.0.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scad_format-1.0.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file scad_format-1.0.0.tar.gz.

File metadata

  • Download URL: scad_format-1.0.0.tar.gz
  • Upload date:
  • Size: 27.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scad_format-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a545f64081b73ed626ea06123f6526368bce147901767f48eec85d89cc5b2f4d
MD5 45012ceb7553176a24c3255da2c18924
BLAKE2b-256 bdacec562c809b308c06169f4c8bc24af2715ea102d0c8e2ea40720295846776

See more details on using hashes here.

File details

Details for the file scad_format-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: scad_format-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for scad_format-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0a524f1e0b2585965715e112b49544e667df5e31db79e815973235b863b260
MD5 b0ae5e7b7d15c72b0a7a95d7ca88a6d8
BLAKE2b-256 ddb3a8140afc55167c93fa28ef1623ea723409e5952ae8a39e66963a726db203

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page