Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

* [Topiary](https://github.com/tweag/topiary): tree-sitter based uniform formatter
* This repo contains:
* languages.ncl: configuration that enables nushell
* nu.scm: tree-sitter query DSL that defines the behavior of the formatter for nushell
* `bin/topiary-nushell` wrapper for `topiary` that formats nushell files
* `languages.ncl`: configuration that enables nushell
* `languages/nu.scm`: tree-sitter query DSL that defines the behavior of the formatter for nushell
* `format.nu`: deprecated version of `bin/topiary-nushell`
* stand-alone tests written in nushell

## Status
Expand All @@ -30,22 +32,11 @@ cargo install topiary-cli
2. Clone this repo somewhere

```nushell
# e.g. to `$env.XDG_CONFIG_HOME/topiary`
git clone https://github.com/blindFS/topiary-nushell ($env.XDG_CONFIG_HOME | path join topiary)
cd ~ # Replace `~` with wherever you'd like this repo to live
git clone https://github.com/blindFS/topiary-nushell
```

3. Setup environment variables (Optional)

> [!WARNING]
> This is required if you want to do the formatting via vanilla topiary-cli, like in the neovim/helix settings below.
>
> While the [`format.nu`](https://github.com/blindFS/topiary-nushell/blob/main/format.nu) script in this repo just wraps that for you.

```nushell
# Set environment variables according to the path of the clone
$env.TOPIARY_CONFIG_FILE = ($env.XDG_CONFIG_HOME | path join topiary languages.ncl)
$env.TOPIARY_LANGUAGE_DIR = ($env.XDG_CONFIG_HOME | path join topiary languages)
```
3. Add the `topiary-nushell/bin` folder to your PATH environment variable

> [!WARNING]
> For windows users, if something went wrong the first time you run the formatter,
Expand Down Expand Up @@ -75,14 +66,11 @@ $env.TOPIARY_LANGUAGE_DIR = ($env.XDG_CONFIG_HOME | path join topiary languages)

## Usage

<details>
<summary>Using the <a href="https://github.com/blindFS/topiary-nushell/blob/main/format.nu">format.nu</a> wrapper </summary>

```markdown
Helper to run topiary with the correct environment variables for topiary-nushell
Wrapper for `topiary` that formats `nushell` files

Usage:
> format.nu {flags} ...(files)
> topiary-nushell {flags} ...(files)

Flags:
-c, --config_dir <path>: Root of the topiary-nushell repo, defaults to the parent directory of this script
Expand All @@ -101,29 +89,15 @@ Input/output types:

Examples:
Read from stdin
> bat foo.nu | format.nu
> bat foo.nu | topiary-nushell

Format files (in-place replacement)
> format.nu foo.nu bar.nu
> topiary-nushell foo.nu bar.nu

Path overriding
> format.nu -c /path/to/topiary-nushell foo.nu bar.nu
```

</details>

<details>
<summary>Using topiary-cli </summary>

```nushell
# in-place formatting
topiary format script.nu
# stdin -> stdout
cat foo.nu | topiary format --language nu
> topiary-nushell -c /path/to/topiary-nushell foo.nu bar.nu
```

</details>

### Locally Disable Formatting for Certain Expression

If you don't like the formatted output of certain parts of your code,
Expand All @@ -147,7 +121,7 @@ This will keep the let assignment as it is while formatting the rest of the code
<details>
<summary>Neovim </summary>
Format on save with <a href="https://github.com/stevearc/conform.nvim">conform.nvim</a>:

```lua
-- lazy.nvim setup
{
Expand All @@ -160,8 +134,7 @@ This will keep the let assignment as it is while formatting the rest of the code
},
formatters = {
topiary_nu = {
command = "topiary",
args = { "format", "--language", "nu" },
command = "topiary-nushell",
},
},
},
Expand All @@ -179,7 +152,7 @@ To format on save in Helix, add this configuration to your `helix/languages.toml
[[language]]
name = "nu"
auto-format = true
formatter = { command = "topiary", args = ["format", "--language", "nu"] }
formatter = { command = "topiary-nushell" }
```

</details>
Expand All @@ -192,7 +165,7 @@ formatter = { command = "topiary", args = ["format", "--language", "nu"] }
"Nu": {
"formatter": {
"external": {
"command": "/path-to-the-clone/format.nu"
"command": "topiary-nushell"
}
},
"format_on_save": "on"
Expand Down
22 changes: 22 additions & 0 deletions bin/topiary-nushell
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env -S nu --stdin

const repo_path = path self ..

# Wrapper for `topiary` that formats `nushell` files
@example "Read from stdin" { bat foo.nu | topiary-nushell }
@example "Format files (in-place replacement)" { topiary-nushell foo.nu bar.nu }
@example "Path overriding" { topiary-nushell -c /path/to/topiary-nushell-repo foo.nu bar.nu }
def main [
--config_dir (-c): path # Root of the topiary-nushell repo, defaults to the directory one level up from this script
...files: path # Files to format
]: [nothing -> nothing string -> string] {
let config_dir = $config_dir | default $repo_path
$env.TOPIARY_CONFIG_FILE = ($config_dir | path join languages.ncl)
$env.TOPIARY_LANGUAGE_DIR = ($config_dir | path join languages)

if ($files | is-not-empty) {
topiary format ...$files
} else {
topiary format --language nu
}
}
3 changes: 3 additions & 0 deletions bin/topiary-nushell.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
SET binDir=%~dp0
nu %binDir%topiary-nushell %*
9 changes: 5 additions & 4 deletions format.nu
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

const script_path = path self .

# Helper to run topiary with the correct environment variables for topiary-nushell
@example "Read from stdin" { bat foo.nu | format.nu }
@example "Format files (in-place replacement)" { format.nu foo.nu bar.nu }
@example "Path overriding" { format.nu -c /path/to/topiary-nushell foo.nu bar.nu }
# DEPRECATED. Migrate to `bin/topiary-nushell` (which supports being added to the system PATH variable).
# This `format.nu` script may be removed from the `topiary-nushell` repo in the future.
def main [
--config_dir (-c): path # Root of the topiary-nushell repo, defaults to the parent directory of this script
...files: path # Files to format
]: [nothing -> nothing string -> string] {

print -e "WARNING: This format.nu script is deprecated and may be removed in the future.\nMigrate to `bin/topiary-nushell`.\n"

let config_dir = $config_dir | default $script_path
$env.TOPIARY_CONFIG_FILE = ($config_dir | path join languages.ncl)
$env.TOPIARY_LANGUAGE_DIR = ($config_dir | path join languages)
Expand Down