MunifTanjim / prettier.nvim

Prettier plugin for Neovim's built-in LSP client.
MIT License
289 stars 9 forks source link

[Question] Package.json search process #23

Closed sentientmachin3 closed 1 year ago

sentientmachin3 commented 1 year ago

Hi, not really an issue, more like a how-does-it-work question.

I have a monorepo with the following setup:

root --> package.json with prettier entry
root/package-a ---> package.json without prettier entry

I noticed when editing a file in the root/package-a path, the lsp does not format the code using prettier. I guess this happens because the plugin stops searching after the first package.json found, which does not contain the prettier entry, is this correct?

Great job btw, works flawlessly.

sockthedev commented 1 year ago

I have the same question. Is it possible to override the config to look for the package.json at the git root?

For example, in my tsserver config I do;

        root_dir = require("lspconfig").util.root_pattern(".git"),
sentientmachin3 commented 1 year ago

@sockthedev inspecting the code I think it starts checking from the folder vim was opened in.

MunifTanjim commented 1 year ago

To change the condition for starting prettierd server, you can do this:

prettier.setup({
  bin = "prettierd", -- (v0.23.3+)
  ["null-ls"] = {
    condition = function()
      -- will always start
      return true
      -- will start if you open vim in a folder with `package.json` file
      -- i.e. for javascript/typescript projects
      return require("lspconfig.util").find_package_json_ancestor(vim.fn.getcwd())
    end,
    -- ...
  },
  -- ...
})

The important thing is that the function pass to condition will only run once at startup. By default it checks if current working directory has prettier config file (.pretterrc*, prettier.config.*) or package.json with "prettier" key.

Note that, even if prettierd server is not running, you can still execute :Prettier command to format the file manually.

I hope that helps.