-
Notifications
You must be signed in to change notification settings - Fork 47
Add a Stdin Specification #586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,55 @@ | ||||||
--- | ||||||
outline: deep | ||||||
--- | ||||||
|
||||||
# Stdin Specification | ||||||
|
||||||
Formatters **MAY** also implement the Stdin Specification, which allows | ||||||
formatting "virtual files" passed via stdin. | ||||||
|
||||||
A formatter **MUST** implement the Stdin Specification if its formatting behavior | ||||||
can depend on the name of the file being formatted. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is a bit hard to read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, but I don't know how to fix it. Does flipping the sentence around help? Suggestions appreciated.
|
||||||
|
||||||
## Rules | ||||||
|
||||||
In order for the formatter to comply with this spec, it **MUST** implement the | ||||||
vanilla [Formatter Specification](/reference/formatter-spec), and additionally | ||||||
satisfy the following: | ||||||
|
||||||
### 1. `--stdin-filepath` flag | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
KISS? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm lightly opposed to this, for 2 reasons:
WDYT? |
||||||
|
||||||
The formatter's CLI **MUST** be of the form: | ||||||
|
||||||
``` | ||||||
<command> [options] [--stdin-filepath <path>] | ||||||
``` | ||||||
|
||||||
Where: | ||||||
|
||||||
- `<command>` is the name of the formatting tool. | ||||||
- `[options]` are any number of flags and options that the formatter accepts. | ||||||
- `--stdin-filepath <path>` is an optional flag that puts the formatter in | ||||||
"stdin mode". In stdin mode, the formatter reads file contents from stdin | ||||||
rather than the filesystem. | ||||||
- The formatter _MAY_ alter its behavior based on the given `<path>`. For | ||||||
example, if a there are different formatting rules in different | ||||||
directories. If the formatter's behavior doesn't depend on the given | ||||||
`<path>`, it's ok to ignore it. | ||||||
- The formatter _MAY_ understand `--stdin-filepath=<path>` as well, but **MUST** | ||||||
understand the space separated variant. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would make sense to outline the purpose of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the path argument is not needed by the tool, it's also valid to ignore it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've taken a stab at this. Please take a look. |
||||||
|
||||||
Example: | ||||||
|
||||||
``` | ||||||
$ echo "{}" | nixfmt --stdin-filepath path/to/file.nix | ||||||
``` | ||||||
|
||||||
### 2. Print to stdout, do not assume file is present on filesystem | ||||||
|
||||||
When in stdin mode, the formatter: | ||||||
|
||||||
1. **MUST** print the formatted file to stdout. | ||||||
2. **MUST NOT** attempt to read the file on the filesystem. Instead, it | ||||||
**MUST** read from stdin. | ||||||
3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to | ||||||
temporary files elsewhere on disk, but _SHOULD_ clean them up when done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this support expressed in the treefmt.toml config?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we'd add a new
formatter.<mylanguage>.stdin
(orstdin_supported
) bool that defaults to false if unspecified. Feedback appreciated.Do you think the stdin spec is the place to put this? I was thinking this would go in our config docs.