fsouza/prettierd

how to confirm proper configuration

EdmundsEcho opened this issue · 5 comments

Thank you for putting this together. Using "format on save" and the like is a big productivity gain for me. I've recently migrated to lsp... (like many). I'm still getting my "bearings". I'm doing some work with css and html. I was using the online prettier parser and decided to see if I could get it running in nvim.

Prettierd is working (status running). I understand where the port and instance id are located to call it via a TCP request.

This said, in my nvim config I'm using the following documented snippet:

-- Prettier
local prettier = require("prettier")
prettier.setup({
    bin = 'prettierd', 
    formatCommand = 'prettierd "${INPUT}"',
    formatStdin = true,
    env = {string.format('PRETTIERD_DEFAULT_CONFIG=%s', vim.fn.expand('~/.config/nvim/utils/linter-config/.prettierrc.json'))},
    filetypes = {
        "css", "graphql", "html", "javascript", "javascriptreact", "json", "less", "markdown", "scss", "typescript", "typescriptreact", "yaml"
    },
})

As a "first go" I chose to use the null-ls plugin as an interface:

--
-- Linters and formatters that do not have an LSP interface; use null-ls to
-- bridge the gap.
local null_ls = require("null-ls")
require("null-ls").setup({
    capabilities = capabilities,
    on_attach = on_attach,
    sources = {
        null_ls.builtins.diagnostics.flake8, -- python
        null_ls.builtins.formatting.rustfmt, -- rust
        null_ls.builtins.formatting.brittany, -- haskell
        null_ls.builtins.diagnostics.yamllint, -- yaml
        null_ls.builtins.formatting.prettierd -- fast prettier
        -- null_ls.builtins.code_actions.eslint, -- html, css, js
    }
})

The nvim :checkhealth reports all is good with prettierd:

null-ls: require("null-ls.health").check()
========================================================================
  - ....
  - OK: yamllint: the command "yamllint" is executable.
  - OK: prettierd: the command "prettierd" is executable.

Yet, when I hit the <leader>f command, I don't see the css "straighten-up" (i.e., clean-up the formatting).

The mapping is is used by many lsp:

    -- bind conditional on server capabilities
    if client.resolved_capabilities.document_formatting then
        buf_set_keymap('n', '<Leader>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
    elseif client.resolved_capabilities.document_range_formatting then
        buf_set_keymap('n', '<Leader>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
    end

Have I failed to "connect all the dots"? How can I tell that prettierd has been invoked when I call <leader>f?

Thanks in advance for any pointers.

Hey, thanks for opening this issue. Is there a way to hook into

Can you try bypassing null-ls to see what happens? Something like:

cat file.css | prettierd file.css

If that prints the expected formatted file to stdout, then prettierd should be good. In that case, a good next step would be to try and see if null-ls is attached to the buffer? Inside neovim, you can run the command :lua print(vim.inspect(vim.lsp.get_active_clients())) maybe? That will print all clients, but if you only open the css file you should see only the clients that attach to that file.

Also curious on where you setting this up:

-- Prettier
local prettier = require("prettier")
prettier.setup({
    bin = 'prettierd', 
    formatCommand = 'prettierd "${INPUT}"',
    formatStdin = true,
    env = {string.format('PRETTIERD_DEFAULT_CONFIG=%s', vim.fn.expand('~/.config/nvim/utils/linter-config/.prettierrc.json'))},
    filetypes = {
        "css", "graphql", "html", "javascript", "javascriptreact", "json", "less", "markdown", "scss", "typescript", "typescriptreact", "yaml"
    },
})

What's that "prettier" module, is it coming from null-ls?

Great questions. Here are the answers.

cat file.css | prettierd file.css

I see the file get printed to stdout. However, despite prettier (not prettierd) finding ways to format the document, I don't see where or how prettierd found the same.

clients attached to document

When I run the nvim command :LspInfo, I get the following confirmation that null-ls is active:

 Language client log: /Users/edmund/.cache/nvim/lsp.log
 Detected filetype:   css
 
 1 client(s) attached to this buffer: 
 
 Client: null-ls (id: 1, pid: 5001, bufnr: [3, 4])
 	filetypes:       python, rust, haskell, javascript, typescriptreact, typescript, vue, javascriptreact, yaml, graphql, handlebars, html, markdown, json, jsonc, css, less, scss
 	autostart:       false
 	root directory:  /Users/edmund/Programming-Local/axum-seed
 	cmd:             nvim
 
:NullLsInfo
>>
	Active source(s)
	* name: prettierd
	* filetypes: javascriptreact | javascript | typescript | graphql | markdown | vue | less | html | jsonc | css | handlebars | scss | yaml | json | typescriptreact
	* methods: formatting
	

html language server services; one works the other doesn't

I have a couple of html lsp services. The one that works "directly" with lsp, seems to work.

1. html
2. null-ls

Select 1 will fix the alignment of the second line in the following:

<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>

The null-ls option served by prettierd does not change the buffer.

what is prettier pointing to in my configuration

I have removed this bit of the configuration. I'm using the builtin null-ls use of prettierd.

Question

What might prevent prettierd from formatting where either the html lsp and prettier seems to?

Answer

... .prettierignore entries

Solved? Yes, but...

Is it reasonable to expect this behavior?

The key take-away for me here is that no-matter how prettier is invoked, it will always consider the .prettierignore file. This is counter-intuitive when calling prettier for a specific file.

I could not find a way. I might post a suggestion on the prettier repo.

Thank you for your help.

Oh interesting, do you have a link to the issue in the prettier repo? I can keep an eye on it and implement any option that they add in prettier.

If there's a flag in prettier to skip the ignore file, we could eventually support it when #237 is resolved.

Brilliant minds think alike. Here is the link to the prettier issue