diff --git a/CHANGELOG.md b/CHANGELOG.md index 40c9460..311ab88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog]. settings ([#261]). ### Formatters +* [`dprint`](https://dprint.dev) for various (depending on + [installed plugins](https://dprint.dev/plugins/)) ([#209]). * [`js-beautify`](https://github.com/beautify-web/js-beautify) for [JavaScript](https://www.javascript.com/), [JSON](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON), @@ -25,6 +27,7 @@ The format is based on [Keep a Changelog]. * [cljfmt](https://github.com/weavejester/cljfmt) for clojure, clojurescript, edn files. ([#271]) +[#209]: https://github.com/radian-software/apheleia/pull/209 [#229]: https://github.com/radian-software/apheleia/pull/229 [#257]: https://github.com/radian-software/apheleia/pull/257 [#260]: https://github.com/radian-software/apheleia/pull/260 diff --git a/apheleia-formatters.el b/apheleia-formatters.el index e1ce8c1..c5e7f93 100644 --- a/apheleia-formatters.el +++ b/apheleia-formatters.el @@ -56,6 +56,7 @@ (denofmt-md . ("deno" "fmt" "-" "--ext" "md")) (denofmt-ts . ("deno" "fmt" "-" "--ext" "ts")) (denofmt-tsx . ("deno" "fmt" "-" "--ext" "tsx")) + (dprint . ("dprint" "fmt" "--stdin" filepath)) (elm-format . ("elm-format" "--yes" "--stdin")) (fish-indent . ("fish_indent")) (fourmolu . ("fourmolu")) @@ -291,12 +292,14 @@ rather than using this system." (cmake-mode . cmake-format) (cmake-ts-mode . cmake-format) (common-lisp-mode . lisp-indent) + (conf-toml-mode . dprint) (cperl-mode . perltidy) (crystal-mode . crystal-tool-format) (css-mode . prettier-css) (css-ts-mode . prettier-css) (dart-mode . dart-format) (dart-ts-mode . dart-format) + (dockerfile-mode . dprint) (elixir-mode . mix-format) (elixir-ts-mode . mix-format) (elm-mode . elm-format) diff --git a/test/formatters/apheleia-ft.el b/test/formatters/apheleia-ft.el index a8ac9d4..1e1f4ce 100755 --- a/test/formatters/apheleia-ft.el +++ b/test/formatters/apheleia-ft.el @@ -269,8 +269,10 @@ environment variable, defaulting to all formatters." (dolist (in-file (apheleia-ft--input-files formatter)) (let ((extension (file-name-extension in-file)) (in-text (apheleia-ft--read-file in-file)) + ;; The `in-temp-real-file' variable is set to whatever + ;; temporary file the formatter will run on (in case it + ;; uses the `file' or `filepath' symbol or is a function). (in-temp-real-file nil) - (in-temp-file nil) (out-temp-file nil) (command (alist-get (intern formatter) apheleia-formatters)) (syms nil) @@ -292,15 +294,15 @@ environment variable, defaulting to all formatters." ;; Some formatters use the current file-name or buffer-name to interpret the ;; type of file that is being formatted. Some may not be able to determine ;; this from the contents of the file so we set this to force it. - (rename-buffer in-file) + (rename-buffer (file-name-nondirectory in-file)) (setq stdout-buffer (get-buffer-create (format "*apheleia-ft-stdout-%S%s" formatter extension))) (with-current-buffer stdout-buffer (erase-buffer)) (if (functionp command) - (progn - (setq in-temp-file (apheleia-ft--write-temp-file - in-text extension)) + (let ((in-temp-file (apheleia-ft--write-temp-file + in-text extension))) + (setq in-temp-real-file in-temp-file) (with-current-buffer (find-file-noselect in-temp-file) (funcall command :buffer (current-buffer) @@ -308,14 +310,20 @@ environment variable, defaulting to all formatters." :formatter formatter :callback (lambda ())) (copy-to-buffer stdout-buffer (point-min) (point-max)))) - (progn - - (let ((ctx (apheleia--formatter-context - (intern formatter) command nil nil))) - (setq command `(,(apheleia-formatter--arg1 ctx) - ,@(apheleia-formatter--argv ctx)) - in-temp-real-file (apheleia-formatter--input-fname ctx) - out-temp-file (apheleia-formatter--output-fname ctx))) + (let ((in-temp-file (apheleia-ft--write-temp-file + in-text extension))) + (with-current-buffer (find-file-noselect in-temp-file) + (let ((ctx (apheleia--formatter-context + (intern formatter) command nil nil))) + (setq command `(,(apheleia-formatter--arg1 ctx) + ,@(apheleia-formatter--argv ctx)) + ;; In this case the real temp file might be + ;; different from the one we generated, because + ;; the context creator might generate another + ;; temporary file to avoid touching our existing + ;; one. + in-temp-real-file (apheleia-formatter--input-fname ctx) + out-temp-file (apheleia-formatter--output-fname ctx)))) (with-current-buffer stdout-buffer (erase-buffer)) diff --git a/test/formatters/installers/dprint.bash b/test/formatters/installers/dprint.bash new file mode 100644 index 0000000..929c4b9 --- /dev/null +++ b/test/formatters/installers/dprint.bash @@ -0,0 +1,19 @@ +npm install -g dprint + +# Included: WASM plugins, preferring first over 3rd party wrappers +# where redundant (e.g. -typescript and -json instead of -biome) +cat <<\EOF >/tmp/dprint.json +{ + "excludes": [], + "plugins": [ + "https://plugins.dprint.dev/dockerfile-0.3.0.wasm", + "https://plugins.dprint.dev/json-0.19.1.wasm", + "https://plugins.dprint.dev/markdown-0.16.3.wasm", + "https://plugins.dprint.dev/ruff-0.0.2.wasm", + "https://plugins.dprint.dev/toml-0.5.4.wasm", + "https://plugins.dprint.dev/typescript-0.88.7.wasm" + ] +} +EOF + +# dprint config update # not done to keep formatting stable diff --git a/test/formatters/samplecode/dprint/in.dockerfile b/test/formatters/samplecode/dprint/in.dockerfile new file mode 100644 index 0000000..444eb9e --- /dev/null +++ b/test/formatters/samplecode/dprint/in.dockerfile @@ -0,0 +1 @@ +FROM scratch diff --git a/test/formatters/samplecode/dprint/in.js b/test/formatters/samplecode/dprint/in.js new file mode 120000 index 0000000..da49303 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.js @@ -0,0 +1 @@ +../prettier-javascript/in.js \ No newline at end of file diff --git a/test/formatters/samplecode/dprint/in.json b/test/formatters/samplecode/dprint/in.json new file mode 120000 index 0000000..599a1e2 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.json @@ -0,0 +1 @@ +../prettier-json/in.json \ No newline at end of file diff --git a/test/formatters/samplecode/dprint/in.md b/test/formatters/samplecode/dprint/in.md new file mode 120000 index 0000000..99482b8 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.md @@ -0,0 +1 @@ +../prettier-markdown/in.md \ No newline at end of file diff --git a/test/formatters/samplecode/dprint/in.py b/test/formatters/samplecode/dprint/in.py new file mode 120000 index 0000000..ebc1034 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.py @@ -0,0 +1 @@ +../ruff/in.py \ No newline at end of file diff --git a/test/formatters/samplecode/dprint/in.toml b/test/formatters/samplecode/dprint/in.toml new file mode 100644 index 0000000..1f05503 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.toml @@ -0,0 +1 @@ +title = "Apheleia dprint TOML test" diff --git a/test/formatters/samplecode/dprint/in.ts b/test/formatters/samplecode/dprint/in.ts new file mode 120000 index 0000000..1b5f9d6 --- /dev/null +++ b/test/formatters/samplecode/dprint/in.ts @@ -0,0 +1 @@ +../prettier-typescript/in.ts \ No newline at end of file diff --git a/test/formatters/samplecode/dprint/out.dockerfile b/test/formatters/samplecode/dprint/out.dockerfile new file mode 100644 index 0000000..c35f1b5 --- /dev/null +++ b/test/formatters/samplecode/dprint/out.dockerfile @@ -0,0 +1 @@ +FROM scratch diff --git a/test/formatters/samplecode/dprint/out.js b/test/formatters/samplecode/dprint/out.js new file mode 100644 index 0000000..c0c58c0 --- /dev/null +++ b/test/formatters/samplecode/dprint/out.js @@ -0,0 +1,3 @@ +function HelloWorld({ greeting = "hello", greeted = "\"World\"", silent = false, onMouseOver }) { + if (!greeting) return null; +} diff --git a/test/formatters/samplecode/dprint/out.json b/test/formatters/samplecode/dprint/out.json new file mode 100644 index 0000000..59bb3b4 --- /dev/null +++ b/test/formatters/samplecode/dprint/out.json @@ -0,0 +1,19 @@ +{ + "arrowParens": "always", + "bracketSpacing": true, + "embeddedLanguageFormatting": "auto", + "htmlWhitespaceSensitivity": "css", + "insertPragma": false, + "jsxBracketSameLine": false, + "jsxSingleQuote": false, + "printWidth": 80, + "proseWrap": "preserve", + "quoteProps": "as-needed", + "requirePragma": false, + "semi": true, + "singleQuote": false, + "tabWidth": 2, + "trailingComma": "es5", + "useTabs": false, + "vueIndentScriptAndStyle": false +} diff --git a/test/formatters/samplecode/dprint/out.md b/test/formatters/samplecode/dprint/out.md new file mode 100644 index 0000000..4d08fcd --- /dev/null +++ b/test/formatters/samplecode/dprint/out.md @@ -0,0 +1,4 @@ +| col1 | col 2 | +| ------ | ---------- | +| nice | fits | +| oh no! | it's ugly! | diff --git a/test/formatters/samplecode/dprint/out.py b/test/formatters/samplecode/dprint/out.py new file mode 100644 index 0000000..0a63d13 --- /dev/null +++ b/test/formatters/samplecode/dprint/out.py @@ -0,0 +1,3 @@ +def asdjf(l, a): + 3 + +4 diff --git a/test/formatters/samplecode/dprint/out.toml b/test/formatters/samplecode/dprint/out.toml new file mode 100644 index 0000000..091bd6b --- /dev/null +++ b/test/formatters/samplecode/dprint/out.toml @@ -0,0 +1 @@ +title = "Apheleia dprint TOML test" diff --git a/test/formatters/samplecode/dprint/out.ts b/test/formatters/samplecode/dprint/out.ts new file mode 100644 index 0000000..143b5a7 --- /dev/null +++ b/test/formatters/samplecode/dprint/out.ts @@ -0,0 +1,6 @@ +interface GreetingSettings { + greeting: string; + duration?: number; + color?: string; +} +declare function greet(setting: GreetingSettings): void;