stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
3.02k stars 156 forks source link

How to use prettier only when the project uses it? #407

Closed csntr closed 4 months ago

csntr commented 4 months ago

I have a nuxt project where I want to use antfo eslint config which says its supposed to work without prettier. In other projects the most common practice is to use prettier. I have configured conform to use prettier for most js/ts/vue files but in this case the best would be if it somehow could see that there is no prettier in the project so it could fallback to LSP formatting which is what I want optimally with the ESLint LSP. I didn't find anything on this, is this possible somehow?

hougesen commented 4 months ago

You can use the require_cwd option to disable prettier when no config is found.

You might have to overwrite cwd too since the default for prettier includes package.json.

local conform = require("conform")

conform.setup({
    formatters = {
        prettier = {
            -- cwd means "config working directory"
            require_cwd = true,

            cwd = require("conform.util").root_file({
                ".prettierrc",
                ".prettierrc.json",
                ".prettierrc.yml",
                ".prettierrc.yaml",
                ".prettierrc.json5",
                ".prettierrc.js",
                ".prettierrc.cjs",
                ".prettierrc.mjs",
                ".prettierrc.toml",
                "prettier.config.js",
                "prettier.config.cjs",
                "prettier.config.mjs",
            }),
        },
    },

    -- rest of your config
})

You can find the default prettier filenames here

https://github.com/stevearc/conform.nvim/blob/00f9d91391b04b1935e2f15948bd96cc111e7d3a/lua/conform/formatters/prettier.lua#L63-L78

rodhash commented 2 weeks ago

Is it possible to point to a default .prettierrc in a place different than home directory?

ro0gr commented 2 weeks ago

@rodhash do prettier/prettier_d themselves support it?

rodhash commented 2 weeks ago

Hmm I'm not really sure .. on my personal computer it works just fine but at work I have a special set up that's why I was trying to move it away from home dir.

rodhash commented 2 weeks ago

Another question pls: Conform reads the standard Prettier configuration from somewhere? Perhaps some config file?

I'm asking this because even when no ".prettierrc.json" exists Conform still manages to format the file with lua require("conform").format()

I even removed the .prettierrc.json just to test it and it works.

Checking ConformInfo things look just fine:

Formatters for this buffer:                                                                                    
LSP: ts_ls, null-ls                                                                                            
prettierd ready (javascriptreact, typescript, typescriptreact, graphql, css, json, yaml, jsonc, less, scss, mar
ro0gr commented 2 weeks ago

By default conform assumes a project with a package.json should be prettier enabled.

Please check the comment above for more details https://github.com/stevearc/conform.nvim/issues/407#issuecomment-2120988992

rodhash commented 2 weeks ago

I see. Yeah I noticed that comment 407.

What I find interesting is: apparently Conform uses a standard configuration even when no .prettierrc (or any of the other files) is provided ..

That's what I'm trying to understand, where this standard configuration is coming from because I can run Conform on any JS / TS files apparently.