Skip to content
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

format support #21

Open
Freed-Wu opened this issue Oct 10, 2024 · 7 comments
Open

format support #21

Freed-Wu opened this issue Oct 10, 2024 · 7 comments

Comments

@Freed-Wu
Copy link

Freed-Wu commented Oct 10, 2024

def f(x):
  .;

. |
  . + 1 |
  f(3)

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

TIA!

@wader
Copy link
Owner

wader commented Oct 10, 2024

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 wader/jqjq#8 (there is a jqjq branch with an unfinished AST to string function)

@Freed-Wu
Copy link
Author

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

@Freed-Wu
Copy link
Author

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

@wader
Copy link
Owner

wader commented Oct 10, 2024

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
Copy link
Owner

wader commented Oct 10, 2024

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 itchyny/gojq#62 (comment) 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?

@Freed-Wu
Copy link
Author

seem to strip comments atm

It shouldn't happened. Is it a bug?

@wader
Copy link
Owner

wader commented Oct 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants