conform.nvim icon indicating copy to clipboard operation
conform.nvim copied to clipboard

Should we try to grab Ruby syntax tree from bundle?

Open SamSaffron opened this issue 1 year ago • 1 comments

I made this in my local config:

			local function create_stree_formatter()
				-- Check if Gemfile.lock exists and contains syntax_tree using grep
				local has_stree_in_bundle = function()
					local gemfile_lock = vim.fn.findfile("Gemfile.lock", ".;")
					if gemfile_lock ~= "" then
						local grep_result = vim.fn.system("grep -q syntax_tree " .. vim.fn.shellescape(gemfile_lock))
						return vim.v.shell_error == 0
					end
					return false
				end

				return {
					command = function()
						if has_stree_in_bundle() then
							return "bundle"
						else
							return "stree"
						end
					end,
					args = function()
						if has_stree_in_bundle() then
							return { "exec", "stree", "write", "$FILENAME" }
						else
							return { "write", "$FILENAME" }
						end
					end,
					stdin = false,
					cwd = util.root_file({ ".streerc" }),
				}
			end

Idea is to look in Gemfile.lock to see if syntax tree is there, if it is then use it via bundle exec, otherwise fallback to the installed gem.

should we make this default behavior ? It seems similar to a degree to what we do for node but the downside with ruby is that we need to grep inside a file (and it makes conform info a bit ugly cause it says:

syntax_tree ready (ruby) /home/sam/.gem/ruby/3.3.6/bin/bundle

vs this which would be a bit nicer

syntax_tree ready (ruby) /home/sam/.gem/ruby/3.3.6/bin/bundle exec stree

SamSaffron avatar Jan 30 '25 00:01 SamSaffron

I would be open to doing something like this as the default behavior. Shelling out to grep isn't great, though. If you want to try making a PR take a look at what we did for yew-fmt https://github.com/stevearc/conform.nvim/blob/master/lua/conform/formatters/yew-fmt.lua

stevearc avatar Feb 12 '25 05:02 stevearc