Add support for dprint (#249)

https://dprint.dev

---------

Co-authored-by: Radon Rosborough <radon@intuitiveexplanations.com>
This commit is contained in:
Ville Skyttä 2023-12-15 04:30:35 +02:00 committed by GitHub
parent f3cbdbcae8
commit 53c0389b5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 90 additions and 13 deletions

View file

@ -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

View file

@ -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)

View file

@ -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))

View file

@ -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

View file

@ -0,0 +1 @@
FROM scratch

View file

@ -0,0 +1 @@
../prettier-javascript/in.js

View file

@ -0,0 +1 @@
../prettier-json/in.json

View file

@ -0,0 +1 @@
../prettier-markdown/in.md

View file

@ -0,0 +1 @@
../ruff/in.py

View file

@ -0,0 +1 @@
title = "Apheleia dprint TOML test"

View file

@ -0,0 +1 @@
../prettier-typescript/in.ts

View file

@ -0,0 +1 @@
FROM scratch

View file

@ -0,0 +1,3 @@
function HelloWorld({ greeting = "hello", greeted = "\"World\"", silent = false, onMouseOver }) {
if (!greeting) return null;
}

View file

@ -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
}

View file

@ -0,0 +1,4 @@
| col1 | col 2 |
| ------ | ---------- |
| nice | fits |
| oh no! | it's ugly! |

View file

@ -0,0 +1,3 @@
def asdjf(l, a):
3
+4

View file

@ -0,0 +1 @@
title = "Apheleia dprint TOML test"

View file

@ -0,0 +1,6 @@
interface GreetingSettings {
greeting: string;
duration?: number;
color?: string;
}
declare function greet(setting: GreetingSettings): void;