Plugin for formatting embedded(injected) code (e.g. lua
in markdown
)using null-ls in NeoVim.
Embedded languages are found using injections.csm
treesitter queries from nvim-treesitter,
so if the highlighting works, the formatting should as well.
nvim.mp4
packer:
use({ "LostNeophyte/null-ls-embedded" })
Add this plugin to the null-ls sources or use the builtin functions.
To get the best results add it as the last one.
require("null-ls").setup({
sources = {
-- other sources
require("null-ls-embedded").nls_source,
},
})
The null-ls source is enabled only for some filetypes by default, to configure them use this:
require("null-ls").setup({
sources = {
require("null-ls-embedded").nls_source.with({
-- default filetypes:
filetypes = { "markdown", "html", "vue", "lua" },
}),
},
})
Format by calling vim.lsp.buf.format
.
Range formatting is supported with this method (as long as the formatter will format the selected range).
require("null-ls-embedded").buf_format()
- format every code block in the bufferrequire("null-ls-embedded").format_current()
- format the current code block
local config = {
-- don't format these injected languages
ignore_langs = {
["*"] = { "comment" }, -- ignore `comment` in all languages
markdown = { "markdown_inline" }, -- ignore `markdown_inline` in `markdown`
},
timeout = 1000,
}
require("null-ls-embedded").config(config)
If you want the plugin to detect additional code blocks, add the treesitter queries to injections.csm
.
- TJ DeVries: Magically format embedded languages in Neovim
- NeoVim: Code for getting treesitter injections