nix-community.nixvim/plugins/languages/treesitter/treesitter.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

338 lines
9.8 KiB
Nix
Raw Normal View History

{
lib,
helpers,
config,
pkgs,
...
}:
with lib;
helpers.neovim-plugin.mkNeovimPlugin config {
name = "treesitter";
originalName = "nvim-treesitter";
luaName = "nvim-treesitter.configs";
defaultPackage = pkgs.vimPlugins.nvim-treesitter;
general: add package options (#127) * barbar: package option * Base16: package option * gruvbox: package option * nord: package option * one: package option * onedark: package option * tokyonight: package option * nvim-cmp: package option * coq: package option * lspkind: package option * helpers: added package option to mkPlugin * fugitive: package option * gitgutter: package option * gitsigns: package option * neogit: package option * ledger: package option * nix: package option * plantuml-syntax: package option * treesitter-context: package option + formatting * treesitter-refactor: package option + formatting * treesitter: package option * zig: package option * null-ls: package option * null-ls/servers: package option * lsp-lines: package option * lspsaga: package option * trouble: package option * luasnip: added description for package option * airline: package option * lightline: package option * lualine: package option * telescope: package option * telescope/frecency: package option * telescope/fzf-native: package option * telescope/media-files: package option * comment-nvim: package option * vim-commentary: package option * dashboard: package option * easyescape: package option * emmet: package option * endwise: package option * floaterm: package option * goyo: package option * intellitab: package option * mark-radar: package option * notify: package option * nvim-autopairs: package option * nvim-tree: package option * project-nvim: package option * specs: package option * startify: package option * surround: package option * undotree: package option
2023-01-19 10:45:15 +00:00
maintainers = [ lib.maintainers.khaneliman ];
# TODO introduced 2024-07-06: remove after 24.11
optionsRenamedToSettings = [
"ensureInstalled"
"ignoreInstall"
"parserInstallDir"
[
"incrementalSelection"
"enable"
]
[
"incrementalSelection"
"keymaps"
"initSelection"
"nodeDecremental"
]
[
"incrementalSelection"
"keymaps"
"initSelection"
"nodeIncremental"
]
[
"incrementalSelection"
"keymaps"
"initSelection"
"scopeIncremental"
]
];
imports =
let
basePluginPath = [
"plugins"
"treesitter"
];
settingsPath = basePluginPath ++ [ "settings" ];
in
[
(lib.mkRenamedOptionModule (basePluginPath ++ [ "moduleConfig" ]) settingsPath)
(lib.mkRenamedOptionModule (basePluginPath ++ [ "customCaptures" ]) (
settingsPath
++ [
"highlight"
"custom_captures"
]
))
(lib.mkRenamedOptionModule (basePluginPath ++ [ "disabledLanguages" ]) (
settingsPath
++ [
"highlight"
"disable"
]
))
(lib.mkRenamedOptionModule (basePluginPath ++ [ "indent" ]) (
settingsPath
++ [
"indent"
"enable"
]
))
];
settingsOptions = {
auto_install = helpers.defaultNullOpts.mkBool false ''
Whether to automatically install missing parsers when entering a buffer.
'';
highlight = {
additional_vim_regex_highlighting =
helpers.defaultNullOpts.mkNullableWithRaw
(with helpers.nixvimTypes; either bool (listOf (maybeRaw str)))
false
''
Setting this to true will run `syntax` and tree-sitter at the same time. \
Set this to `true` if you depend on 'syntax' being enabled (e.g. for indentation). \
See `:h syntax`.
2021-02-01 16:21:42 +00:00
Using this option may slow down your editor, and you may see some duplicate highlights. \
Instead of true, it can also be a list of languages.
'';
2021-02-01 16:21:42 +00:00
enable = helpers.defaultNullOpts.mkBool false ''
Whether to enable treesitter highlighting.
'';
disable =
helpers.defaultNullOpts.mkStrLuaFnOr (with helpers.nixvimTypes; listOf (maybeRaw str)) null
''
Can either be a list of the names of parsers you wish to disable or
a lua function that returns a boolean indicating the parser should be disabled.
'';
custom_captures = helpers.defaultNullOpts.mkAttrsOf types.str { } ''
Custom capture group highlighting.
'';
};
incremental_selection = {
enable = helpers.defaultNullOpts.mkBool false ''
Incremental selection based on the named nodes from the grammar.
'';
2021-02-01 16:21:42 +00:00
keymaps =
let
mkKeymap = default: helpers.defaultNullOpts.mkStr default "Key shortcut";
2024-05-05 19:39:35 +02:00
in
{
init_selection = mkKeymap "gnn";
node_incremental = mkKeymap "grn";
scope_incremental = mkKeymap "grc";
node_decremental = mkKeymap "grm";
2021-02-01 16:40:07 +00:00
};
};
2021-02-01 16:21:42 +00:00
indent = {
enable = helpers.defaultNullOpts.mkBool false ''
Whether to enable treesitter indentation.
'';
};
ensure_installed = helpers.defaultNullOpts.mkNullable' {
type =
with helpers.nixvimTypes;
oneOf [
(enum [ "all" ])
(listOf (maybeRaw str))
rawLua
];
pluginDefault = [ ];
description = ''
Either `"all"` or a list of languages to ensure installing.
'';
};
ignore_install = helpers.defaultNullOpts.mkListOf types.str [ ] ''
List of parsers to ignore installing. Used when `ensure_installed` is set to `"all"`.
'';
parser_install_dir = helpers.mkNullOrOption' {
type = with helpers.nixvimTypes; maybeRaw str;
# Backport the default from nvim-treesitter 1.0
# The current default doesn't work on nix, as it is readonly
default.__raw = "vim.fs.joinpath(vim.fn.stdpath('data'), 'site')";
pluginDefault = lib.literalMD "the plugin's package directory";
description = ''
Location of the parsers to be installed by the plugin (only needed when `nixGrammars` is disabled).
By default, parsers are installed to the "site" dir.
If set to `null` the _plugin default_ is used, which will not work on nix.
'';
};
sync_install = helpers.defaultNullOpts.mkBool false ''
Install parsers synchronously (only applied to `ensure_installed`).
'';
};
settingsExample = {
auto_install = false;
ensure_installed = "all";
ignore_install = [ "rust" ];
parser_install_dir.__raw = "vim.fs.joinpath(vim.fn.stdpath('data'), 'treesitter')";
sync_install = false;
highlight = {
enable = true;
additional_vim_regex_highlighting = true;
disable = [ "rust" ];
custom_captures = { };
};
incremental_selection = {
enable = true;
keymaps = {
init_selection = "gnn";
node_decremental = "grm";
node_incremental = "grn";
scope_incremental = "grc";
2022-12-01 14:07:08 +00:00
};
};
indent = {
enable = true;
};
};
extraOptions = {
folding = mkOption {
type = types.bool;
default = true;
example = false;
description = "Whether to enable treesitter folding.";
};
gccPackage = helpers.mkPackageOption {
name = "gcc";
default = pkgs.gcc;
defaultText = literalExpression "pkgs.gcc";
example = literalExpression "pkgs.gcc14";
description = ''
Which package (if any) to be added as the GCC compiler.
This is required to build grammars if you are not using `nixGrammars`.
To disable the installation of GCC, set this option to `null`.
'';
};
grammarPackages = mkOption {
type = with types; listOf package;
default = config.plugins.treesitter.package.passthru.allGrammars;
example = literalExpression "pkgs.vimPlugins.nvim-treesitter.passthru.allGrammars";
defaultText = literalExpression "config.plugins.treesitter.package.passthru.allGrammars";
description = "Grammar packages to install";
};
# TODO: Implement rawLua support to be passed into extraConfigLua.
languageRegister = mkOption {
type = with types; attrsOf (coercedTo str toList (listOf str));
default = { };
example = {
cpp = "onelab";
python = [
"foo"
"bar"
];
};
description = ''
This is a wrapping of the `vim.treesitter.language.register` function.
Register specific parsers to one or several filetypes.
The keys are the parser names and the values are either one or several filetypes.
'';
2022-12-01 14:05:35 +00:00
};
nixGrammars = mkOption {
type = types.bool;
default = true;
example = false;
description = "Whether to install grammars defined in `grammarPackages`.";
};
2024-05-05 19:39:35 +02:00
nixvimInjections = mkOption {
type = types.bool;
default = true;
example = false;
description = "Whether to enable Nixvim injections, e.g. highlighting `extraConfigLua` as lua.";
};
2021-02-01 16:21:42 +00:00
nodejsPackage = helpers.mkPackageOption {
name = "nodejs";
default = pkgs.nodejs;
defaultText = literalExpression "pkgs.nodejs";
example = literalExpression "pkgs.nodejs_22";
description = ''
Which package (if any) to be added as the nodejs package.
This is required to build grammars if you are not using `nixGrammars`.
To disable the installation of NodeJS, set this option to `null`.
'';
};
treesitterPackage = helpers.mkPackageOption {
name = "tree-sitter";
default = pkgs.tree-sitter;
defaultText = literalExpression "pkgs.tree-sitter";
description = ''
Which package (if any) to be added as the tree-sitter binary.
This is required to build grammars if you are not using `nixGrammars`.
To disable the installation of tree-sitter, set this option to `null`.
'';
};
};
# NOTE: We call setup manually below.
callSetup = false;
# NOTE: We install cfg.package manually so we can install grammars using it.
installPackage = false;
extraConfig = cfg: {
extraConfigLua =
# NOTE: Upstream state that the parser MUST be at the beginning of runtimepath.
# Otherwise the parsers from Neovim takes precedent, which may be incompatible with some queries.
(optionalString (cfg.settings.parser_install_dir != null) ''
vim.opt.runtimepath:prepend(${helpers.toLuaObject cfg.settings.parser_install_dir})
'')
+ ''
require('nvim-treesitter.configs').setup(${helpers.toLuaObject cfg.settings})
''
+ (optionalString (cfg.languageRegister != { }) ''
do
local __parserFiletypeMappings = ${helpers.toLuaObject cfg.languageRegister}
2024-03-07 19:44:13 +01:00
for parser_name, ft in pairs(__parserFiletypeMappings) do
require('vim.treesitter.language').register(parser_name, ft)
end
end
'');
extraFiles = mkIf cfg.nixvimInjections { "queries/nix/injections.scm".source = ./injections.scm; };
extraPlugins = mkIf (cfg.package != null) [
(mkIf cfg.nixGrammars (cfg.package.withPlugins (_: cfg.grammarPackages)))
(mkIf (!cfg.nixGrammars) cfg.package)
];
extraPackages = [
cfg.gccPackage
cfg.nodejsPackage
cfg.treesitterPackage
];
opts = mkIf cfg.folding {
foldmethod = mkDefault "expr";
foldexpr = mkDefault "nvim_treesitter#foldexpr()";
};
};
}