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

Configuration file loading #494

Closed
maobai405 opened this issue Jul 20, 2024 · 4 comments
Closed

Configuration file loading #494

maobai405 opened this issue Jul 20, 2024 · 4 comments

Comments

@maobai405
Copy link

In my config file I configured this:

      formatters = {
        injected = { options = { ignore_errors = true } },
        prettier = {
          prepend_args = { "--config= .. vim.fn.expand "~/.config/nvim/prettierrc.json" },
        },
        biome = {
          prepend_args = { "format", "--config-path=" .. vim.fn.expand "~/.config/nvim" },
        },
      },

However, I expected that if there's a prettierrc.json file in my project directory, it will be formatted according to the configuration in the project, instead of the configuration in the prepend_args path. How can I achieve this?

@rodhash
Copy link

rodhash commented Sep 15, 2024

Hi @maobai405, any luck with this?

@stevearc
Copy link
Owner

You can define prepend_args as a function

prepend_args = function(self, ctx)
  local util = require("conform.util")
  -- you may want to include all the possible config file names
  -- see https://github.com/stevearc/conform.nvim/blob/46c107ad0e7d83b5a7091112ec2994c847577d32/lua/conform/formatters/prettier.lua#L64-L77
  local config_file = vim.fs.find({".prettierrc", "prettierrc.json"}, { upward = true, path = ctx.dirname })[1]
  if config_file then
    return {} -- don't need to return anything because it will pick up the file from the cwd
  else
    return { "--config=" .. vim.fn.expand("~/.config/nvim/prettierrc.json") }
  end
end

@yamiteru
Copy link

What if I want to use biome only if there's biome.json in the directory?

@stevearc
Copy link
Owner

@yamiteru set require_cwd = true for the biome formatter. Then the formatter will only be used when the cwd function returns a value.

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

4 participants