Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
powerman opened this issue Jul 2, 2024 · 0 comments
Open
1 task done
Labels
bug Something isn't working P0 Highest priority, will receive attention

Comments

@powerman
Copy link

powerman commented Jul 2, 2024

Neovim version (nvim -v)

v0.10.0

Operating system/version

Gentoo Linux

Add the debug logs

  • I have set log_level = vim.log.levels.DEBUG and pasted the log contents below.

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).
  • prettierd will produce extra closing tags and thus break formatting.
  • djlint and htmlbeautifier won't produce final result on first run, it'll take about 4-5 times to apply formatting until they'll get final version - also with broken formatting, but in another way.

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

@powerman powerman added the bug Something isn't working label Jul 2, 2024
@stevearc stevearc added the P0 Highest priority, will receive attention label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P0 Highest priority, will receive attention
Projects
None yet
Development

No branches or pull requests

2 participants