stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.72k stars 142 forks source link

bug: Can't format lua using prettier "No parser could be inferred for file" (OR change specific formatting style setting in stylua) #386

Open MikeLemo1 opened 3 months ago

MikeLemo1 commented 3 months ago

Neovim version (nvim -v)

0.10

Operating system/version

Ubuntu

Add the debug logs

Log file

15:52:19[ERROR] Formatter 'prettier' error: [error] No parser could be inferred for file "/home/user/.config/nvim/lua/user/lspconfig.lua".

Describe the bug

Using "prettier" to fomat a lua file produce the log error unlike with stylue and yes both of them are isntalled with mason

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

open nvim and then :lua require("conform").format()

Expected Behavior

Format the document...

Minimal example file

No response

Minimal init.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/conform.nvim",
    config = function()
      require("conform").setup({
        log_level = vim.log.levels.DEBUG,
        -- add your config here
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Additional context

I'm basically trying to find a formatter that will not add spaces after parenteses for all nodes or remove or add lines between master nodes but other than the indentwidth in stylua I can't find a similar setting

stevearc commented 3 months ago

Please paste the contents of :ConformInfo in a lua file and ensure that you have included a full config for a minimal repro (I don't see anything about prettier in the one provided)

savchenko commented 2 months ago

Summary

I can confirm this bug, tried re-implementing prettier as the standalone parser, but the issue persists.

I have prettier binary in $PATH. Launching it from CLI works fine:

cat index.js | prettier --range-start 4554 --range-end 5058 --stdin-filepath index.js

Error

14:09:42[ERROR] Formatter 'ugly' error: [error] No parser and no file path given, couldn't infer a parser.

Configuration

conform.formatters.ugly = { -- {{{
    stdin = true,
    command = "prettier",
    args = {
        "--no-color",
        "--stdin-filepath",
        vim.api.nvim_buf_get_name(0),
    },
    cwd = require("conform.util").root_file({
        ".prettierrc.json",
        ".prettierrc",
        "prettier.config.js",
    }),
    range_args = function(self) -- {{{

        current_mode = tonumber(table.concat(
            vim.fn.str2list(vim.api.nvim_get_mode().mode)
        ))

        local len_selection, len_before, cursor, _from
        local _esc = vim.api.nvim_replace_termcodes('<esc>', true, false, true)
        local start_char = vim.api.nvim_call_function('getcharpos', { 'v' })
        local end_char = vim.api.nvim_call_function('getcharpos', { '.' })
        local start_line = math.min(start_char[2], end_char[2])
        local end_line = math.max(start_char[2], end_char[2])

        -- n
        if current_mode == 110 then
            cursor = vim.fn.getcursorcharpos()
            vim.fn.setcursorcharpos(start_line, 0)
            len_before = vim.fn.wordcount().cursor_chars
            vim.fn.setcursorcharpos(cursor[2], cursor[3])
            len_selection = vim.fn.strchars(vim.api.nvim_get_current_line())
            return { '--range-start', len_before, '--range-end', len_before + len_selection }

        -- v, V, ^V
        elseif current_mode == 118 or current_mode == 86 or current_mode == 22 then
            if start_line ~= end_line then
                _from = start_char[2] == start_line and start_char or end_char
            else
                _from = start_char[3] < end_char[3] and start_char or end_char
            end
            len_selection = tonumber(vim.fn.wordcount().visual_chars)
            cursor = vim.fn.getcursorcharpos()
            vim.api.nvim_feedkeys(_esc, 'x', false)
            vim.fn.setcursorcharpos(_from[2], _from[3])
            len_before = vim.fn.wordcount().cursor_chars
            vim.api.nvim_feedkeys('gv', 'x', false)
            vim.fn.setcursorcharpos(cursor[2], cursor[3])
            return { '--range-start', len_before, '--range-end', len_before + len_selection }
        else
            vim.print('Mode ' .. tostring(current_mode) .. ' is not implemented!')
            return {}
        end

    end -- }}}
} -- }}}
savchenko commented 1 month ago

@stevearc , could you please check the message above?