Metadata-Version: 2.4
Name: beautifhy
Version: 1.2.6
Summary: A Hy pretty-printer / code formatter / beautifier.
Author-email: Ati Sharma <ati+beautifhy@agalmic.ltd>
License: MIT License
        
        Copyright (c) 2024 the authors.
        
        Permission is hereby granted, free of charge, to any person obtaining a
        copy of this software and associated documentation files (the "Software"),
        to deal in the Software without restriction, including without limitation
        the rights to use, copy, modify, merge, publish, distribute, sublicense,
        and/or sell copies of the Software, and to permit persons to whom the
        Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
        THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
        FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
        DEALINGS IN THE SOFTWARE.
        
Project-URL: Repository, https://github.com/atisharma/beautifhy
Keywords: hy,hylang,utilities,automation,formatter
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Lisp
Classifier: Programming Language :: Hy
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Quality Assurance
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Utilities
Requires-Python: >=3.13
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: AUTHORS
Requires-Dist: hy>=1.2.0
Requires-Dist: hyrule>=1.0.0
Requires-Dist: multimethod>=2.0
Requires-Dist: pygments
Requires-Dist: toolz
Dynamic: license-file

## 🦑 Beautifhy

*A [Hy](https://hylang.org) beautifier / code formatter / pretty-printer / linter.*

Probably compatible with Hy 1.0.0 and later.


### Install

```bash
$ pip install -U beautifhy
```

The pygments style may be modified with the environment variable
`HY_PYGMENTS_STYLE`. This sets the name of a pygments style to use for
highlighting. Defaults to `lightbulb`.


### Usage: pretty-printer and syntax highlighter

From the command line, to pretty-print the file `core.hy`:
```bash
$ beautifhy core.hy
```
gives the output

```hylang
(import toolz [first second last])

(defmacro defmethod [#* args]
  "Define a multimethod (using multimethod.multimethod).
  For example, the Hy code

  `(defmethod f [#^ int x #^ float y]
    (// x (int y)))`

  is equivalent to the following Python code:

  `@multimethod
  def f(x: int, y: float):
      return await x // int(y)`

  You can also define an asynchronous multimethod:

  `(defmethod :async f [#* args #** kwargs]
    (await some-async-function #* args #** kwargs))`
  "
  (if (= :async (first args))
    (let [f (second args) body (cut args 2 None)]
      `(defn :async [hy.I.multimethod.multimethod] ~f ~@body))
    (let [f (first args) body (cut args 1 None)]
      `(defn [hy.I.multimethod.multimethod] ~f ~@body))))

(defn slurp [fname #** kwargs]
  "Read a file and return as a string.
  kwargs can include mode, encoding and buffering, and will be passed
  to open()."
  (let [f (if (:encoding kwargs None) hy.I.codecs.open open)]
    (with [o (f fname #** kwargs)]
      (o.read))))

(defmacro rest [xs]
  "A slice of all but the first element of a sequence."
  `(cut ~xs 1 None))
```

To apply syntax highlighting (no pretty-printing), do
```bash
$ hylight core.hy
```

You can use stdin and pipe by replacing the filename with `-`:
```bash
$ beautifhy core.hy | hylight -
```
which will pretty-print `core.hy` and then syntax highlight the output.


To convert python code to Hy (using [py2hy](https://github.com/hylang/py2hy)), autoformat, then apply syntax highlighting, do
```bash
$ pip3 install py2hy
$ python3 -m py2hy some_code.py | beautifhy - | hylight -
```

### Usage: linter

`hylint` checks Hy source for syntax errors and common issues.

```bash
$ hylint myfile.hy
```

With `--style` / `-s`, also runs opinionated style checks (camelCase names,
missing docstrings, earmuff variables):

```bash
$ hylint --style myfile.hy
```

With `--error-only` / `-e`, only errors are shown (suppresses warnings and info):

```bash
$ hylint --error-only myfile.hy
```

Reads from stdin with `-`:
```bash
$ cat myfile.hy | hylint -
```

Exit code is 1 if any errors are found, 0 otherwise. Suitable for CI.

**Lint rules (always on):**
- `(defn f (x) ...)` — use `[]` for parameter lists, not `()`
- `(defn f [...] (do ...))` — remove redundant `do`
- `(fn [...] (do ...))` — remove redundant `do`
- `(when cond (do ...))` — remove redundant `do`
- `(if cond (do ...))` — use `(when cond ...)`
- `(do expr)` — redundant single-expression `do`
- `(+ x 0)`, `(* x 1)` — identity arithmetic
- `(+ x 1)` → `(inc x)`, `(- x 1)` → `(dec x)` (from hyrule)

**Style rules (`--style`):**
- camelCase function names — use kebab-case
- Missing docstrings on `defn`/`defmacro`
- Trailing commas in parameter lists
- Earmuff variables (`*var*`) — use UPPER_SNAKE or kebab-case

### Acknowledgements

The whole library uses [pygments](https://pygments.org/).
The autoformatter relies on polymorphic dispatch provided by [multimethod](https://coady.github.io/multimethod/).


### Docs

The docstrings are not bad. Otherwise, try clicking below.

[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/atisharma/beautifhy)
