From d805197b45cc853bd94e9b5deae57900e1bc9757 Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 10:43:17 -0600 Subject: [PATCH 1/7] refactor: copy `format.nu` to `bin/topiary-nushell` --- bin/topiary-nushell | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100755 bin/topiary-nushell diff --git a/bin/topiary-nushell b/bin/topiary-nushell new file mode 100755 index 0000000..9b7f003 --- /dev/null +++ b/bin/topiary-nushell @@ -0,0 +1,22 @@ +#!/usr/bin/env -S nu --stdin + +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 } +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] { + 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) + + if ($files | is-not-empty) { + topiary format ...$files + } else { + topiary format --language nu + } +} From d8889bba4ae9dc43b7c3650e917128b11120cde5 Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 10:47:36 -0600 Subject: [PATCH 2/7] refactor: deprecate format.nu --- format.nu | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/format.nu b/format.nu index 9b7f003..540e173 100755 --- a/format.nu +++ b/format.nu @@ -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) From ec778da380697ccca8029e6c3e7ee355a511a1da Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 10:55:54 -0600 Subject: [PATCH 3/7] fix: update default `config_dir` in `bin/topiary-nushell` --- bin/topiary-nushell | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/topiary-nushell b/bin/topiary-nushell index 9b7f003..82dcaea 100755 --- a/bin/topiary-nushell +++ b/bin/topiary-nushell @@ -1,6 +1,6 @@ #!/usr/bin/env -S nu --stdin -const script_path = path self . +const repo_path = path self .. # Helper to run topiary with the correct environment variables for topiary-nushell @example "Read from stdin" { bat foo.nu | format.nu } @@ -10,7 +10,7 @@ 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] { - let config_dir = $config_dir | default $script_path + 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) From ed5f24435e19382c876406e7ef0ab3beaf02de0c Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 13:05:05 -0600 Subject: [PATCH 4/7] docs: update bin/topiary-nushell help text --- bin/topiary-nushell | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/topiary-nushell b/bin/topiary-nushell index 82dcaea..080d4fe 100755 --- a/bin/topiary-nushell +++ b/bin/topiary-nushell @@ -2,12 +2,12 @@ const repo_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 } +# 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 parent directory of this script + --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 From 73fc414b4a26cc6dbf7cefbeafd2ec91f003cb54 Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 11:04:27 -0600 Subject: [PATCH 5/7] docs: expand and improve listing of repo files --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f6a7658..5a88758 100644 --- a/README.md +++ b/README.md @@ -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 @@ -77,7 +79,7 @@ $env.TOPIARY_LANGUAGE_DIR = ($env.XDG_CONFIG_HOME | path join topiary languages)
Using the format.nu wrapper - + ```markdown Helper to run topiary with the correct environment variables for topiary-nushell @@ -114,7 +116,7 @@ Examples:
Using topiary-cli - + ```nushell # in-place formatting topiary format script.nu @@ -147,7 +149,7 @@ This will keep the let assignment as it is while formatting the rest of the code
Neovim Format on save with conform.nvim: - + ```lua -- lazy.nvim setup { From f3018fd98539b2626722cb443df5b91b1a3c206e Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 11:49:21 -0600 Subject: [PATCH 6/7] docs: focus on usage of the `topiary-nushell` script --- README.md | 51 +++++++++++---------------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 5a88758..e679d41 100644 --- a/README.md +++ b/README.md @@ -32,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, @@ -77,14 +66,11 @@ $env.TOPIARY_LANGUAGE_DIR = ($env.XDG_CONFIG_HOME | path join topiary languages) ## Usage -
- Using the format.nu wrapper - ```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 : Root of the topiary-nushell repo, defaults to the parent directory of this script @@ -103,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 + > topiary-nushell -c /path/to/topiary-nushell foo.nu bar.nu ``` -
- -
- Using topiary-cli - -```nushell -# in-place formatting -topiary format script.nu -# stdin -> stdout -cat foo.nu | topiary format --language nu -``` - -
- ### Locally Disable Formatting for Certain Expression If you don't like the formatted output of certain parts of your code, @@ -162,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", }, }, }, @@ -181,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" } ```
@@ -194,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" From 8fc534124a585d2e986d108bb7bc691beb9a4087 Mon Sep 17 00:00:00 2001 From: ItsHarper Date: Sat, 29 Nov 2025 12:11:00 -0600 Subject: [PATCH 7/7] fix: add `bin/topiary-nushell.bat` for Windows --- bin/topiary-nushell.bat | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bin/topiary-nushell.bat diff --git a/bin/topiary-nushell.bat b/bin/topiary-nushell.bat new file mode 100644 index 0000000..0b1fba6 --- /dev/null +++ b/bin/topiary-nushell.bat @@ -0,0 +1,3 @@ +@echo off +SET binDir=%~dp0 +nu %binDir%topiary-nushell %*