/null-ls-embedded

Format embedded(injected) code using null-ls in NeoVim

Primary LanguageLuaApache License 2.0Apache-2.0

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.

nvim.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