From 8c5d002afef960d99d6df1e8f40361fd9d919663 Mon Sep 17 00:00:00 2001
From: Zhang Yining <zhang.yining@gmail.com>
Date: Tue, 28 Nov 2023 22:18:55 +1100
Subject: [PATCH] add: support for `nickel format` as `Nickel` fixer

Nickel(https://nickel-lang.org/) is a configuration language, like
Jsonnet, Cue, Dhall.

`nickel`(https://github.com/tweag/nickel) is the main command to run,
export and also format Nickel code.

this commit adds `nickel format` as a Nickel fixer, together with some
tests and documentation.
---
 autoload/ale/fix/registry.vim                 |  7 ++++-
 autoload/ale/fixers/nickel_format.vim         | 16 +++++++++++
 doc/ale-nickel.txt                            | 25 +++++++++++++++++
 doc/ale-supported-languages-and-tools.txt     |  2 ++
 doc/ale.txt                                   |  2 ++
 supported-tools.md                            |  2 ++
 .../test_nickel_format_fixer_callback.vader   | 27 +++++++++++++++++++
 7 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 autoload/ale/fixers/nickel_format.vim
 create mode 100644 doc/ale-nickel.txt
 create mode 100644 test/fixers/test_nickel_format_fixer_callback.vader

diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
index 97346fb9e2..050cc6c705 100644
--- a/autoload/ale/fix/registry.vim
+++ b/autoload/ale/fix/registry.vim
@@ -635,7 +635,12 @@ let s:default_registry = {
 \       'function': 'ale#fixers#erbformatter#Fix',
 \       'suggested_filetypes': ['eruby'],
 \       'description': 'Apply erb-formatter -w to eruby/erb files.',
-\   }
+\   },
+\   'nickel_format': {
+\       'function': 'ale#fixers#nickel_format#Fix',
+\       'suggested_filetypes': ['nickel'],
+\       'description': 'Fix nickel files with nickel format',
+\   },
 \}
 
 " Reset the function registry to the default entries.
diff --git a/autoload/ale/fixers/nickel_format.vim b/autoload/ale/fixers/nickel_format.vim
new file mode 100644
index 0000000000..07eed8f9ed
--- /dev/null
+++ b/autoload/ale/fixers/nickel_format.vim
@@ -0,0 +1,16 @@
+" Author: Yining <zhang.yining@gmail.com>
+" Description: nickel format as ALE fixer for Nickel files
+
+call ale#Set('nickel_nickel_format_executable', 'nickel')
+call ale#Set('nickel_nickel_format_options', '')
+
+function! ale#fixers#nickel_format#Fix(buffer) abort
+    let l:executable = ale#Var(a:buffer, 'nickel_nickel_format_executable')
+    let l:options = ale#Var(a:buffer, 'nickel_nickel_format_options')
+
+    return {
+    \   'command': ale#Escape(l:executable) . ' format'
+    \       . (empty(l:options) ? '' : ' ' . l:options)
+    \}
+endfunction
+
diff --git a/doc/ale-nickel.txt b/doc/ale-nickel.txt
new file mode 100644
index 0000000000..a8dd6af83f
--- /dev/null
+++ b/doc/ale-nickel.txt
@@ -0,0 +1,25 @@
+===============================================================================
+ALE Nickel Integration                                      *ale-nickel-options*
+
+
+===============================================================================
+nickel_format                                         *ale-nickel-nickel-format*
+
+g:ale_nickel_nickel_format_executable    *g:ale_nickel_nickel_format_executable*
+                                         *b:ale_nickel_nickel_format_executable*
+  Type: |String|
+  Default: `'nickel'`
+
+  This option can be changed to change the path for `nickel`.
+
+
+g:ale_nickel_nickel_format_options          *g:ale_nickel_nickel_format_options*
+                                            *b:ale_nickel_nickel_format_options*
+  Type: |String|
+  Default: `''`
+
+  This option can be changed to pass extra options to `'nickel format'`
+
+
+===============================================================================
+  vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt
index 939b9870a5..23d884cf4e 100644
--- a/doc/ale-supported-languages-and-tools.txt
+++ b/doc/ale-supported-languages-and-tools.txt
@@ -381,6 +381,8 @@ Notes:
   * `mmc`!!
 * NASM
   * `nasm`!!
+* Nickel
+  * `nickel_format`
 * Nim
   * `nim check`!!
   * `nimlsp`
diff --git a/doc/ale.txt b/doc/ale.txt
index a2362ddc57..7af8d62cee 100644
--- a/doc/ale.txt
+++ b/doc/ale.txt
@@ -3187,6 +3187,8 @@ documented in additional help files.
     mmc...................................|ale-mercury-mmc|
   nasm....................................|ale-nasm-options|
     nasm..................................|ale-nasm-nasm|
+  nickel..................................|ale-nickel-options|
+    nickel_format.........................|ale-nickel-nickel-format|
   nim.....................................|ale-nim-options|
     nimcheck..............................|ale-nim-nimcheck|
     nimlsp................................|ale-nim-nimlsp|
diff --git a/supported-tools.md b/supported-tools.md
index 01999d0901..9fe7164e42 100644
--- a/supported-tools.md
+++ b/supported-tools.md
@@ -390,6 +390,8 @@ formatting.
   * [mmc](http://mercurylang.org) :floppy_disk:
 * NASM
   * [nasm](https://www.nasm.us/) :floppy_disk:
+* Nickel
+  * [nickel_format](https://github.com/tweag/nickel#formatting)
 * Nim
   * [nim check](https://nim-lang.org/docs/nimc.html) :floppy_disk:
   * [nimlsp](https://github.com/PMunch/nimlsp)
diff --git a/test/fixers/test_nickel_format_fixer_callback.vader b/test/fixers/test_nickel_format_fixer_callback.vader
new file mode 100644
index 0000000000..af3e0c2684
--- /dev/null
+++ b/test/fixers/test_nickel_format_fixer_callback.vader
@@ -0,0 +1,27 @@
+Before:
+  Save g:ale_nickel_nickel_format_executable
+  Save g:ale_nickel_nickel_format_options
+  Save &l:expandtab
+  Save &l:shiftwidth
+  Save &l:tabstop
+
+After:
+  Restore
+
+Execute(The nickel_format callback should return 'nickel format' as default command):
+  setlocal noexpandtab
+  Assert
+  \ ale#fixers#nickel_format#Fix(bufnr('')).command =~# '^' . ale#Escape('nickel') . ' format',
+  \ "Default command name is expected to be 'nickel format'"
+
+Execute(The nickel executable and options should be configurable):
+  let g:ale_nickel_nickel_format_executable = 'foobar'
+  let g:ale_nickel_nickel_format_options = '--some-option'
+
+  AssertEqual
+  \ {
+  \   'command': ale#Escape('foobar')
+  \     . ' format'
+  \     . ' --some-option',
+  \ },
+  \ ale#fixers#nickel_format#Fix(bufnr(''))