我正在尝试使用 `prettier` 来格式化某些类型的文件,但我的 `formatter.lua` 设置似乎不起作用,可能是什么问题?

问题描述 投票:0回答:1

我是

NeoVim
生态系统的新手。我根据
ThePrimeage's
视频设置了我的配置。之后,我尝试使用
formatter.nvim
包为
NeoVim
安装附加格式。

这就是我的

formatter.lua
文件的样子:

vim.cmd([[nnoremap <silent> <leader>fm :Format<CR>]])

local util = require("formatter.util")

local function prettier()
    return {
        exe = "prettier",
        args = {
            "--write",
            "--config-precedence",
            "prefer-file",
            "--single-quote",
            "--no-bracket-spacing",
            "--prose-wrap",
            "always",
            "--arrow-parens",
            "always",
            "--trailing-comma",
            "all",
            "--no-semi",
            "--end-of-line",
            "lf",
            "--print-width",
            vim.bo.textwidth,
            "--stdin-filepath",
            vim.fn.shellescape(vim.api.nvim_buf_get_name(0)),
        },
        stdin = true,
    }
end

require("formatter").setup({
    logging = true,
    log_level = vim.log.levels.WARN,
    filetype = {
        javascript = { prettier },
        typescript = { prettier },
        javascriptreact = { prettier },
        typescriptreact = { prettier },
        vue = { prettier },
        ["javascript.jsx"] = { prettier },
        ["typescript.tsx"] = { prettier },
        markdown = { prettier },
        css = { prettier },
        json = { prettier },
        jsonc = { prettier },
        scss = { prettier },
        less = { prettier },
        yaml = { prettier },
        graphql = { prettier },
        html = { prettier },
        reason = {
            function()
                return {
                    exe = "refmt",
                    stdin = true,
                }
            end,
        },
        lua = {
            require("formatter.filetypes.lua").stylua,

            function()
                if util.get_current_buffer_file_name() == "special.lua" then
                    return nil
                end

                return {
                    exe = "stylua",
                    args = {
                        "--search-parent-directories",
                        "--stdin-filepath",
                        util.escape_path(util.get_current_buffer_file_path()),
                        "--",
                        "-",
                    },
                    stdin = true,
                }
            end,
        },
        go = {
            function()
                return {
                    exe = "gofmt",
                    args = { "-s", "-w", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)) },
                    stdin = false,
                }
            end,
        },
        c = {
            function()
                return {
                    exe = "clang-format",
                    args = { "-i", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)) },
                    stdin = false,
                }
            end,
        },
        rust = {
            function()
                return {
                    exe = "rustfmt",
                    args = { "--emit=stdout", "--edition=2018", vim.fn.fnameescape(vim.api.nvim_buf_get_name(0)) },
                    stdin = false,
                }
            end,
        },
    },
})

我在我的系统上全局安装了

prettier
/usr/local/bin/prettier

当我尝试提供更漂亮的可执行文件的相对路径而不是仅仅编写

prettier
时,它找不到格式化程序。

我的项目目录中有默认的

prettier
配置文件(
.prettierrc.json
)

注意:我也尝试过其他格式的配置,但没有任何结果。

注意:对其他文件进行格式化确实有效(

Go
Rust
C
)。只是
prettier
好像有问题。

注意:我也使用

prettier
安装了
Mason
,但它没有解决问题,只是在执行格式化时更改了
prettier
路径(此命令 -
print(vim.inspect(vim.fn["systemlist"]("which prettier")))

如何才能在我的

prettier
设置中成功利用
NeoVim
格式?

linux ubuntu lua neovim prettier
1个回答
0
投票

遇到同样的问题。尝试过 null-ls 但放弃了它的存档。

我现在使用vim-prettier

使用它将其添加到打包机中。但无论你使用什么,请更换纱线。

use { 'prettier/vim-prettier',
        run = 'yarn install',
        ft = { 'javascript', 'typescript', 'css', 'less', 'scss', 'graphql', 'markdown', 'vue', 'html' }
    }

我现在要做的是,如果存在 prettierrc 文件,则将键绑定更改为

<leader>f
;如果不存在,则默认为 buff 格式。

© www.soinside.com 2019 - 2024. All rights reserved.