stevearc / conform.nvim

Lightweight yet powerful formatter plugin for Neovim
MIT License
2.6k stars 136 forks source link

bug: Latex in Mardown doesn't format #298

Open nenikitov opened 5 months ago

nenikitov commented 5 months ago

Neovim version (nvim -v)

NVIM v0.10.0-dev-2359+g9f8c96240d

Operating system/version

Arch Linux 6.6.8

Add the debug logs

Log file

15:31:49[DEBUG] Running formatters on /home/nenikitov/test.md: { "injected" } 15:31:49[INFO] Run injected on /home/nenikitov/test.md 15:31:49[DEBUG] Injected format lua:2:3: { "stylua" } 15:31:49[INFO] Run stylua on /home/nenikitov/test.md.1.lua 15:31:49[DEBUG] Run command: { "stylua", "--search-parent-directories", "--stdin-filepath", "/home/nenikitov/test.md.1.lua", "-" } 15:31:49[DEBUG] stylua exited with code 0

Describe the bug

Latex injected into .md doesn't format.

It formats correctly inside .tex files.

What is the severity of this bug?

tolerable (can work around it)

Steps To Reproduce

  1. nvim -u repo.lua /path/to/example-file.md
  2. :w
  3. Observe how lua code is formatted, but not latex (inside ``` and $$)

Expected Behavior

Latex should be formatted using latexindent

Minimal example file

local a=10
\begin{bmatrix}
10
\end{bmatrix}
\begin{bmatrix}
10
\end{bmatrix}

$$ \begin{bmatrix} 10 \end{bmatrix} $$

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"},
          tex = {"latexindent"},
          lua = {"stylua"},
        },
        format_on_save = {
          timeout_ms = 500,
          lsp_fallback = false,
        }
      })
    end,
  },
  -- add any other plugins here
  {
    "WhoIsSethDaniel/mason-tool-installer.nvim",
    dependencies = {
      {
        "williamboman/mason.nvim",
        config = true
      },
    },
    config = function()
      require("mason-tool-installer").setup {
        ensure_installed = {
          "latexindent",
          "stylua",
        }
      }
    end
  },
  {
    "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate",
    config = function()
      require("nvim-treesitter.configs").setup({
        ensure_installed = {
          "markdown",
          "markdown_inline",
          "latex",
          "lua",
        }
      })
    end
  }
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

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

Additional context

No response

stevearc commented 4 months ago

Check out the options in this doc: https://github.com/stevearc/conform.nvim/blob/master/doc/formatter_options.md#injected

Open your markdown file and run :InspectTree, then press I to toggle the language of the nodes. This will help you find out the name of the language that is being used for the injection. If it says markdown, then something is going wrong with your injection (are the parsers installed?) Once you get the name (e.g. plaintex), add another entry to that lang_to_ext map. For example:

require("conform").setup({
  formatters = {
    injected = {
      options = {
        lang_to_ext = {
          plaintex = "tex"
        }
      }
    }
  }
})

If this seems like a setting that would make sense for everyone, feel free to open a PR to add it to the default list of lang_to_ext.