helix: Formatter doesn't have access to filename
#2942 added formatter option, which is very useful.
I used it prettier but quickly discovered that I have conflicting formatting between CI and my editor.
This happens because prettier change formatting based on filename, e.g. package.json formatted with json-stringify but other *.json files formatted with json parser.
https://github.com/prettier/prettier/blob/2316e2fecd171335fc00f3ec97b7e46cc8964153/src/language-js/index.js#L81-L94
To solve this use case, prettier provides –stdin-filepath, but I can’t use it in helix since there is no way to pass the filename into fomatter option.
Having a placeholder of some kind would be great, e.g. $filename.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 23
- Comments: 23 (9 by maintainers)
Commits related to this issue
- feat(format): current file in formatter args Add support for a current file placeholder in formatter arguments. This should make it possible to run custom formatters, such as `rome` (Typescript), `ru... — committed to filipdutescu/helix by filipdutescu a year ago
- feat(format): current file in formatter args Add support for a current file placeholder in formatter arguments. This should make it possible to run custom formatters, such as `rome` (Typescript), `ru... — committed to filipdutescu/helix by filipdutescu a year ago
- feat(format): current file in formatter args Add support for a current file placeholder in formatter arguments. This should make it possible to run custom formatters, such as `rome` (Typescript), `ru... — committed to filipdutescu/helix by filipdutescu a year ago
- feat(format): current file in formatter args Add support for a current file placeholder in formatter arguments. This should make it possible to run custom formatters, such as `rome` (Typescript), `ru... — committed to filipdutescu/helix by filipdutescu a year ago
- feat(helix): lua-language-server support still no formatting with stylua, check https://github.com/helix-editor/helix/issues/3596 — committed to storopoli/flakes by storopoli 7 months ago
Previously #2942 did implement passing a file path to the formatter, but it was removed since stdin was deemed sufficient. Maybe this use case will change things.
You can manually specify the parser for prettier to use to avoid needing the file name, see https://prettier.io/docs/en/options.html#parser.
E.g.:
Just got mugged…
@zjr It wasn’t my intention. I wanted to contribute by giving a feedback as a user and to highlight the importance of this feature for formatters usually based on local config files. Nothing else.
And if I had enough time and if I knew rust well, it would have been with pleasure. Could you please give the benefit of doubt next time and not make hasty conclusions. It’s toxical for the community. Although English is not main language of many contributers and they can miss some nuances.
To address the original issue, it is possible to create a custom language block for a specific set of files. This can be done by adding the following block to your languages.toml file:
To get syntax highlighting to work, you also need to copy (or symlink) the
runtime/queries/jsondirectory toruntime/queries/package.json.This seems like the cleanest workaround for now (to me), but I agree that it would be best if helix supported passing the filename to the formatter.
I’m using this as a workaround.
I’ve encountered two cases where this would be useful:
clang-format-diffneeds to know what file you’re editing, so it can apply a relevant section of the diff. This is useful when you want to format new changes in a codebase that is not fully formatted.goimportsrequires a filename, not stdin. I think this is because it needs to know the name of the module when sorting imports.~ I’m wrong,goimportsworks over stdin.clang-formatwould also benefit from being able to pass the filename.@Anthuang this is hard to use, because the cli programmer know better than you. for prettier, it has
flow|babel|babel-flow|babel-ts|typescript|acorn|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwcparserswe can not count on a hard coded config to manual calculate the parser to use. this is bad.