Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Commit

Permalink
fix: handle filetypes without TS queries
Browse files Browse the repository at this point in the history
  • Loading branch information
LostInTheLogs committed May 21, 2023
1 parent 26a7c27 commit efdd657
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# null-ls-embedded

Plugin for formatting embedded code using [null-ls](https://github.com/jose-elias-alvarez/null-ls.nvim) in NeoVim.
Plugin for formatting embedded(injected) code (e.g. `lua` in `markdown`)using [null-ls](https://github.com/jose-elias-alvarez/null-ls.nvim) in NeoVim.
Embedded languages are found using `injections.csm` treesitter queries from [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter),
so if the highlighting works, the formatting should as well.

Expand Down Expand Up @@ -41,10 +41,22 @@ require("null-ls").setup({
})
```

The null-ls source is enabled only for some filetypes by default, to configure them use this:

```lua
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
Expand All @@ -54,6 +66,7 @@ Range formatting is supported with this method (as long as the formatter will fo

```lua
local config = {
-- don't format these injected languages
ignore_langs = {
["*"] = { "comment" }, -- ignore `comment` in all languages
markdown = { "markdown_inline" }, -- ignore `markdown_inline` in `markdown`
Expand Down
6 changes: 5 additions & 1 deletion lua/null-ls-embedded/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ end
function M.get_ts_injection_nodes(bufnr)
local root_lang = vim.api.nvim_buf_get_option(bufnr, "filetype")
local query = require("nvim-treesitter.query").get_query(root_lang, "injections")
if not query then
require("null-ls.logger"):warn("[null-ls-embedded] Couldn't find TS queries for " .. root_lang)
return {}
end

local root = vim.treesitter.get_parser(bufnr):parse()[1]:root()

Expand All @@ -211,7 +215,7 @@ function M.get_ts_injection_nodes(bufnr)
local name = query.captures[id]

if name == "language" and not lang then
lang = vim.treesitter.query.get_node_text(node, bufnr)
lang = vim.treesitter.get_node_text(node, bufnr)
elseif name == "content" and #nodes == 0 then
table.insert(nodes, node)
elseif string.sub(name, 1, 1) ~= "_" then
Expand Down

0 comments on commit efdd657

Please sign in to comment.