format support
def f(x):
.;
. |
. + 1 |
f(3)
the indent of def just like python's def. the indent of | just like sh's pipe.
TIA!
Hey, currently there is no plan for adding formatting support, but it would be nice to have. If i would add it i think i would like it be style-"preserving", ex make white space like new lines affect formatting, also placement of comments might be a bit tricky. To do this i think jq-lsp would have to use a parser that includes whitespace and comment, the current gojq based parser is not suitable for this.
BTW have you looked at https://github.com/noperator/jqfmt?
Also maybe this jqjq issue can be interesting https://github.com/wader/jqjq/issues/8 (there is a jqjq branch with an unfinished AST to string function)
Oh, I haven't looked at jqfmt. It looks like jqfmt is simple to be integrated to jq-lsp, right?
https://github.com/noperator/jqfmt?tab=readme-ov-file#to-do
It looks jqfmt will add a PR to support gojq fmt?
Not sure how well the current jqfmt would work as a formatter to use while writing jq code. I have a feeling it might be a bit too aggressive with reformatting, as mentioned above i would like to have something that is a bit style aware. And also not sure how well it handles invalid syntax? also seem to strip comments atm
$ jqfmt <<EOT
heredoc> # comment
heredoc> 1+2
heredoc> EOT
1 + 2
https://github.com/noperator/jqfmt?tab=readme-ov-file#to-do It looks jqfmt will add a PR to support
gojq fmt?
I asked about it some years ago https://github.com/itchyny/gojq/issues/62#issuecomment-762745347 maybe could happen if someone would take on the task of doing it in a nice way and also convince itchyny. I think it would require quite a bit of changes to the parser and also tricky heuristics how to preserve things. But similar things has been done for other languages so it's probably technically possible. I would probably develop it independently of gojq first, maybe using a tree-sitter gramma etc?
seem to strip comments atm
It shouldn't happened. Is it a bug?
seem to strip comments atm
It shouldn't happened. Is it a bug?
I would say so, but might be tricky to fix because of how gojq's parser work. Comments can be in many places so it's tricky to get them into an AST, maybe one would need another kind of syntax tree that is more suitable for formatting than compiling/evaluating.