stevearc / conform.nvim

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

bug: (with tree-sitter) injected formatter should not process html tags inside markdown #485

Open powerman opened 3 months ago

powerman commented 3 months ago

Neovim version (nvim -v)

v0.10.0

Operating system/version

Gentoo Linux

Add the debug logs

Log file

23:48:42[DEBUG] Running formatters on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md: { "injected" } 23:48:42[INFO] Run injected on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md 23:48:42[DEBUG] Injected format html:30:32: { "djlint" } 23:48:42[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.1.html 23:48:42[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:42[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:42[DEBUG] Injected format html:14:18: { "djlint" } 23:48:42[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.2.html 23:48:42[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:42[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:42[DEBUG] Injected format html:8:10: { "djlint" } 23:48:42[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.3.html 23:48:42[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:42[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:42[DEBUG] djlint exited with code 0 23:48:42[DEBUG] djlint exited with code 0 23:48:42[DEBUG] djlint exited with code 0 23:48:43[DEBUG] Running formatters on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md: { "injected" } 23:48:43[INFO] Run injected on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md 23:48:43[DEBUG] Injected format html:8:19: { "djlint" } 23:48:43[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.2.html 23:48:43[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:43[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:44[DEBUG] djlint exited with code 0 23:48:45[DEBUG] Running formatters on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md: { "injected" } 23:48:45[INFO] Run injected on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md 23:48:45[DEBUG] Injected format html:8:21: { "djlint" } 23:48:45[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.2.html 23:48:45[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:45[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:45[DEBUG] djlint exited with code 0 23:48:46[DEBUG] Running formatters on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md: { "injected" } 23:48:46[INFO] Run injected on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md 23:48:46[DEBUG] Injected format html:8:22: { "djlint" } 23:48:46[INFO] Run djlint on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md.2.html 23:48:46[DEBUG] Run command: { "djlint", "--reformat", "--indent", 4, "-" } 23:48:46[DEBUG] Run default CWD: /home/powerman/tmp/bug-nvim-conform-injected-html 23:48:47[DEBUG] djlint exited with code 0 23:48:48[DEBUG] Running formatters on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md: { "injected" } 23:48:48[INFO] Run injected on /home/powerman/tmp/bug-nvim-conform-injected-html/README.md

Describe the bug

It looks like from injected (with tree-sitter) point of view in markdown both code blocks and html tags are "injected languages". As result injected tries to call html formatter on html tags inside markdown (not inside code block!). And this leads to really bad results: all available html formatters fail to handle this (in different ways), and only work around is to completely disable injected html formatter.

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

  1. Install any supported html formatter ('djlint' or 'prettierd' or 'htmlbeautifier'). They all have issues in this use case, just a different ones, so you can try any of them (just update repro.lua accordingly).
  2. Download example README.md file (attached below) to a directory containing repro.lua.
  3. nvim -u repro.lua README.md
  4. :lua require('conform').format() (repeat few times, result of repeating depends on chosen formatter).
  5. README.md will became incorrectly formatted (in different ways, depending on chosen formatter).

Expected Behavior

HTML tags inside markdown file shouldn't be processed by 'injected' when using plugin 'tree-sitter' with installed 'markdown' and 'html' languages.

Minimal example file

README.md

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
                formatters_by_ft = {
                    markdown = { "injected" },
                    html = { "djlint" },
                },
            })
        end,
    },
    -- add any other plugins here
    { -- Highlight, edit, and navigate code
        "nvim-treesitter/nvim-treesitter",
        build = ":TSUpdate",
        opts = {
            ensure_installed = {
                "bash",
                "html",
                "markdown",
            },
            auto_install = true,
            highlight = { enable = true },
            indent = { enable = true },
        },
        config = function(_, opts)
            require("nvim-treesitter.install").prefer_git = true
            ---@diagnostic disable-next-line: missing-fields
            require("nvim-treesitter.configs").setup(opts)
        end,
    },
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

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

Additional context

No response