stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.58k stars 136 forks source link

[feat]: add treesitter query formatter #425

Closed magnuslarsen closed 1 month ago

magnuslarsen commented 1 month ago

I found out that nvim-treesitter comes with a formatter; that they themselves use to format their queries:\ https://github.com/nvim-treesitter/nvim-treesitter/blob/ea2b137f35fb1e87a6471ec311805920fdf45745/.github/workflows/lint.yml#L36-L51

They maintain it, and it ships with the plugin... So why not use it?

I threw this a little hastily together; if you have some better ideas for the condition or anything else, please let me know :-)

stevearc commented 1 month ago

This is fantastic, thanks for the PR!

ribru17 commented 1 month ago

Thank you! For anyone using the latest nvim-treesitter, the query parser used to format the files got some breaking changes, so I had to use the following to get it to work (recognize the latest parser rather than the one bundled with neovim):

local qfmt = require('conform.formatters.format-queries')
local nvim_ts_path = vim.fn.stdpath('data') .. '/lazy/nvim-treesitter/'
util.add_formatter_args(qfmt, { '-c', 'set rtp^=' .. nvim_ts_path })

NOTE: This will only work when using lazy but you can replace nvim_ts_path with your path to nvim-treesitter and it will work

stevearc commented 1 month ago

@ribru17 thanks for the tip! I've incorporated that into the formatter

ribru17 commented 1 month ago

Awesome, thank you!

magnuslarsen commented 1 month ago

Thanks both of you!