null-ls-embedded
null-ls-embedded copied to clipboard
Format embedded(injected) code using null-ls in NeoVim
null-ls-embedded
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.
https://user-images.githubusercontent.com/110467150/205495468-f0f8b9d7-5730-48d6-beb1-ea60dabb0021.mp4
installation
Dependencies:
packer:
use({ "LostNeophyte/null-ls-embedded" })
Formatting
Add this plugin to the null-ls sources or use the builtin functions.
As a null-ls source
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).
By calling functions
-
require("null-ls-embedded").buf_format()
- format every code block in the buffer -
require("null-ls-embedded").format_current()
- format the current code block
Configuration
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)
Additional queries
If you want the plugin to detect additional code blocks, add the treesitter queries to injections.csm
.
Credits
- TJ DeVries: Magically format embedded languages in Neovim
- NeoVim: Code for getting treesitter injections