Merge pull request #1540 from snoweuph/feat/template-injecitons
Some checks are pending
Set up binary cache / cachix (default) (push) Waiting to run
Set up binary cache / cachix (maximal) (push) Waiting to run
Set up binary cache / cachix (nix) (push) Waiting to run
Treewide Checks / Validate flake (push) Waiting to run
Treewide Checks / Check formatting (push) Waiting to run
Treewide Checks / Check source tree for typos (push) Waiting to run
Treewide Checks / Validate documentation builds (push) Waiting to run
Treewide Checks / Validate documentation builds-1 (push) Waiting to run
Treewide Checks / Validate documentation builds-2 (push) Waiting to run
Treewide Checks / Validate documentation builds-3 (push) Waiting to run
Treewide Checks / Validate hyperlinks in documentation sources (push) Waiting to run
Treewide Checks / Validate Editorconfig conformance (push) Waiting to run
Build and deploy documentation / Check latest commit (push) Waiting to run
Build and deploy documentation / publish (push) Blocked by required conditions

Injections, Injections,... INJECTIONS!
This commit is contained in:
Snoweuph 2026-04-24 22:33:35 +02:00 committed by GitHub
commit 4139469926
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 150 additions and 104 deletions

View file

@ -22,7 +22,7 @@ foo = mkLuaInline ''
vim.treesitter.queries = [{
type = "injections";
filetypes = ["nix"];
content = ''
query = ''
;; extends
((apply_expression

View file

@ -277,12 +277,24 @@
- Added {option}`vim.treesitter.queries` to support adding custom queries.
- Added injections for `vim.treesitter.queries.*.content` as `query` and
`mkLualine`, `entryAnywhere`, `entryBefore`, `entryAfter` as `lua` in nix.
- Added injections for `query = '' ... ''` as `query` and `mkLualine '' ... ''`,
`entryAnywhere '' ... ''`, `entryBefore [] '' ... ''`,
`entryAfter [] '' ... ''` as `lua` in nix.
- Added {option}`vim.languages.tera.treesitter.injection` to configure, what
language the content is.
- Added {option}`vim.languages.jinja.treesitter.injection` to configure, what
language the content is.
- Added {option}`vim.treesitter.filetypeMappings` to support mappings similar to
<https://github.com/nvim-treesitter/nvim-treesitter/blob/main/plugin/filetypes.lua>.
This is mostly use full for Markdown code block injections.
- Added some Tree-sitter filetype mappings for:
- `bash` = `ash`, `dash`, `zsh`
- `yaml` = `yaml`
- Added `vim.lsp.presets.<name>` to contain LSP configurations. This allows for
more flexibility in nvf and reuse of LSPs across languages. Dropped
`deprecatedSingleOrListOf` in favor of `listOf` for the affected LSP options.

View file

@ -95,15 +95,19 @@ in {
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
vim.treesitter = {
enable = true;
grammars = [cfg.treesitter.package];
# not perfect mappings, but better than none
filetypeMappings.bash = ["ash" "dash" "zsh"];
};
})
(mkIf cfg.lsp.enable {
vim.lsp = {
presets = genAttrs cfg.lsp.servers (_: {enable = true;});
servers = genAttrs cfg.lsp.servers (_: {
filetypes = ["bash" "sh" "zsh"];
filetypes = ["bash" "sh" "ash" "dash" "zsh"];
});
};
})

View file

@ -287,7 +287,7 @@ in {
{
type = "injections";
filetypes = ["gotmpl"];
content = ''
query = ''
;; extends
((text) @injection.content

View file

@ -7,7 +7,7 @@
inherit (lib) genAttrs;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.options) literalExpression mkEnableOption mkOption;
inherit (lib.types) enum listOf;
inherit (lib.types) enum listOf str;
inherit (lib.nvim.types) mkGrammarOption;
cfg = config.vim.languages.jinja;
@ -27,6 +27,11 @@ in {
};
package = mkGrammarOption pkgs "jinja";
inlinePackage = mkGrammarOption pkgs "jinja_inline";
injection = mkOption {
type = str;
default = "html";
description = "Treesitter language to inject in Jinja templates";
};
};
lsp = {
@ -46,11 +51,27 @@ in {
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [
cfg.treesitter.package
cfg.treesitter.inlinePackage
];
vim.treesitter = {
enable = true;
grammars = [
cfg.treesitter.package
cfg.treesitter.inlinePackage
];
queries = [
{
type = "injections";
filetypes = ["jinja"];
query = ''
;; extends
((content) @injection.content
(#set! injection.language "${cfg.treesitter.injection}")
(#set! injection.combined)
)
'';
}
];
};
})
(mkIf cfg.lsp.enable {

View file

@ -132,53 +132,41 @@ in {
enable = true;
grammars = [cfg.treesitter.package];
queries = [
# vim.treesitter.queries.*.content
# query = ''; -> query
{
type = "injections";
filetypes = ["nix"];
content = ''
query = ''
;; extends
((binding
attrpath: (attrpath
(identifier) @_a
(identifier) @_b
(identifier)? @_c)
(#eq? @_a "vim")
(#any-of? @_b "treesitter")
(#any-of? @_c "queries")
expression: (attrset_expression
(binding_set
(binding
attrpath: (attrpath
(identifier) @_queries)
(#eq? @_queries "queries")
expression: (list_expression
(attrset_expression
(binding_set
(binding
attrpath: (attrpath
(identifier) @_field)
(#eq? @_field "content")
expression: [
(string_expression
(string_fragment) @injection.content)
(indented_string_expression
(string_fragment) @injection.content)
]
(#set! injection.language "query")
(#set! injection.combined))))))))))
(identifier) @_path)
(#eq? @_path "query")
expression: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "query")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "query")))
(apply_expression
argument: [
(string_expression
((string_fragment) @injection.content
(#set! injection.language "query")))
(indented_string_expression
((string_fragment) @injection.content
(#set! injection.language "query")))
])
]))
'';
}
# mkLuaInline, entryAnywhere, entryBefore, entryAfter = lua
# mkLuaInline, entryAnywhere, entryBefore, entryAfter -> lua
{
type = "injections";
filetypes = ["nix"];
content = ''
query = ''
;; extends
((apply_expression
@ -187,8 +175,8 @@ in {
(#any-of? @_func "mkLuaInline" "entryAnywhere"))
argument: (indented_string_expression
(string_fragment) @injection.content))
(#set! injection.language "lua")
(#set! injection.combined))
(#set! injection.language "lua")
(#set! injection.combined))
((apply_expression
function: (apply_expression
@ -198,8 +186,8 @@ in {
argument: (_))
argument: (indented_string_expression
(string_fragment) @injection.content))
(#set! injection.language "lua")
(#set! injection.combined))
(#set! injection.language "lua")
(#set! injection.combined))
'';
}
];

View file

@ -57,7 +57,7 @@ in {
{
type = "injections";
filetypes = ["tera"];
content = ''
query = ''
;; extends
((content) @injection.content

View file

@ -158,8 +158,6 @@ in {
};
config = mkIf cfg.enable (mkMerge [
{vim.globals.markdown_fenced_languages = ["ts=typescript"];}
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [

View file

@ -46,8 +46,11 @@ in {
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.package];
vim.treesitter = {
enable = true;
grammars = [cfg.treesitter.package];
filetypeMappings.yaml = ["yml"];
};
})
(mkIf cfg.lsp.enable {

View file

@ -24,78 +24,85 @@ in {
treesitter.grammars = optionals cfg.addDefaultGrammars cfg.defaultGrammars;
pluginRC.treesitter-autocommands = entryAfter ["basic"] ''
vim.api.nvim_create_augroup("nvf_treesitter", { clear = true })
pluginRC = {
treesitter-autocommands = entryAfter ["basic"] ''
vim.api.nvim_create_augroup("nvf_treesitter", { clear = true })
${lib.optionalString cfg.highlight.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
pcall(vim.treesitter.start)
end,
})
''}
${lib.optionalString cfg.indent.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = ${toLuaObject cfg.indent.pattern},
callback = function(args)
${optionalString (builtins.length cfg.indent.excludes > 0) ''
local ft = vim.bo[args.buf].filetype
if vim.tbl_contains(${toLuaObject cfg.indent.excludes}, ft) then
return
end
${lib.optionalString cfg.highlight.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
pcall(vim.treesitter.start)
end,
})
''}
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
''}
${lib.optionalString cfg.fold ''
-- Enable treesitter folding for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
vim.wo[0][0].foldmethod = "expr"
vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
end,
})
''}
'';
${lib.optionalString cfg.indent.enable ''
-- Enable treesitter highlighting for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = ${toLuaObject cfg.indent.pattern},
callback = function(args)
${optionalString (builtins.length cfg.indent.excludes > 0) ''
local ft = vim.bo[args.buf].filetype
if vim.tbl_contains(${toLuaObject cfg.indent.excludes}, ft) then
return
end
''}
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
end,
})
''}
${lib.optionalString cfg.fold ''
-- Enable treesitter folding for all filetypes
vim.api.nvim_create_autocmd("FileType", {
group = "nvf_treesitter",
pattern = "*",
callback = function()
vim.wo[0][0].foldmethod = "expr"
vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
end,
})
''}
'';
treesitter-filetype-mappings = entryAfter ["basic"] ''
for lang, ft in pairs(${toLuaObject cfg.filetypeMappings}) do
vim.treesitter.language.register(lang, ft)
end
'';
};
additionalRuntimePaths = mkIf (cfg.queries != []) [
(let
grouped =
foldl'
(
acc: query:
acc: entry:
foldl'
(
inner: filetype: let
path = "queries/${filetype}/${query.type}.scm";
path = "queries/${filetype}/${entry.type}.scm";
prev = inner.${path} or "";
in
inner
// {
${path} = prev + query.content;
${path} = prev + entry.query;
}
)
acc
query.filetypes
entry.filetypes
)
{}
cfg.queries;
files =
mapAttrsToList
(path: content: {
(path: query: {
name = path;
path = pkgs.writeText path content;
path = pkgs.writeText path query;
})
grouped;
in

View file

@ -4,7 +4,7 @@
...
}: let
inherit (lib.options) mkOption mkEnableOption literalExpression;
inherit (lib.types) listOf nullOr package bool str lines enum submodule oneOf;
inherit (lib.types) listOf nullOr package bool str lines enum submodule oneOf attrsOf;
queriesType = submodule {
options = {
@ -17,7 +17,7 @@
default = [];
description = "The filetypes for which the query should be registered.";
};
content = mkOption {
query = mkOption {
type = lines;
description = "The queries scm script.";
};
@ -111,5 +111,18 @@ in {
default = [];
description = "A list of Neovim treesitter queries to be registered.";
};
filetypeMappings = mkOption {
type = attrsOf (listOf str);
default = {};
example = {
"sh" = ["ash" "dash"];
};
description = ''
For each parser, registers a list of alternative filetypes.
For more information see `:h vim.treesitter.language.register()`.
See treesitter builtin mappings here: <https://github.com/nvim-treesitter/nvim-treesitter/blob/main/plugin/filetypes.lua>
'';
};
};
}