lxxxvi / ruboclean

Ruby gem that puts `.rubocop.yml` into order.
MIT License
18 stars 4 forks source link

Work with editor code formatters #47

Open paul opened 1 month ago

paul commented 1 month ago

Most editors can invoke an external command to format a file, but they don't expect the file to change out from under them.

The simplest would be to support a --stdin flag, that would read the file in stdin, and output the formatted file to stdout.

FWIW, I was able to get it to work with Neovim & conform.vim using the tempfile feature. I'm not sure how many other editors make that possible, though.

formatters = {
  ruboclean = {
    condition = function(ctx)
      return vim.fs.basename(ctx.filename) ~= ".rubocop.yml"
    end,
    command = "ruboclean",
    args = { "$FILENAME", "--preserve-comments", "--preserve-paths", "--silent" },
    stdin = false,
  },
},
formatters_by_ft = {
  yaml = { "ruboclean" }
}
lxxxvi commented 1 month ago

Hi @paul 👋

Thanks for your input, I appreciate it.

I don't use neovim (or alike), so I may not entirely understand what's going on there. 🙃

Your idea is to add another way to feed in the YAML data, STDIN instead of reading from a file, and then write the result (the processed YAML) to STDOUT, is that right?

paul commented 1 month ago

@lxxxvi Yes, exactly. Similar to how rubocop has a --stdin flag: https://docs.rubocop.org/rubocop/usage/basic_usage.html#command-line-flags