jq-lsp icon indicating copy to clipboard operation
jq-lsp copied to clipboard

format support

Open Freed-Wu opened this issue 1 year ago • 7 comments

def f(x):
  .;

. |
  . + 1 |
  f(3)

the indent of def just like python's def. the indent of | just like sh's pipe.

TIA!

Freed-Wu avatar Oct 10 '24 08:10 Freed-Wu

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)

wader avatar Oct 10 '24 08:10 wader

Oh, I haven't looked at jqfmt. It looks like jqfmt is simple to be integrated to jq-lsp, right?

Freed-Wu avatar Oct 10 '24 12:10 Freed-Wu

https://github.com/noperator/jqfmt?tab=readme-ov-file#to-do It looks jqfmt will add a PR to support gojq fmt?

Freed-Wu avatar Oct 10 '24 12:10 Freed-Wu

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

wader avatar Oct 10 '24 12:10 wader

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?

wader avatar Oct 10 '24 12:10 wader

seem to strip comments atm

It shouldn't happened. Is it a bug?

Freed-Wu avatar Oct 10 '24 13:10 Freed-Wu

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.

wader avatar Oct 10 '24 14:10 wader