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 a Python module or standalone script
  • Supports piping, in-place editing, and batch processing

Installation

pip install scad-format

Or install from source:

git clone https://github.com/ashleyharris-maptek-com-au/scad-format
cd scad-format
pip install -e .

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-0.1.0.tar.gz (27.0 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-0.1.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for scad_format-0.1.0.tar.gz
Algorithm Hash digest
SHA256 985fb15a5a4245357a638fb4b9ec42c418d194de940bcca1ad6eaeb14372ddef
MD5 a15ed5619e7025e0ef63eb0e81346ed0
BLAKE2b-256 b19c2fd6056a102dda9666225fb4f8eec2bb29b7df73c7dd434c662a24879745

See more details on using hashes here.

File details

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

File metadata

  • Download URL: scad_format-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 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-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32bfdd69611e48d026d888cec8b75607c397b7d32918c1f36c1c8ae08ccfe61d
MD5 f2cb6b26371a9c62213ab00f3f6edb20
BLAKE2b-256 b1020fa7d3246ba0f7b283b05c650df16112d7d71393543f439f94adb9a4756a

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